self

We are planning to deprecate the use by Firefox add-ons of the techniques described in this document.

Don't use these techniques to develop new add-ons. Use WebExtensions instead.

If you maintain an add-on which uses the techniques described here, consider migrating it to use WebExtensions instead.

Add-ons developed using these techniques might not work with multiprocess Firefox (e10s), which is already the default in Firefox Nightly and Firefox Developer Edition, and will soon be the default in Beta and Release versions of Firefox. We have documentation on making your add-ons multiprocess-compatible, but it will be more future-proof for you to migrate to WebExtensions.

A wiki page containing resources, migration paths, office hours, and more, is available to help developers transition to the new technologies.

This article documents the self object that is available as a global in content scripts. self provides:

  • access to the options object
  • access to the port object
  • access to a mostly deprecated messaging API

For an overview of content scripts, see the main article.

Note that the self object in content scripts is completely different from the self module, which provides an API for an add-on to access its data files and ID.

self

Properties

options

Content-scripting APIs such as tab.attach(), page-mod, and page-worker let you pass read-only data to the content script as a JSON object via the contentScriptOptions option. If you do this, the data is available to the content script in the options property of self:

// main.js

const tabs = require("sdk/tabs");

tabs.open({
  url: "./page.html",
  onReady: function(tab) {
    tab.attach({
      contentScriptFile: "./content-script.js",
      contentScriptOptions: {
        a: "blah"
      }
    });
  }
});
// content-script.js

alert(self.options.a);

port

You can use port to receive messages from, and send messages to, the main add-on code. See the documentation for port.

Methods

The self object has four methods, which enable the content script to send messages to, and receive messages from, the main add-on code.

This is an older API than the API provided by port, and for most purposes the port API is a better choice. The exception is the context-menu module, which still uses postMessage.

postMessage()

Send a message from a content script to a listener in the main add-on code:

self.postMessage(contentScriptMessage);

This takes a single parameter, the message payload, which may be any JSON-serializable value.

on()

Start listening to messages from the main add-on code:

self.on("message", function(addonMessage) {
  // Handle the message
});

This takes two parameters: the name of the event, and the handler function. The handler function is passed the message payload.

once()

Exactly like on(), but stop listening after the first message is received.

removeListener()

Remove a listener to an event. This takes two parameters: the name of the event to stop listening to, and the listener function to remove.

Document Tags and Contributors

 Contributors to this page: wbamberg, erxin
 Last updated by: wbamberg,