-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcapture.js
216 lines (183 loc) · 7.01 KB
/
capture.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
const MAX_PRIMARY_DIMENSION = 15000 * 2,
MAX_SECONDARY_DIMENSION = 4000 * 2,
MAX_AREA = MAX_PRIMARY_DIMENSION * MAX_SECONDARY_DIMENSION;
const isValidUrl = (string) => {
let url;
try {
url = new URL(string);
} catch (ex) {
return false;
}
return url.protocol === 'http:' || url.protocol === 'https:';
};
const initiateCapture = (tab, fullPage, callback) => {
chrome.tabs.sendMessage(tab.id, {msg: fullPage ? 'scrollPage' : 'visibleOnly'}, () => {
callback();
});
};
const capture = (data, screenshots, sendResponse, format, quality) => {
return new Promise((resolve, reject) => {
chrome.tabs.captureVisibleTab(resultWindowId, { format, quality }, (dataURI) => {
if (dataURI) {
let image = new Image();
image.onload = () => {
data.image = {width: image.width, height: image.height};
if (data.windowWidth !== image.width) {
let scale = image.width / data.windowWidth;
data.x *= scale;
data.y *= scale;
data.totalWidth *= scale;
data.totalHeight *= scale;
}
let output = {};
if (!screenshots.length) {
output = initScreenshots(data.totalWidth, data.totalHeight);
Array.prototype.push.apply(
screenshots,
output.results
);
}
filterScreenshots(
data.x, data.y, image.width, image.height, screenshots
).forEach((screenshot) => {
screenshot.ctx.drawImage(
image,
data.x - screenshot.left,
data.y - screenshot.top
);
});
sendResponse(JSON.stringify(data, null, 4) || true);
resolve(output.sizes);
};
image.src = dataURI;
}
});
});
};
const initScreenshots = (totalWidth, totalHeight) => {
let 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;
let canvasIndex = 0;
let results = [];
let sizes = [];
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);
sizes.push({ width: canvas.width, height: canvas.height });
left = col * maxWidth;
top = row * maxHeight;
results.push({
canvas: canvas,
ctx: canvas.getContext('2d'),
index: canvasIndex,
left: left,
right: left + canvas.width,
top: top,
bottom: top + canvas.height
});
canvasIndex++;
}
}
return { results, sizes };
};
const filterScreenshots = (imgLeft, imgTop, imgWidth, imgHeight, screenshots) => {
let imgRight = imgLeft + imgWidth,
imgBottom = imgTop + imgHeight;
return screenshots.filter((screenshot) => {
return (imgLeft < screenshot.right &&
imgRight > screenshot.left &&
imgTop < screenshot.bottom &&
imgBottom > screenshot.top);
});
};
const getBlobs = (screenshots) => {
return screenshots.map((screenshot) => {
const dataURI = screenshot.canvas.toDataURL();
const byteString = atob(dataURI.split(',')[1]);
const mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0];
const ab = new ArrayBuffer(byteString.length);
const ia = new Uint8Array(ab);
for (let i = 0; i < byteString.length; i++)
ia[i] = byteString.charCodeAt(i);
return new Blob([ab], {type: mimeString});
});
};
const saveBlob = (blob, filename, callback, errback) => {
callback(filename);
};
const captureToBlobs = (tab, format, quality, fullPage, callback, errback, progress) => {
let loaded = false,
screenshots = [],
sizes = [],
timeout = 3000,
timedOut = false,
noOp = () => {};
callback = callback || noOp;
errback = errback || noOp;
progress = progress || noOp;
if (!isValidUrl(tab.url))
errback('invalid url');
const listener = (request, sender, sendResponse) => {
if (request.msg === 'capture') {
progress(request.complete);
capture(request, screenshots, sendResponse, format, quality).then((res) => {
Array.prototype.push.apply(
sizes,
res
);
});
return true;
}
else {
console.error('Unknown message received from content script: ' + request.msg);
errback('internal error');
return false;
}
};
chrome.runtime.onMessage.addListener(listener);
chrome.scripting.executeScript({ target: { tabId: tab.id }, files: ['page.js'] }, () => {
if (timedOut) {
console.error('Timed out too early while waiting for chrome.scripting.executeScript. Try increasing the timeout.');
chrome.runtime.onMessage.removeListener(listener);
}
else {
loaded = true;
progress(0);
initiateCapture(tab, fullPage, () => {
chrome.runtime.onMessage.removeListener(listener);
callback(getBlobs(screenshots), sizes);
});
}
});
window.setTimeout(() => {
if (!loaded) {
timedOut = true;
errback('execute timeout');
}
}, timeout);
};
const captureToFiles = (tab, filename, format, quality, fullPage, callback, errback, progress) => {
captureToBlobs(tab, format, quality, fullPage, (blobs, sizes) => {
let i = 0,
len = blobs.length,
filenames = [];
(function doNext() {
saveBlob(blobs[i], filename, (filename) => {
i++;
filenames.push(filename);
i >= len ? callback(blobs, sizes, format, filenames, settings) : doNext();
}, errback);
})();
}, errback, progress);
};