opera.extension.addEventListener()

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.

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:

  • type: The type of event to listen to.
  • listener: The function that will be called
  • useCapture: If true, the event listener is only added for the capture phase and target

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

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

Comments

No new comments accepted.