Last active
November 28, 2024 12:50
-
-
Save brendandahl/8b4b20bef28c4e86ff207477393f0316 to your computer and use it in GitHub Desktop.
Snapshot a page in Firefox
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Current window | |
var el = window.document.createElementNS("http://www.w3.org/1999/xhtml", "canvas"); | |
el.width = window.innerWidth; | |
el.height = window.innerHeight; | |
var ctx = el.getContext("2d"); | |
SpecialPowers.wrap(ctx).drawWindow(window, 0, 0, window.innerWidth, window.innerHeight, "rgba(0,0,0,0)", | |
ctx.DRAWWINDOW_USE_WIDGET_LAYERS | ctx.DRAWWINDOW_DRAW_VIEW | ctx.DRAWWINDOW_DRAW_CARET); | |
dump("\n" + el.toDataURL() + "\n"); | |
// Parent window in e10s mode | |
function getSnapshot() { | |
function parentProcessSnapshot() { | |
addMessageListener('snapshot', function() { | |
Components.utils.import('resource://gre/modules/Services.jsm'); | |
var topWin = Services.wm.getMostRecentWindow('navigator:browser'); | |
// take the snapshot | |
var canvas = topWin.document.createElementNS("http://www.w3.org/1999/xhtml", "canvas"); | |
canvas.width = topWin.innerWidth; | |
canvas.height = topWin.innerHeight; | |
var ctx = canvas.getContext("2d"); | |
ctx.drawWindow(topWin, 0, 0, topWin.innerWidth, topWin.innerHeight, 'rgb(255,255,255)', ctx.DRAWWINDOW_DRAW_VIEW | ctx.DRAWWINDOW_USE_WIDGET_LAYERS | ctx.DRAWWINDOW_DRAW_CARET); | |
return canvas.toDataURL(); | |
}); | |
} | |
if (typeof getSnapshot.chromeHelper == 'undefined') { | |
// This is the first time getSnapshot is being called; do initialization | |
getSnapshot.chromeHelper = SpecialPowers.loadChromeScript(parentProcessSnapshot); | |
SimpleTest.registerCleanupFunction(function() { getSnapshot.chromeHelper.destroy() }); | |
} | |
return getSnapshot.chromeHelper.sendSyncMessage('snapshot').toString(); | |
} | |
dump(">>> SNAPSHOT: \n"); | |
dump('\n\n>>> ' + getSnapshot() + '\n\n'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment