« Documentation Home

opera.extension.addEventListener()

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:

Registers a listener for an event specific to the popup window. The listener needs to use the standard EventListener interface - a function with an Event object as its first argument (e.g., var myListener = function(event){alert(event.type)}.

Parameters:

Syntax:

void addEventListener (<DOMString> type, <EventListener> listener, UseCapture)

Example:

Listen for an incoming message from the background process. The source event is a messagePort to the connecting environment.

//
// The background process (e.g. '/background.js'). 
//

// Set options for the button
var UIItemProperties = {
  disabled: false,
  title: 'Opera Extension',
  icon: 'images/icon_18.png',
  popup: {
    href: 'popup.html',
    width: 500,
    height: 400
  },
  onclick: function() {
    opera.extension.broadcastMessage();
  }
};

// Create the button and add it to the toolbar
var button = opera.contexts.toolbar.createItem(UIItemProperties);
opera.contexts.toolbar.addItem(button);
//
// The popup script (e.g. '/popup.js'). 
//

// Note the "on" is removed from "onmessage" here
opera.extension.addEventListener('message', function(event) {
  document.write('Message received: ' + event.data);
}, false);

Note that this event listener can also be written using onmessage, as below.

// Create the button and add it to the toolbar
var button = opera.contexts.toolbar.createItem(UIItemProperties);
opera.contexts.toolbar.addItem(button);

//
// The popup script (e.g. '/popup.js'). 
//

opera.extension.onmessage = function(event) {
  document.write('Message received: ' + event.data); 
};