XPCOM 对象方法无法从 JavaScript 访问
我正在尝试为 Firefox 构建一个扩展。此扩展使用 XPCOM 组件(C++ dll)。我正在编译DLL,编译没问题。
我还成功地构建了一个 JS 代码,该代码实例化了 XPCOM 中的对象:
try {
greenfox;
return true;
} catch( e ) {
alert( e );
return false;
}
返回的对象是这个:
QueryInterface
QueryInterface()
__proto__
[xpconnect wrapped native prototype] { QueryInterface=QueryInterface()}
QueryInterface
QueryInterface()
一切都很好,除了我无法调用应该在我的 XPCOM 组件中的函数。
这是我的 IDL 文件:
[scriptable, uuid(ec8030f7-c20a-464f-9b0e-13a3a9e97384)]
interface nsISample : nsISupports
{
attribute string value;
void writeValue(in string aPrefix);
void poke(in string aValue);
void start();
double stop();
};
当调用start() 函数时,我收到 Javascript 错误:“不是函数”
greenfox.start();
你有什么想法吗?我的 XPCOM 中似乎没有公开任何功能。
I'm trying to build an extension for Firefox. This extension uses an XPCOM component (a C++ dll). I'm compiling the DLL, compilation is OK.
I also succeeded in building a JS code which instanciates the object from XPCOM:
try {
greenfox;
return true;
} catch( e ) {
alert( e );
return false;
}
The object returned is this one:
QueryInterface
QueryInterface()
__proto__
[xpconnect wrapped native prototype] { QueryInterface=QueryInterface()}
QueryInterface
QueryInterface()
Everything is fine, except I can't call the function which are supposed to be in my XPCOM component.
Here is my IDL file:
[scriptable, uuid(ec8030f7-c20a-464f-9b0e-13a3a9e97384)]
interface nsISample : nsISupports
{
attribute string value;
void writeValue(in string aPrefix);
void poke(in string aValue);
void start();
double stop();
};
When callingstart() function, I get the Javascript error: "is not a function"
greenfox.start();
Do you have any idea? It seems like no function is exposed in my XPCOM.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您似乎正在查看一个仅公开 nsISupports 接口的对象。默认情况下,您的接口 (
nsISample
) 不会公开,您必须明确请求它。例如,您可以通过实例化您的组件来实现此目的,如下所示:或者,您也可以在已有的对象上调用 QueryInterface:
一般来说,我不建议使用二进制 XPCOM 组件,原因如下: a href="http://adblockplus.org/blog/binary-xpcom-components-are-dead-js-ctypes-is-the-way-to-go" rel="nofollow noreferrer">此处 ,维护它们需要太多的努力。我宁愿建议编译一个常规的 DLL 并通过 js-ctypes 使用它。 将二进制组件引用到 js-ctypes< /a> 提到如何在附加组件中找到 DLL 以通过 js-ctypes 使用它。
You seem to be looking at an object exposing only the
nsISupports
interface. Your interface (nsISample
) won't get exposed by default, you have to explicitly request it. You can do it for example by instantiating your component like this:Alternatively, you can also call
QueryInterface
on an object you already have:Generally, I wouldn't recommend using a binary XPCOM component for reasons outlined here, maintaining them requires way too much effort. I would rather suggest compiling a regular DLL and using it via js-ctypes. Reference a binary-component to js-ctypes mentions how you would locate a DLL inside your add-on to use it via js-ctypes.
你用你的 uuid 调用 QueryInterface 吗?在使用创建的组件实例之前需要调用它。您的使用情况与此处中的内容相符吗?
如果你不想使用 XPCOM,你可以使用 js-ctypes。
Do you call QueryInterface with your uuid? It is necessary to call it before using the component instance created. Does your usage match what's in here?
If you don't want to deal with XPCOM you can use js-ctypes.