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. 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:132 次

字数:3454

最后编辑:7年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文