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
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
/* globals communication, shot, main, auth, catcher, analytics, buildSettings, blobConverters, thumbnailGenerator */
"use strict";
this.takeshot = (function() {
const exports = {};
const Shot = shot.AbstractShot;
const { sendEvent, incrementCount } = analytics;
const MAX_CANVAS_DIMENSION = 32767;
communication.register("screenshotPage", (sender, selectedPos, isFullPage, devicePixelRatio) => {
return screenshotPage(selectedPos, isFullPage, devicePixelRatio);
});
function screenshotPage(pos, isFullPage, devicePixelRatio) {
pos.width = Math.min(pos.right - pos.left, MAX_CANVAS_DIMENSION);
pos.height = Math.min(pos.bottom - pos.top, MAX_CANVAS_DIMENSION);
// If we are printing the full page or a truncated full page,
// we must pass in this rectangle to preview the entire image
let options = {format: "png"};
if (isFullPage) {
let rectangle = {
x: 0,
y: 0,
width: pos.width,
height: pos.height,
}
options.rect = rectangle;
// To avoid creating extremely large images (which causes
// performance problems), we set the scale to 1.
devicePixelRatio = options.scale = 1;
} else {
let rectangle = {
x: pos.left,
y: pos.top,
width: pos.width,
height: pos.height,
}
options.rect = rectangle
}
return catcher.watchPromise(browser.tabs.captureTab(
null,
options,
).then((dataUrl) => {
const image = new Image();
image.src = dataUrl;
return new Promise((resolve, reject) => {
image.onload = catcher.watchFunction(() => {
const xScale = devicePixelRatio;
const yScale = devicePixelRatio;
const canvas = document.createElement("canvas");
canvas.height = pos.height * yScale;
canvas.width = pos.width * xScale;
const context = canvas.getContext("2d");
context.drawImage(
image,
0, 0,
pos.width * xScale, pos.height * yScale,
0, 0,
pos.width * xScale, pos.height * yScale
);
const result = canvas.toDataURL();
resolve(result);
});
});
}));
}
/** Combines two buffers or Uint8Array's */
function concatBuffers(buffer1, buffer2) {
const tmp = new Uint8Array(buffer1.byteLength + buffer2.byteLength);
tmp.set(new Uint8Array(buffer1), 0);
tmp.set(new Uint8Array(buffer2), buffer1.byteLength);
return tmp.buffer;
}
/** Creates a multipart TypedArray, given {name: value} fields
and a files array in the format of
[{fieldName: "NAME", filename: "NAME.png", blob: fileBlob}, {...}, ...]
Returns {body, "content-type"}
*/
function createMultipart(fields, files) {
const boundary = "---------------------------ScreenshotBoundary" + Date.now();
let body = [];
for (const name in fields) {
body.push("--" + boundary);
body.push(`Content-Disposition: form-data; name="${name}"`);
body.push("");
body.push(fields[name]);
}
body.push("");
body = body.join("\r\n");
const enc = new TextEncoder("utf-8");
body = enc.encode(body).buffer;
const blobToArrayPromises = files.map(f => {
return blobConverters.blobToArray(f.blob);
});
return Promise.all(blobToArrayPromises).then(buffers => {
for (let i = 0; i < buffers.length; i++) {
let filePart = [];
filePart.push("--" + boundary);
filePart.push(`Content-Disposition: form-data; name="${files[i].fieldName}"; filename="${files[i].filename}"`);
filePart.push(`Content-Type: ${files[i].blob.type}`);
filePart.push("");
filePart.push("");
filePart = filePart.join("\r\n");
filePart = concatBuffers(enc.encode(filePart).buffer, buffers[i]);
body = concatBuffers(body, filePart);
body = concatBuffers(body, enc.encode("\r\n").buffer);
}
let tail = `\r\n--${boundary}--`;
tail = enc.encode(tail);
body = concatBuffers(body, tail.buffer);
return {
"content-type": `multipart/form-data; boundary=${boundary}`,
body,
};
});
}
function uploadShot(shot, blob, thumbnail) {
let headers;
return auth.authHeaders().then((_headers) => {
headers = _headers;
if (blob) {
const files = [ {fieldName: "blob", filename: "screenshot.png", blob} ];
if (thumbnail) {
files.push({fieldName: "thumbnail", filename: "thumbnail.png", blob: thumbnail});
}
return createMultipart(
{shot: JSON.stringify(shot)},
files
);
}
return {
"content-type": "application/json",
body: JSON.stringify(shot),
};
}).then((submission) => {
headers["content-type"] = submission["content-type"];
sendEvent("upload", "started", {eventValue: Math.floor(submission.body.length / 1000)});
return fetch(shot.jsonUrl, {
method: "PUT",
mode: "cors",
headers,
body: submission.body,
});
}).then((resp) => {
if (!resp.ok) {
sendEvent("upload-failed", `status-${resp.status}`);
const exc = new Error(`Response failed with status ${resp.status}`);
exc.popupMessage = "REQUEST_ERROR";
throw exc;
} else {
sendEvent("upload", "success");
}
}, (error) => {
// FIXME: I'm not sure what exceptions we can expect
sendEvent("upload-failed", "connection");
error.popupMessage = "CONNECTION_ERROR";
throw error;
});
}
return exports;
})();
|