-
Notifications
You must be signed in to change notification settings - Fork 346
/
api.js
308 lines (255 loc) · 11 KB
/
api.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
window.CaptureAPI = (function() {
var MAX_PRIMARY_DIMENSION = 15000 * 2,
MAX_SECONDARY_DIMENSION = 4000 * 2,
MAX_AREA = MAX_PRIMARY_DIMENSION * MAX_SECONDARY_DIMENSION;
//
// URL Matching test - to verify we can talk to this URL
//
var matches = ['http://*/*', 'https://*/*', 'ftp://*/*', 'file://*/*'],
noMatches = [/^https?:\/\/chrome.google.com\/.*$/];
function isValidUrl(url) {
// couldn't find a better way to tell if executeScript
// wouldn't work -- so just testing against known urls
// for now...
var r, i;
for (i = noMatches.length - 1; i >= 0; i--) {
if (noMatches[i].test(url)) {
return false;
}
}
for (i = matches.length - 1; i >= 0; i--) {
r = new RegExp('^' + matches[i].replace(/\*/g, '.*') + '$');
if (r.test(url)) {
return true;
}
}
return false;
}
function initiateCapture(tab, callback) {
chrome.tabs.sendMessage(tab.id, {msg: 'scrollPage'}, function() {
// We're done taking snapshots of all parts of the window. Display
// the resulting full screenshot images in a new browser tab.
callback();
});
}
function capture(data, screenshots, sendResponse, splitnotifier) {
chrome.tabs.captureVisibleTab(
null, {format: 'png'}, function(dataURI) {
if (dataURI) {
var image = new Image();
image.onload = function() {
data.image = {width: image.width, height: image.height};
// given device mode emulation or zooming, we may end up with
// a different sized image than expected, so let's adjust to
// match it!
if (data.windowWidth !== image.width) {
var scale = image.width / data.windowWidth;
data.x *= scale;
data.y *= scale;
data.totalWidth *= scale;
data.totalHeight *= scale;
}
// lazy initialization of screenshot canvases (since we need to wait
// for actual image size)
if (!screenshots.length) {
Array.prototype.push.apply(
screenshots,
_initScreenshots(data.totalWidth, data.totalHeight)
);
if (screenshots.length > 1) {
if (splitnotifier) {
splitnotifier();
}
$('screenshot-count').innerText = screenshots.length;
}
}
// draw it on matching screenshot canvases
_filterScreenshots(
data.x, data.y, image.width, image.height, screenshots
).forEach(function(screenshot) {
screenshot.ctx.drawImage(
image,
data.x - screenshot.left,
data.y - screenshot.top
);
});
// send back log data for debugging (but keep it truthy to
// indicate success)
sendResponse(JSON.stringify(data, null, 4) || true);
};
image.src = dataURI;
}
});
}
function _initScreenshots(totalWidth, totalHeight) {
// Create and return an array of screenshot objects based
// on the `totalWidth` and `totalHeight` of the final image.
// We have to account for multiple canvases if too large,
// because Chrome won't generate an image otherwise.
//
var badSize = (totalHeight > MAX_PRIMARY_DIMENSION ||
totalWidth > MAX_PRIMARY_DIMENSION ||
totalHeight * totalWidth > MAX_AREA),
biggerWidth = totalWidth > totalHeight,
maxWidth = (!badSize ? totalWidth :
(biggerWidth ? MAX_PRIMARY_DIMENSION : MAX_SECONDARY_DIMENSION)),
maxHeight = (!badSize ? totalHeight :
(biggerWidth ? MAX_SECONDARY_DIMENSION : MAX_PRIMARY_DIMENSION)),
numCols = Math.ceil(totalWidth / maxWidth),
numRows = Math.ceil(totalHeight / maxHeight),
row, col, canvas, left, top;
var canvasIndex = 0;
var result = [];
for (row = 0; row < numRows; row++) {
for (col = 0; col < numCols; col++) {
canvas = document.createElement('canvas');
canvas.width = (col == numCols - 1 ? totalWidth % maxWidth || maxWidth :
maxWidth);
canvas.height = (row == numRows - 1 ? totalHeight % maxHeight || maxHeight :
maxHeight);
left = col * maxWidth;
top = row * maxHeight;
result.push({
canvas: canvas,
ctx: canvas.getContext('2d'),
index: canvasIndex,
left: left,
right: left + canvas.width,
top: top,
bottom: top + canvas.height
});
canvasIndex++;
}
}
return result;
}
function _filterScreenshots(imgLeft, imgTop, imgWidth, imgHeight, screenshots) {
// Filter down the screenshots to ones that match the location
// of the given image.
//
var imgRight = imgLeft + imgWidth,
imgBottom = imgTop + imgHeight;
return screenshots.filter(function(screenshot) {
return (imgLeft < screenshot.right &&
imgRight > screenshot.left &&
imgTop < screenshot.bottom &&
imgBottom > screenshot.top);
});
}
function getBlobs(screenshots) {
return screenshots.map(function(screenshot) {
var dataURI = screenshot.canvas.toDataURL();
// convert base64 to raw binary data held in a string
// doesn't handle URLEncoded DataURIs
var byteString = atob(dataURI.split(',')[1]);
// separate out the mime component
var mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0];
// write the bytes of the string to an ArrayBuffer
var ab = new ArrayBuffer(byteString.length);
var ia = new Uint8Array(ab);
for (var i = 0; i < byteString.length; i++) {
ia[i] = byteString.charCodeAt(i);
}
// create a blob for writing to a file
var blob = new Blob([ab], {type: mimeString});
return blob;
});
}
function saveBlob(blob, filename, index, callback, errback) {
filename = _addFilenameSuffix(filename, index);
function onwriteend() {
// open the file that now contains the blob - calling
// `openPage` again if we had to split up the image
var urlName = ('filesystem:chrome-extension://' +
chrome.i18n.getMessage('@@extension_id') +
'/temporary/' + filename);
callback(urlName);
}
// come up with file-system size with a little buffer
var size = blob.size + (1024 / 2);
// create a blob for writing to a file
var reqFileSystem = window.requestFileSystem || window.webkitRequestFileSystem;
reqFileSystem(window.TEMPORARY, size, function(fs){
fs.root.getFile(filename, {create: true}, function(fileEntry) {
fileEntry.createWriter(function(fileWriter) {
fileWriter.onwriteend = onwriteend;
fileWriter.write(blob);
}, errback); // TODO - standardize error callbacks?
}, errback);
}, errback);
}
function _addFilenameSuffix(filename, index) {
if (!index) {
return filename;
}
var sp = filename.split('.');
var ext = sp.pop();
return sp.join('.') + '-' + (index + 1) + '.' + ext;
}
function captureToBlobs(tab, callback, errback, progress, splitnotifier) {
var loaded = false,
screenshots = [],
timeout = 3000,
timedOut = false,
noop = function() {};
callback = callback || noop;
errback = errback || noop;
progress = progress || noop;
if (!isValidUrl(tab.url)) {
errback('invalid url'); // TODO errors
}
// TODO will this stack up if run multiple times? (I think it will get cleared?)
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
if (request.msg === 'capture') {
progress(request.complete);
capture(request, screenshots, sendResponse, splitnotifier);
// https://developer.chrome.com/extensions/messaging#simple
//
// If you want to asynchronously use sendResponse, add return true;
// to the onMessage event handler.
//
return true;
} else {
console.error('Unknown message received from content script: ' + request.msg);
errback('internal error');
return false;
}
});
chrome.tabs.executeScript(tab.id, {file: 'page.js'}, function() {
if (timedOut) {
console.error('Timed out too early while waiting for ' +
'chrome.tabs.executeScript. Try increasing the timeout.');
} else {
loaded = true;
progress(0);
initiateCapture(tab, function() {
callback(getBlobs(screenshots));
});
}
});
window.setTimeout(function() {
if (!loaded) {
timedOut = true;
errback('execute timeout');
}
}, timeout);
}
function captureToFiles(tab, filename, callback, errback, progress, splitnotifier) {
captureToBlobs(tab, function(blobs) {
var i = 0,
len = blobs.length,
filenames = [];
(function doNext() {
saveBlob(blobs[i], filename, i, function(filename) {
i++;
filenames.push(filename);
i >= len ? callback(filenames) : doNext();
}, errback);
})();
}, errback, progress, splitnotifier);
}
return {
captureToBlobs: captureToBlobs,
captureToFiles: captureToFiles
};
})();