|
1 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
2 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
4 'use strict'; |
|
5 |
|
6 module.metadata = { |
|
7 'stability': 'unstable' |
|
8 }; |
|
9 |
|
10 const { Cc, Ci, Cu } = require('chrome'); |
|
11 const AppShellService = Cc['@mozilla.org/appshell/appShellService;1']. |
|
12 getService(Ci.nsIAppShellService); |
|
13 |
|
14 const NS = 'http://www.w3.org/1999/xhtml'; |
|
15 const COLOR = 'rgb(255,255,255)'; |
|
16 |
|
17 /** |
|
18 * Creates canvas element with a thumbnail of the passed window. |
|
19 * @param {Window} window |
|
20 * @returns {Element} |
|
21 */ |
|
22 function getThumbnailCanvasForWindow(window) { |
|
23 let aspectRatio = 0.5625; // 16:9 |
|
24 let thumbnail = AppShellService.hiddenDOMWindow.document |
|
25 .createElementNS(NS, 'canvas'); |
|
26 thumbnail.mozOpaque = true; |
|
27 thumbnail.width = Math.ceil(window.screen.availWidth / 5.75); |
|
28 thumbnail.height = Math.round(thumbnail.width * aspectRatio); |
|
29 let ctx = thumbnail.getContext('2d'); |
|
30 let snippetWidth = window.innerWidth * .6; |
|
31 let scale = thumbnail.width / snippetWidth; |
|
32 ctx.scale(scale, scale); |
|
33 ctx.drawWindow(window, window.scrollX, window.scrollY, snippetWidth, |
|
34 snippetWidth * aspectRatio, COLOR); |
|
35 return thumbnail; |
|
36 } |
|
37 exports.getThumbnailCanvasForWindow = getThumbnailCanvasForWindow; |
|
38 |
|
39 /** |
|
40 * Creates Base64 encoded data URI of the thumbnail for the passed window. |
|
41 * @param {Window} window |
|
42 * @returns {String} |
|
43 */ |
|
44 exports.getThumbnailURIForWindow = function getThumbnailURIForWindow(window) { |
|
45 return getThumbnailCanvasForWindow(window).toDataURL() |
|
46 }; |