Adding APIs to the navigator object

Starting with Gecko 9.0 (Firefox 9.0 / Thunderbird 9.0 / SeaMonkey 2.6), you can easily add new APIs to the window.navigator object by using the Category Manager. Simply add an entry to the "JavaScript-navigator-property" category.

The object must be implemented as an XPCOM component. Each method below of adding new objects to the navigator object requires that the new object is a registered XPCOM component. You can read about creating and registering XPCOM components in JavaScript.

Programmatically adding an object to navigator

var categoryManager = Components.classes["@mozilla.org/categorymanager;1"]
                      .getService(Components.interfaces.nsICategoryManager);
categoryManager.addCategoryEntry("JavaScript-navigator-property", "myApi",
                      MY_CONTRACT_ID, false, true);

This adds a new object, myApi, to the window.navigator object. The newly added object is a reference to the component specified by the contract ID MY_CONTRACT_ID. You can learn more about Contract IDs are unique text identifiers for XPCOM components.

Using a manifest to add an object to navigator

You can also add an object to the window.navigator object by using the chrome manifest of an add-on:

component {ffffffff-ffff-ffff-ffff-ffffffffffff} MyComponent.js
contract @mozilla.org/mycomponent;1 {ffffffff-ffff-ffff-ffff-ffffffffffff}
category JavaScript-navigator-property myComponent @mozilla.org/mycomponent;1

Generate a GUID and replace the "ffff" sections in both the component and contract lines with your GUID.

This adds a new API, myComponent, to the navigator object, which you can then access as navigator.myComponent.

Real-world example

You can see an example of how this is used in Firefox by taking a look at how the mozApps API is implemented:

See also

Document Tags and Contributors

 Contributors to this page: Dietrich, sampenrose, Sheppy, kscarfone, amccreight
 Last updated by: Dietrich,