Screenshot API
From Opera 15 onward, Opera 11 & 12’s extension format is no longer supported, and instead, we’ve switched to Chromium’s extension model. Check out our new documentation for developing extensions for Opera 15 and higher and start building your own extensions.
- opera.extension.getScreenshot() (also BrowserTab.getScreenshot())
- Captures a screenshot of a page within a tab.
Overview
The Screenshot API for Opera extensions enables a screenshot to be taken of a page within a tab. Only the visible portion of the page is included in the screenshot, excluding the browser chrome (i.e. no toolbars and scrollbars, etc.).
There is a single method available to create a screenshot — getScreenshot()
— and this method can be called from two contexts:
- An extension's injected script
- An extension's background script
Because of this, possible use cases are either a toolbar button to call the method in the background script, or an in-page UI to call the method in an injected script.
The getScreenshot()
method does not return a value, but rather executes a callback function which is specified as its parameter. The callback function in turn needs its own parameter with an arbitrary name. This parameter is an ImageData
object which can be written to an HTML5 <canvas>
context using the putImageData()
method.
Example
<!--
The configuration file ('config.xml').
-->
<?xml version='1.0' encoding='utf-8'?>
<widget xmlns="http://www.w3.org/ns/widgets">
<feature name="opera:screenshot" required="false"/>
</widget>
//
// An injected script (e.g. includes/injected.js)
//
window.addEventListener('load', function() {
// Check the Screenshot API is available
if (opera.extension.getScreenshot) {
opera.postError('Screenshot API found');
// Callback function to replace page content with the screenshot
function applyScreenshot(imageData) {
opera.postError('Appending screenshot to current page...');
// Create a blank canvas
var canvas = document.createElement('canvas');
canvas.width = imageData.width;
canvas.height = imageData.height;
// Write the screenshot image data to the canvas context
var ctx = canvas.getContext('2d');
ctx.putImageData(imageData, 0, 0);
// Replace the page's content with the canvas
var body = document.body;
body.innerHTML = '';
body.appendChild(canvas);
opera.postError('Snapshot appended to current page.');
}
// Use the API's method to execute the callback function
opera.extension.getScreenshot(applyScreenshot);
} else {
opera.postError('No Screenshot API found');
}
}, false);
Compatibility
Google Chrome extensions have access to a similar API which uses a method named captureVisibleTab()
. If you wish to port a Google Chrome extension to Opera, below is a function you can use to mimic the behaviour of captureVisibleTab()
.
// Note: This assumes that all optional arguments are present
function captureVisibleTab(window, options, callBack) {
if (options.format)
var type = "image/" + options.format;
else
type = "image/jpeg"; //chrome gives jpeg by default
opera.extension.getScreenshot(handleImage);
function handleImage(theImage) {
var myCanvas = document.createElement('canvas');
myCanvas.width = theImage.width;
myCanvas.height = theImage.height;
var ctx = aCanvas.getContext('2D');
ctx.putImageData('theImage');
theData = aCanvas.toDataURL(type);
callBack(theData);
}
}
(Image attribution: Camera designed by Simon Child from The Noun Project)
This article is licensed under a Creative Commons Attribution 3.0 Unported license.
Comments
Michael A. Puls II
Thursday, May 17, 2012
:(
JanGen
Thursday, May 17, 2012
IMHO a much more powerful approach would be if the api could take a screenshot of any element in the DOM.
so you can take screenshots of parts of a page ( how an element is rendered).
Would be helpful for writing extensions that offer help for filling in forms etc.
QuHno
Thursday, May 17, 2012
Idea:
You could scroll one page, capture another screenshot, scroll one page ...
and add up all screenshots in one canvas. May be a good enough scripter can write a working example that can do that and post it here in the comments?
Christoph
Monday, October 8, 2012
for all of you who want to know how you can make a screenshot of the entire page (small version - alternative to adding pages in a canvas together like others suggested already):
http://my.opera.com/christoph142/blog/2012/10/08/taking-a-screenshot-of-the-complete-page-using-operas-new-screensho-3
Zotlan
Tuesday, October 9, 2012
PS: It'd be helpful to add to this article that using <feature name="opera:screenshot" required="false"/> is essential for the screenshot API to work, took me a while to figure that out :shamedface;
Дмитрий
Thursday, November 8, 2012
My crazy dream is that the screenshot will include an overall browser window, not just a webpage. In this case there will be no need any more to use external screenshot programs. Opera itself will be all-sufficient.