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

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/addon-sdk/source/lib/sdk/content/thumbnail.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,46 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +'use strict';
     1.8 +
     1.9 +module.metadata = {
    1.10 +  'stability': 'unstable'
    1.11 +};
    1.12 +
    1.13 +const { Cc, Ci, Cu } = require('chrome');
    1.14 +const AppShellService = Cc['@mozilla.org/appshell/appShellService;1'].
    1.15 +                        getService(Ci.nsIAppShellService);
    1.16 +
    1.17 +const NS = 'http://www.w3.org/1999/xhtml';
    1.18 +const COLOR = 'rgb(255,255,255)';
    1.19 +
    1.20 +/**
    1.21 + * Creates canvas element with a thumbnail of the passed window.
    1.22 + * @param {Window} window
    1.23 + * @returns {Element}
    1.24 + */
    1.25 +function getThumbnailCanvasForWindow(window) {
    1.26 +  let aspectRatio = 0.5625; // 16:9
    1.27 +  let thumbnail = AppShellService.hiddenDOMWindow.document
    1.28 +                    .createElementNS(NS, 'canvas');
    1.29 +  thumbnail.mozOpaque = true;
    1.30 +  thumbnail.width = Math.ceil(window.screen.availWidth / 5.75);
    1.31 +  thumbnail.height = Math.round(thumbnail.width * aspectRatio);
    1.32 +  let ctx = thumbnail.getContext('2d');
    1.33 +  let snippetWidth = window.innerWidth * .6;
    1.34 +  let scale = thumbnail.width / snippetWidth;
    1.35 +  ctx.scale(scale, scale);
    1.36 +  ctx.drawWindow(window, window.scrollX, window.scrollY, snippetWidth,
    1.37 +                snippetWidth * aspectRatio, COLOR);
    1.38 +  return thumbnail;
    1.39 +}
    1.40 +exports.getThumbnailCanvasForWindow = getThumbnailCanvasForWindow;
    1.41 +
    1.42 +/**
    1.43 + * Creates Base64 encoded data URI of the thumbnail for the passed window.
    1.44 + * @param {Window} window
    1.45 + * @returns {String}
    1.46 + */
    1.47 +exports.getThumbnailURIForWindow = function getThumbnailURIForWindow(window) {
    1.48 +  return getThumbnailCanvasForWindow(window).toDataURL()
    1.49 +};

mercurial