Skip to content

Instantly share code, notes, and snippets.

@brendandahl
Last active November 28, 2024 12:50
Show Gist options
  • Save brendandahl/8b4b20bef28c4e86ff207477393f0316 to your computer and use it in GitHub Desktop.
Save brendandahl/8b4b20bef28c4e86ff207477393f0316 to your computer and use it in GitHub Desktop.
Snapshot a page in Firefox
// 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