michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: 'use strict'; michael@0: michael@0: module.metadata = { michael@0: 'stability': 'unstable' michael@0: }; michael@0: michael@0: const { Cc, Ci, Cu } = require('chrome'); michael@0: const AppShellService = Cc['@mozilla.org/appshell/appShellService;1']. michael@0: getService(Ci.nsIAppShellService); michael@0: michael@0: const NS = 'http://www.w3.org/1999/xhtml'; michael@0: const COLOR = 'rgb(255,255,255)'; michael@0: michael@0: /** michael@0: * Creates canvas element with a thumbnail of the passed window. michael@0: * @param {Window} window michael@0: * @returns {Element} michael@0: */ michael@0: function getThumbnailCanvasForWindow(window) { michael@0: let aspectRatio = 0.5625; // 16:9 michael@0: let thumbnail = AppShellService.hiddenDOMWindow.document michael@0: .createElementNS(NS, 'canvas'); michael@0: thumbnail.mozOpaque = true; michael@0: thumbnail.width = Math.ceil(window.screen.availWidth / 5.75); michael@0: thumbnail.height = Math.round(thumbnail.width * aspectRatio); michael@0: let ctx = thumbnail.getContext('2d'); michael@0: let snippetWidth = window.innerWidth * .6; michael@0: let scale = thumbnail.width / snippetWidth; michael@0: ctx.scale(scale, scale); michael@0: ctx.drawWindow(window, window.scrollX, window.scrollY, snippetWidth, michael@0: snippetWidth * aspectRatio, COLOR); michael@0: return thumbnail; michael@0: } michael@0: exports.getThumbnailCanvasForWindow = getThumbnailCanvasForWindow; michael@0: michael@0: /** michael@0: * Creates Base64 encoded data URI of the thumbnail for the passed window. michael@0: * @param {Window} window michael@0: * @returns {String} michael@0: */ michael@0: exports.getThumbnailURIForWindow = function getThumbnailURIForWindow(window) { michael@0: return getThumbnailCanvasForWindow(window).toDataURL() michael@0: };