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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论