Using the SDK, you can do something like this:
const { window: { document } } = require('sdk/addon/window'); const { getTabContentWindow, getActiveTab } = require('sdk/tabs/utils'); const { getMostRecentBrowserWindow } = require('sdk/window/utils'); const canvas = document.createElementNS('http://www.w3.org/1999/xhtml', 'canvas'); document.documentElement.appendChild(canvas); function captureTab(tab=getActiveTab(getMostRecentBrowserWindow())) { let contentWindow = getTabContentWindow(tab); let w = contentWindow.innerWidth; let h = contentWindow.innerHeight; let x = contentWindow.scrollX; let y = contentWindow.scrollY; canvas.width = w; canvas.height = h; let ctx = canvas.getContext('2d'); ctx.drawWindow(contentWindow, x, y, w, h, '#000'); return canvas.toDataURL(); }
It takes only the visible area. By default, it captures the active tab, but you can pass any other tab - since it is designed as a low-level API, it requires the native tab, but not the SDK tab. You can add a module and export only the captureTab function.
Edit: e10s version
The code is currently not compatible with Firefox with available e10s, as Ian Bicking noted in a comment. An easy way to solve this problem is to create a temporary canvas in the same document and the content process that we want to capture the screenshot:
const { getTabContentWindow, getActiveTab } = require('sdk/tabs/utils'); const { getMostRecentBrowserWindow } = require('sdk/window/utils'); function captureTab(tab=getActiveTab(getMostRecentBrowserWindow())) { let contentWindow = getTabContentWindow(tab); let { document } = contentWindow; let w = contentWindow.innerWidth; let h = contentWindow.innerHeight; let x = contentWindow.scrollX; let y = contentWindow.scrollY; let canvas = document.createElementNS('http://www.w3.org/1999/xhtml', 'canvas'); canvas.width = w; canvas.height = h; let ctx = canvas.getContext('2d'); ctx.drawWindow(contentWindow, x, y, w, h, '#000'); let dataURL = canvas.toDataURL(); canvas = null; return dataURL; }
This works in both e10s and non-e10s FF versions; a drawback compared to the previous one creates a canvas every time we want to take a screenshot, but I think that is acceptable.
Zer0
source share