Developers › Dev.Opera

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

May 16, 2012 · by Opera Software

This functionality is currently in production, and only available to test in an experimental build of Opera desktop. To download the experimental build, see Desktop build with new extension APIs: Screenshot, Resource Loader and URL Filter.

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

About this article

Published inDev.Opera
DateMay 16, 2012
AuthorOpera Software
Tagsextensions API, screenshot
OriginalInternet Archive