opera.extension.onmessage

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:

This event listener is invoked when a message is received from an injected script, popup or preferences page. The event's source is a messagePort to the connecting environment.

Example:

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

opera.extension.onmessage = function(event) {
  // Get the message content from the event's data property
  var message = event.data;
};

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

Comments

  • photo

    metude

    Friday, November 2, 2012

    Here's a more description from spadija ( http://my.opera.com/spadija )

    Messaging is just a way to communicate between the parts of an extension.
    The background script, injected scripts, popup, etc. can all listen for
    messages sent to them and send messages to each other.

    // Here I set a function to be called whenever the background script gets
    a message
    opera.extension.onmessage = function(e) {
    // "e" holds information about the message that was sent.
    // for example, "e.source" is a reference to whatever sent the message
    // and "e.data" is the data sent with the message.

    // Here the data is the URL we want the popup to switch to.
    opera.contexts.toolbar[0].popup.href = e.data;
    }

    // In the popup, this sends a message to the background script.
    // The data sent with the message is the new popup URL.
    // When this is run, the background script's "onmessage" function gets
    called,
    // and the "data" property of its first parameter will be
    "http://some-url".
    opera.extension.postMessage('http://some-url');
No new comments accepted.