« Documentation Home

MenuEvent.source

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:

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

Syntax:

readonly MessagePort source

Example:

In this example, a menu item is added to the context menu for editable elements only. When the menu item is clicked, the browser's user agent string is inserted into the editable element.

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

if (opera.contexts.menu) {
  var menu = opera.contexts.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
  menu.addItem(item);
}
//
// An injected script (e.g. '/includes/injected.js').
//

(function() {
  // Source: http://4umi.com/web/javascript/reverse.php
  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
    alert(event.data.query.reverse());
  }, false);
})();