Messaging API

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.

opera.extension.bgProcess
A reference to the window object of the background process.
opera.extension.onconnect
An event listener invoked when an injected script, popup, or options environment is created that enables communication.
opera.extension.ondisconnect
An event listener invoked when an injected script, popup or options environment is destroyed and communication is disabled.
opera.extension.onmessage
An event listener invoked when a message is received from an injected script, popup or preference page.
opera.extension.addEventListener()
A method for listening for events being dispatched.
opera.extension.removeEventListener()
A method that removes a listener from receiving an event.
opera.extension.broadcastMessage()
A method used to broadcast data to all connected injected script and popup environments associated with the extension.

Overview

Communicating between different parts of an extension is done by using either the background process or the messaging API.

The background script and injected scripts are isolated from each other and must therefore use the messaging API to communicate. For more information read our article on Opera extensions: messaging

Other parts of an extension, e.g. popup windows and preference pages, can also access the background script by using the messaging API but it's much easier to use the bgProcess object—a common object that refers to the background script's window object.

The background process, as the name suggests, is a process constantly running in the background for the lifetime of an extension. It is responsible for browser UI elements (for example the toolbar button and popup windows) and browser actions (opening and closing a tab, etc.).

Examples

The two code snippets below send a message, in this case a URL, between an injected script and the background process

//
// The injected script ('/includes/injectedScript.js')
//

window.addEventListener('DOMContentLoaded', function() {
  // send message to background script telling it what URL we are visiting
  opera.extension.postMessage(document.URL);
}, false);

The injected script (above) sends its message using opera.extension.postMessage(message) which is received by the background process using the event handler opera.extension.onmessage

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

// Listen for injected script messages (i.e. for image tags)
opera.extension.onmessage = function(event) {
  // got the URL from the injected script
  url = event.data;
};

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

Comments

  • photo

    kad0t

    Friday, October 12, 2012

    Need way to know tab/tabId from message event.
  • photo

    Manuel Andreas Kuhn

    Tuesday, October 16, 2012

    +1 for the request from kad0t
  • photo

    Gerald

    Friday, December 14, 2012

    +1 for the request from kad0t
No new comments accepted.