JavaScript无法访问XPCOM对象方法

时间:2012-01-30 20:20:18

标签: c++ firefox firefox-addon xpcom

我正在尝试为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中没有暴露任何功能。

2 个答案:

答案 0 :(得分:1)

您似乎在查看仅显示nsISupports接口的对象。默认情况下,您的界面(nsISample)不会公开,您必须明确请求它。你可以这样做,例如通过实例化你的组件:

var greenfox = Components.classes["..."].getService(Components.interfaces.nsISample);
greenfox.start();

或者,您也可以在已有的对象上调用QueryInterface

greenfox.QueryInterface(Components.interfaces.nsISample);
greenfox.start();

通常,我不建议使用二进制XPCOM组件,原因是here,维护它们需要花费太多精力。我宁愿建议编译一个常规DLL并通过js-ctypes使用它。 Reference a binary-component to js-ctypes提到如何在附加组件中找到DLL以通过js-ctypes使用它。

答案 1 :(得分:0)

你用你的uuid调用QueryInterface吗?在使用创建的组件实例之前,必须先调用它。您的使用情况是否与here中的内容相符?

如果您不想处理XPCOM,可以使用js-ctypes