在Web页面中将XPCOM组件公开为JavaScript [英] Expose an XPCOM component to javascript in a web page

查看:130
本文介绍了在Web页面中将XPCOM组件公开为JavaScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想写一个XPCOM组件,这个组件在网页中作为一个javascript对象公开。像Google Gears就是我正在寻找的东西。也就是说,在安装Google Gears Firefox扩展之后,任何想要使用它的网页都可以使用javascript对象google.gears。我发现很多关于XPCOM开发的mozilla文档,但是没有公开在网页上运行javascript的组件。这是可能的XPCOM?我需要编写一个Firefox插件而不是扩展名吗?

解决方案

我正在做一个新的API在Firefox 4 - nsiDOMGlobalPropertyInitializer - 它可以让你创建一个JS对象,以便连接到所有窗口的懒惰。这是在Firefox 4中创建新的Web控制台的方式。



您必须在组件中具有以下QI属性:

  QueryInterface:XPCOMUtils.generateQI([Ci.nsIDOMGlobalPropertyInitializer]),

以下是一个完整实现的例子:

https://github.com/daviddahl/domcrypt/blob/master/extension/domcrypt/components/domcrypt.js



基本上,QI到Ci.nsIDOMGlobalPropertyInitializer,然后确保你的清单有一行:

<$ p $ 1



类别JavaScript-global-property crypt @ droplettr.com / domcrypt;请参阅: https://github.com/daviddahl / domcrypt /斑点/主/ E xtension / domcrypt / components / domcrypt.manifest


I'd like to write a XPCOM component that gets exposed as a javascript object in a web page. Something like Google Gears is what I'm seeking. That is, after installing the Google Gears Firefox extension, the javascript object 'google.gears' is available to any web page that wants to use it. I've found lots of mozilla documentation on XPCOM development, but nothing on exposing the component to javascript running in a web page. Is this possible with XPCOM? Do I need to write a Firefox plug-in instead of an extension?

解决方案

I'm doing exactly that with a new API in Firefox 4 - nsiDOMGlobalPropertyInitializer - which lets you create a JS object to attach to all windows lazily. This is the way the new Web Console in Firefox 4 is created.

You have to have the following QI property in your component:

QueryInterface: XPCOMUtils.generateQI([Ci.nsIDOMGlobalPropertyInitializer]),

Here is an example of a full implementation in an extension:

https://github.com/daviddahl/domcrypt/blob/master/extension/domcrypt/components/domcrypt.js

SO basically, QI to Ci.nsIDOMGlobalPropertyInitializer, then make sure your manifest has a line like:

category JavaScript-global-property crypt @droplettr.com/domcrypt;1

see: https://github.com/daviddahl/domcrypt/blob/master/extension/domcrypt/components/domcrypt.manifest

这篇关于在Web页面中将XPCOM组件公开为JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆