opera.extension.getScreenshot() and BrowserTab.getScreenshot()

By Opera Software

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.

Description:

Captures a screenshot of a page within a tab.

Parameters:

  • callback: The callback function to execute. The callback function takes an ImageData object as its parameter, provided by the API.

Syntax:

void getScreenshot (<Function> callback)

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);

This article is licensed under a Creative Commons Attribution 3.0 Unported license.

Comments

No new comments accepted.