Skip to content

Instantly share code, notes, and snippets.

@nelix
Last active August 29, 2015 13:59
Show Gist options
  • Save nelix/10700949 to your computer and use it in GitHub Desktop.
Save nelix/10700949 to your computer and use it in GitHub Desktop.
function captureVisibleTab() {
var win = require("window-utils").windowIterator().next(); // the actual firefox chrome (not the browser chrome.. the firefox concept)
var {Cc, Ci} = require("chrome");
// the... active window...
var mainWindow = win.QueryInterface(Ci.nsIInterfaceRequestor)
.getInterface(Ci.nsIWebNavigation)
.QueryInterface(Ci.nsIDocShellTreeItem)
.rootTreeItem
.QueryInterface(Ci.nsIInterfaceRequestor)
.getInterface(Ci.nsIDOMWindow);
var contentWindow = mainWindow.gBrowser.getBrowserForTab(mainWindow.gBrowser.selectedTab).contentWindow;
var contentDocument = contentWindow.document;
var clientInfo = contentDocument.compatMode == "CSS1Compat" ?
contentDocument.documentElement : contentDocument.body;
var backgroundColor = '#000000'; // requestDetails.backgroundColor;
var width = contentWindow.innerWidth;
var height = contentWindow.innerHeight;
var region = {
x: contentWindow.scrollX,
y: contentWindow.scrollY,
width: clientInfo.clientWidth,
height: clientInfo.clientHeight
};
var canvas = contentDocument.createElement('canvas');
canvas.style.maxwidth = region.width + "px";
canvas.style.maxheight = region.height + "px";
canvas.width = region.width;
canvas.height = region.height;
var ctx = canvas.getContext('2d');
ctx.clearRect(region.x, region.y, region.width, region.height);
try {
ctx.drawWindow(contentWindow, region.x, region.y, region.width, region.height, backgroundColor);
} catch(err) {
console.error(JSON.stringify(err)); // firefox is pretty unfriendly to extnesion devs..
return;
} // sorry :(
return canvas.toDataURL();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment