self

Add-ons using the techniques described in this document are considered a legacy technology in Firefox. 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.

From Firefox 53 onwards, no new legacy add-ons will be accepted on addons.mozilla.org (AMO).

From Firefox 57 onwards, WebExtensions will be the only supported extension type, and Firefox will not load other types.

Even before Firefox 57, changes coming up in the Firefox platform will break many legacy extensions. These changes include multiprocess Firefox (e10s), sandboxing, and multiple content processes. Legacy extensions that are affected by these changes should migrate to WebExtensions if they can. See the "Compatibility Milestones" document for more.

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

Stable

Access data that is bundled with the add-on, and add-on metadata.

Note that the self module is completely different from the global self object accessible to content scripts, which is used by a content script to communicate with the add-on code.

Globals

Properties

uri

This property represents an add-on associated unique URI string. This URI can be used for APIs which require a valid URI string, such as the passwords module.

id

This property is a printable string that is unique for each add-on. It comes from the id property set in the package.json file in the main package (i.e. the package in which you run jpm xpi). While not generally of use to add-on code directly, it can be used by internal API code to index local storage and other resources that are associated with a particular add-on.

name

This property contains the add-on's short name. It comes from the name property in the main package's package.json file.

version

This property contains the add-on's version string. It comes from the version property set in the package.json file in the main package.

loadReason

This property contains of the following strings describing the reason your add-on was loaded:

install
enable
startup
upgrade
downgrade

isPrivateBrowsingSupported

This property indicates whether or not the add-on supports private browsing. It comes from the private-browsing key in the add-on's package.json file.

data

The data object is used to access data that was bundled with the add-on. This data lives in the add-on's data/ directory, immediately below the package.json file. All files in this directory will be copied into the XPI and made available through the data object.

The Package Specification article explains the package.json file.

Methods

data.load(name)

The data.load() method returns the contents of an embedded data file, as a string. It is most useful for data that will be modified or parsed in some way, such as JSON, XML, plain text, or perhaps an HTML template. For data that can be displayed directly in a content frame, use data.url().

Parameters

name : string
The filename to be read, relative to the package's data directory. Each package that uses the self module will see its own data directory.

Returns

string : the file contents.

data.url(name)

The data.url() method returns a resource:// url that points at an embedded data file. It is most useful for data that can be displayed directly in a content frame. The url can be passed to a content frame constructor, such as the Panel:

var self = require("sdk/self");
var myPanel = require("sdk/panel").Panel({
  contentURL: self.data.url("myFile.html")
});
myPanel.show();

From Firefox 34, you can use "./myFile.html" as an alias for self.data.url("myFile.html"). So you can rewrite the above code like this:

var myPanel = require("sdk/panel").Panel({
  contentURL: "./myFile.html"
});
myPanel.show();
Parameters

name : string
The filename to be read, relative to the package's data directory. Each package that uses the self module will see its own data directory.

Returns

String : resource:// URL pointing to the given location under data.

Document Tags and Contributors

Tags: 
 Contributors to this page: wbamberg, Makyen, maybe
 Last updated by: wbamberg,