system/events

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.

Unstable

API for working with the application observer service.

Usage

The system/events module provides core (low level) API for working with the application observer service, also known as nsIObserverService. You can find a list of events dispatched by the Firefox codebase here.

var events = require("sdk/system/events");
var { Ci } = require("chrome");

function listener(event) {
  var channel = event.subject.QueryInterface(Ci.nsIHttpChannel);
  channel.setRequestHeader("User-Agent", "MyBrowser/1.0", false);
}

events.on("http-on-modify-request", listener);

Globals

Functions

emit(type, event)

Send an event to observer service

Parameters

type : string
The event type.

event : object
An optional object with data and subject attributes. data refers to a string that you would like to pass through this event. subject should refer to the actual actor/subject of this event (ie: the object emitting the event).

on(type, listener, strong)

Listen to events of a given type

Parameters

type : string
The event type name to watch.

listener : function
Function that will be called when an event is fired, with a single event object as argument. This object has three attributes:

  • type: the event type name
  • subject: the event subject object
  • data: the event data string

strong : boolean
Default is false, a weak reference, which means it can be garbage collected at any time if there are no other references to it. Determines if we should keep a strong or weak reference to the listener method.

once(type, listener, strong)

Listen only once to a particular event type

Parameters

type : string
The event type name to watch.

listener : function
Function that will be called when an event is fired.

strong : boolean
Default is false, a weak reference, which means it can be garbage collected at any time if there are no other references to it. Determines if we should keep a strong or weak reference to the listener method.

off(type, listener)

Stop listening for an event

Parameters

type : string
The event type name to unsubscribe to.

listener : function
The function we registered to listen for events.

Document Tags and Contributors

 Contributors to this page: wbamberg, serv-inc, MoziGuru, grbradt, Hub, jsantell
 Last updated by: wbamberg,