addon-sdk/source/lib/sdk/content/thumbnail.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     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';
     6 module.metadata = {
     7   'stability': 'unstable'
     8 };
    10 const { Cc, Ci, Cu } = require('chrome');
    11 const AppShellService = Cc['@mozilla.org/appshell/appShellService;1'].
    12                         getService(Ci.nsIAppShellService);
    14 const NS = 'http://www.w3.org/1999/xhtml';
    15 const COLOR = 'rgb(255,255,255)';
    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;
    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 };

mercurial