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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
|
/* 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/. */
import { getFilename } from "chrome://browser/content/screenshots/fileHelpers.mjs";
const lazy = {};
ChromeUtils.defineESModuleGetters(lazy, {
Downloads: "resource://gre/modules/Downloads.sys.mjs",
FileUtils: "resource://gre/modules/FileUtils.sys.mjs",
PrivateBrowsingUtils: "resource://gre/modules/PrivateBrowsingUtils.sys.mjs",
});
const PanelPosition = "bottomright topright";
const PanelOffsetX = -33;
const PanelOffsetY = -8;
export class ScreenshotsComponentParent extends JSWindowActorParent {
async receiveMessage(message) {
let browser = message.target.browsingContext.topFrameElement;
switch (message.name) {
case "Screenshots:CancelScreenshot":
await ScreenshotsUtils.closePanel(browser);
break;
case "Screenshots:CopyScreenshot":
await ScreenshotsUtils.closePanel(browser);
let copyBox = message.data;
ScreenshotsUtils.copyScreenshotFromRegion(copyBox, browser);
break;
case "Screenshots:DownloadScreenshot":
await ScreenshotsUtils.closePanel(browser);
let { title, downloadBox } = message.data;
ScreenshotsUtils.downloadScreenshotFromRegion(
title,
downloadBox,
browser
);
break;
case "Screenshots:ShowPanel":
ScreenshotsUtils.createOrDisplayButtons(browser);
break;
case "Screenshots:HidePanel":
ScreenshotsUtils.closePanel(browser);
break;
}
}
didDestroy() {
// When restoring a crashed tab the browser is null
let browser = this.browsingContext.topFrameElement;
if (browser) {
ScreenshotsUtils.closePanel(browser);
}
}
}
export var ScreenshotsUtils = {
initialized: false,
initialize() {
if (!this.initialized) {
if (
!Services.prefs.getBoolPref(
"screenshots.browser.component.enabled",
false
)
) {
return;
}
Services.obs.addObserver(this, "menuitem-screenshot");
Services.obs.addObserver(this, "screenshots-take-screenshot");
this.initialized = true;
if (Cu.isInAutomation) {
Services.obs.notifyObservers(null, "screenshots-component-initialized");
}
}
},
uninitialize() {
if (this.initialized) {
Services.obs.removeObserver(this, "menuitem-screenshot");
Services.obs.removeObserver(this, "screenshots-take-screenshot");
this.initialized = false;
}
},
handleEvent(event) {
if (event.type === "keydown" && event.key === "Escape") {
this.closePanel(event.view.gBrowser.selectedBrowser, true);
}
},
observe(subj, topic, data) {
let { gBrowser } = subj;
let browser = gBrowser.selectedBrowser;
switch (topic) {
case "menuitem-screenshot":
let success = this.closeDialogBox(browser);
if (!success || data === "retry") {
// only toggle the buttons if no dialog box is found because
// if dialog box is found then the buttons are hidden and we return early
// else no dialog box is found and we need to toggle the buttons
// or if retry because the dialog box was closed and we need to show the panel
this.togglePanelAndOverlay(browser);
}
break;
case "screenshots-take-screenshot":
// need to close the preview because screenshot was taken
this.closePanel(browser, true);
// init UI as a tab dialog box
let dialogBox = gBrowser.getTabDialogBox(browser);
let { dialog } = dialogBox.open(
`chrome://browser/content/screenshots/screenshots.html?browsingContextId=${browser.browsingContext.id}`,
{
features: "resizable=no",
sizeTo: "available",
allowDuplicateDialogs: false,
}
);
this.doScreenshot(browser, dialog, data);
}
return null;
},
/**
* Notify screenshots when screenshot command is used.
* @param window The current window the screenshot command was used.
* @param type The type of screenshot taken. Used for telemetry.
*/
notify(window, type) {
if (Services.prefs.getBoolPref("screenshots.browser.component.enabled")) {
Services.obs.notifyObservers(
window.event.currentTarget.ownerGlobal,
"menuitem-screenshot"
);
} else {
Services.obs.notifyObservers(null, "menuitem-screenshot-extension", type);
}
},
/**
* Creates and returns a Screenshots actor.
* @param browser The current browser.
* @returns JSWindowActor The screenshot actor.
*/
getActor(browser) {
let actor = browser.browsingContext.currentWindowGlobal.getActor(
"ScreenshotsComponent"
);
return actor;
},
/**
* Open the panel buttons and call child actor to open the overlay
* @param browser The current browser
*/
openPanel(browser) {
let actor = this.getActor(browser);
actor.sendQuery("Screenshots:ShowOverlay");
this.createOrDisplayButtons(browser);
},
/**
* Close the panel and call child actor to close the overlay
* @param browser The current browser
* @param {bool} closeOverlay Whether or not to
* send a message to the child to close the overly.
* Defaults to false. Will be false when called from didDestroy.
*/
async closePanel(browser, closeOverlay = false) {
let buttonsPanel = browser.ownerDocument.querySelector(
"#screenshotsPagePanel"
);
if (buttonsPanel && buttonsPanel.state !== "closed") {
buttonsPanel.hidePopup();
}
buttonsPanel?.ownerDocument.removeEventListener("keydown", this);
if (closeOverlay) {
let actor = this.getActor(browser);
await actor.sendQuery("Screenshots:HideOverlay");
}
},
/**
* If the buttons panel exists and is open we will hide both the panel
* and the overlay. If the overlay is showing, we will hide the overlay.
* Otherwise create or display the buttons.
* @param browser The current browser.
*/
async togglePanelAndOverlay(browser) {
let buttonsPanel = browser.ownerDocument.querySelector(
"#screenshotsPagePanel"
);
let isOverlayShowing = await this.getActor(browser).sendQuery(
"Screenshots:isOverlayShowing"
);
if (buttonsPanel && (isOverlayShowing || buttonsPanel.state !== "closed")) {
buttonsPanel.hidePopup();
let actor = this.getActor(browser);
return actor.sendQuery("Screenshots:HideOverlay");
}
let actor = this.getActor(browser);
actor.sendQuery("Screenshots:ShowOverlay");
return this.createOrDisplayButtons(browser);
},
/**
* Gets the screenshots dialog box
* @param browser The selected browser
* @returns Screenshots dialog box if it exists otherwise null
*/
getDialog(browser) {
let currTabDialogBox = browser.tabDialogBox;
let browserContextId = browser.browsingContext.id;
if (currTabDialogBox) {
currTabDialogBox.getTabDialogManager();
let manager = currTabDialogBox.getTabDialogManager();
let dialogs = manager.hasDialogs && manager.dialogs;
if (dialogs.length) {
for (let dialog of dialogs) {
if (
dialog._openedURL.endsWith(
`browsingContextId=${browserContextId}`
) &&
dialog._openedURL.includes("screenshots.html")
) {
return dialog;
}
}
}
}
return null;
},
/**
* Closes the dialog box it it exists
* @param browser The selected browser
*/
closeDialogBox(browser) {
let dialog = this.getDialog(browser);
if (dialog) {
dialog.close();
return true;
}
return false;
},
/**
* If the buttons panel does not exist then we will replace the buttons
* panel template with the buttons panel then open the buttons panel and
* show the screenshots overaly.
* @param browser The current browser.
*/
createOrDisplayButtons(browser) {
let doc = browser.ownerDocument;
let buttonsPanel = doc.querySelector("#screenshotsPagePanel");
if (!buttonsPanel) {
let template = doc.querySelector("#screenshotsPagePanelTemplate");
let clone = template.content.cloneNode(true);
template.replaceWith(clone);
buttonsPanel = doc.querySelector("#screenshotsPagePanel");
}
buttonsPanel.ownerDocument.addEventListener("keydown", this);
let anchor = doc.querySelector("#navigator-toolbox");
buttonsPanel.openPopup(anchor, PanelPosition, PanelOffsetX, PanelOffsetY);
},
/**
* Gets the full page bounds from the screenshots child actor.
* @param browser The current browser.
* @returns { object }
* Contains the full page bounds from the screenshots child actor.
*/
fetchFullPageBounds(browser) {
let actor = this.getActor(browser);
return actor.sendQuery("Screenshots:getFullPageBounds");
},
/**
* Gets the visible bounds from the screenshots child actor.
* @param browser The current browser.
* @returns { object }
* Contains the visible bounds from the screenshots child actor.
*/
fetchVisibleBounds(browser) {
let actor = this.getActor(browser);
return actor.sendQuery("Screenshots:getVisibleBounds");
},
/**
* Add screenshot-ui to the dialog box and then take the screenshot
* @param browser The current browser.
* @param dialog The dialog box to show the screenshot preview.
* @param type The type of screenshot taken.
*/
async doScreenshot(browser, dialog, type) {
await dialog._dialogReady;
let screenshotsUI = dialog._frame.contentDocument.createElement(
"screenshots-ui"
);
dialog._frame.contentDocument.body.appendChild(screenshotsUI);
let rect;
if (type === "full-page") {
rect = await this.fetchFullPageBounds(browser);
} else {
rect = await this.fetchVisibleBounds(browser);
}
return this.takeScreenshot(browser, dialog, rect);
},
/**
* Take the screenshot and add the image to the dialog box
* @param browser The current browser.
* @param dialog The dialog box to show the screenshot preview.
* @param rect DOMRect containing bounds of the screenshot.
*/
async takeScreenshot(browser, dialog, rect) {
let { canvas, snapshot } = await this.createCanvas(rect, browser);
let newImg = dialog._frame.contentDocument.createElement("img");
let url = canvas.toDataURL();
newImg.id = "placeholder-image";
newImg.src = url;
dialog._frame.contentDocument
.getElementById("preview-image-div")
.appendChild(newImg);
if (Cu.isInAutomation) {
Services.obs.notifyObservers(null, "screenshots-preview-ready");
}
snapshot.close();
},
/**
* Creates a canvas and draws a snapshot of the screenshot on the canvas
* @param box The bounds of screenshots
* @param browser The current browser
* @returns The canvas and snapshot in an object
*/
async createCanvas(box, browser) {
let rect = new DOMRect(box.x1, box.y1, box.width, box.height);
let { devicePixelRatio } = box;
let browsingContext = BrowsingContext.get(browser.browsingContext.id);
let snapshot = await browsingContext.currentWindowGlobal.drawSnapshot(
rect,
devicePixelRatio,
"rgb(255,255,255)"
);
let canvas = browser.ownerDocument.createElementNS(
"http://www.w3.org/1999/xhtml",
"html:canvas"
);
let context = canvas.getContext("2d");
canvas.width = snapshot.width;
canvas.height = snapshot.height;
context.drawImage(snapshot, 0, 0);
return { canvas, snapshot };
},
/**
* Copy the screenshot
* @param region The bounds of the screenshots
* @param browser The current browser
*/
async copyScreenshotFromRegion(region, browser) {
let { canvas, snapshot } = await this.createCanvas(region, browser);
let url = canvas.toDataURL();
this.copyScreenshot(url, browser);
snapshot.close();
},
/**
* Copy the image to the clipboard
* @param dataUrl The image data
*/
copyScreenshot(dataUrl) {
// Guard against missing image data.
if (!dataUrl) {
return;
}
const imageTools = Cc["@mozilla.org/image/tools;1"].getService(
Ci.imgITools
);
const base64Data = dataUrl.replace("data:image/png;base64,", "");
const image = atob(base64Data);
const imgDecoded = imageTools.decodeImageFromBuffer(
image,
image.length,
"image/png"
);
const transferable = Cc[
"@mozilla.org/widget/transferable;1"
].createInstance(Ci.nsITransferable);
transferable.init(null);
transferable.addDataFlavor("image/png");
transferable.setTransferData("image/png", imgDecoded);
Services.clipboard.setData(
transferable,
null,
Services.clipboard.kGlobalClipboard
);
},
/**
* Download the screenshot
* @param title The title of the current page
* @param box The bounds of the screenshot
* @param browser The current browser
*/
async downloadScreenshotFromRegion(title, box, browser) {
let { canvas, snapshot } = await this.createCanvas(box, browser);
let dataUrl = canvas.toDataURL();
await this.downloadScreenshot(title, dataUrl, browser);
snapshot.close();
},
/**
* Download the screenshot
* @param title The title of the current page or null and getFilename will get the title
* @param dataUrl The image data
* @param browser The current browser
*/
async downloadScreenshot(title, dataUrl, browser) {
// Guard against missing image data.
if (!dataUrl) {
return;
}
let filename = await getFilename(title, browser);
const targetFile = new lazy.FileUtils.File(filename);
// Create download and track its progress.
try {
const download = await lazy.Downloads.createDownload({
source: dataUrl,
target: targetFile,
});
let isPrivate = lazy.PrivateBrowsingUtils.isWindowPrivate(
browser.ownerGlobal
);
const list = await lazy.Downloads.getList(
isPrivate ? lazy.Downloads.PRIVATE : lazy.Downloads.PUBLIC
);
// add the download to the download list in the Downloads list in the Browser UI
list.add(download);
// Await successful completion of the save via the download manager
await download.start();
} catch (ex) {}
},
};
|