« Documentation Home

Messaging

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.

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