thumbnail.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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. module.metadata = {
  6. 'stability': 'unstable'
  7. };
  8. const { Cc, Ci, Cu } = require('chrome');
  9. const AppShellService = Cc['@mozilla.org/appshell/appShellService;1'].
  10. getService(Ci.nsIAppShellService);
  11. const NS = 'http://www.w3.org/1999/xhtml';
  12. const COLOR = 'rgb(255,255,255)';
  13. /**
  14. * Creates canvas element with a thumbnail of the passed window.
  15. * @param {Window} window
  16. * @returns {Element}
  17. */
  18. function getThumbnailCanvasForWindow(window) {
  19. let aspectRatio = 0.5625; // 16:9
  20. let thumbnail = AppShellService.hiddenDOMWindow.document
  21. .createElementNS(NS, 'canvas');
  22. thumbnail.mozOpaque = true;
  23. thumbnail.width = Math.ceil(window.screen.availWidth / 5.75);
  24. thumbnail.height = Math.round(thumbnail.width * aspectRatio);
  25. let ctx = thumbnail.getContext('2d');
  26. let snippetWidth = window.innerWidth * .6;
  27. let scale = thumbnail.width / snippetWidth;
  28. ctx.scale(scale, scale);
  29. ctx.drawWindow(window, window.scrollX, window.scrollY, snippetWidth,
  30. snippetWidth * aspectRatio, COLOR);
  31. return thumbnail;
  32. }
  33. exports.getThumbnailCanvasForWindow = getThumbnailCanvasForWindow;
  34. /**
  35. * Creates Base64 encoded data URI of the thumbnail for the passed window.
  36. * @param {Window} window
  37. * @returns {String}
  38. */
  39. exports.getThumbnailURIForWindow = function getThumbnailURIForWindow(window) {
  40. return getThumbnailCanvasForWindow(window).toDataURL()
  41. };