简体   繁体   English

XPCOM对象方法无法从JavaScript访问

[英]XPCOM object method is unaccessible from JavaScript

I'm trying to build an extension for Firefox. 我正在尝试为Firefox构建扩展程序。 This extension uses an XPCOM component (a C++ dll). 此扩展使用XPCOM组件(C ++ dll)。 I'm compiling the DLL, compilation is OK. 我正在编译DLL,编译正常。

I also succeeded in building a JS code which instanciates the object from XPCOM: 我还成功构建了一个可以从XPCOM实例化该对象的JS代码:

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. 一切都很好,只是我无法调用应该在我的XPCOM组件中的函数。

Here is my IDL file: 这是我的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();
};

When callingstart() function, I get the Javascript error: "is not a function" 调用start()函数时,出现Javascript错误:“不是函数”

greenfox.start();

Do you have any idea? 你有什么主意吗? It seems like no function is exposed in my XPCOM. 我的XPCOM中似乎没有任何功能公开。

You seem to be looking at an object exposing only the nsISupports interface. 您似乎正在查看仅公开nsISupports接口的对象。 Your interface ( nsISample ) won't get exposed by default, you have to explicitly request it. 默认情况下,您的接口( nsISample )将不会公开,您必须显式请求它。 You can do it for example by instantiating your component like this: 例如,可以通过实例化组件来做到这一点:

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

Alternatively, you can also call QueryInterface on an object you already have: 或者,您也可以在已经具有的对象上调用QueryInterface

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

Generally, I wouldn't recommend using a binary XPCOM component for reasons outlined here , maintaining them requires way too much effort. 通常,出于此处概述的原因,我不建议使用二进制XPCOM组件,维护它们需要花费大量精力。 I would rather suggest compiling a regular DLL and using it via js-ctypes . 我宁愿建议编译一个常规DLL并通过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. 引用js-ctypes的二进制组件时,提到了如何在附件中定位DLL以便通过js-ctypes使用它。

Do you call QueryInterface with your uuid? 您是否用uuid调用QueryInterface? 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 . 如果您不想使用XPCOM,则可以使用js-ctypes

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM