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.


The source attribute is a MessagePort to the current BrowserTab object in which the current event was fired. If the event was fired in an injected script this attribute will be null (default).


readonly MessagePort source


In this example, a menu item is added to the context menu for selected text only. When the menu item is clicked, the selected text is sent to an injected script in the current tab. The selected text is then reversed and shown in a popup alert box.

  The configuration file ('config.xml').
<?xml version='1.0' encoding='utf-8'?>
<widget xmlns="">
    <feature name="opera:contextmenus"/>
// The background process (e.g. '/background.js').

if ( {
  var menu =;

  // Create a menu item properties object
  var itemProps = {
    contexts: ['selection'],
    title: 'Reverse text',
    onclick: function(event) {
      // Send the selected text to the originating tab
      event.source.postMessage({query: event.selectionText});

  // Create a menu item with the specified properties
  var item = menu.createItem(itemProps);
  // Add the menu item to the context menu
// An injected script (e.g. '/includes/injected.js').

(function() {
  // Source:
  String.prototype.reverse = function() {return this.split('').reverse().join('');};

  // Listen for a message from the background process
  opera.extension.addEventListener('message', function(event) {
    // Get the selected text from the event's data object
  }, false);

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


No new comments accepted.