« Documentation Home

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

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.

Description:

Captures a screenshot of a page within a tab.

Parameters:

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