This documentation relates to Opera's now deprecated .oex Extension API framework for Opera versions <= 12.15 and also provided by our OEX2NEX shim library.
For the latest Opera Extensions API documentation for Opera versions > 12.15 please consult the latest Opera Extensions API documentation online.
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:
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.
<!--
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);
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('2DContext');
ctx.putImageData('ImageData');
theData = aCanvas.toDataURL(type);
callBack(theData);
}
}