« Documentation Home

opera.extension.postMessage()

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:

This method is used to post a message to the BackgroundProcess of the extension.

Parameters:

Syntax:

void postMessage (<object> data, <array> ports)

Example:

//
// 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
  },
  badge: {}
};

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

// Check for incoming messages
opera.extension.onmessage = function(event) {
  button.badge.textContent = event.data;
};
//
// The popup script (e.g. '/popup.js'). 
//

// Set an integer and send it to the background process
var num = 42;
opera.extension.postMessage(num);
document.write('Message sent: ' + num);