2012-08-08 76 views
1

我尝试过一百万种不同的东西,无法获得此功能。我的Firefox扩展无法使用。什么是使用C++ XPCOM组件的Firefox扩展(XPI文件)的结构?

里面的XPI文件我有:

* content folder 
    -browserOverlay.js 
    -browserOverlay.xul 

* locale folder 
    *en-US folder 
     -browserOverlay.dtd 
     -browserOverlay.properties 
* skin folder 
     -browserOverlay.css 

我有火狐14.0.1和我的“Hello World”的扩展,不使用XPCOM是工作的一部分。现在只需添加一个带有按钮的工具栏,点击后显示“Hello World”,然后尝试运行我使用Gecko SDK构建并使用VS 2005编译的C++ XPCOM组件。

browserOverlay .js文件包含以下代码:

/** 
* XULSchoolChrome namespace. 
*/ 
if ("undefined" == typeof(XULSchoolChrome)) { 
    var XULSchoolChrome = {}; 
}; 

/** 
* Controls the browser overlay for the Hello World extension. 
*/ 
XULSchoolChrome.BrowserOverlay = { 
    /** 
    * Says 'Hello' to the user. 
    */ 
    sayHello : function(aEvent) { 
    let stringBundle = document.getElementById("xulschoolhello-string-bundle"); 
    let message = stringBundle.getString("xulschoolhello.greeting.label"); 

    window.alert(message); 


    try 
    { 
     // normally Firefox extensions implicitly have XPCOM privileges 
     //but since this is a file we have to request it. 

     netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect"); 
     const cid = "@example.com/XPCOMSample/MyComponent;1"; 
     obj = Components.classes[cid].createInstance(); 
     // bind the instance we just created to our interface 
     obj = obj.QueryInterface(Components.interfaces.IMyComponent); 
    } catch (err) { 
     alert(err); 
     return; 
    } 
    var res = obj.Add(3, 4); 
    alert('Performing 3+4. Returned ' + res + '.'); 
    } 
}; 

当我单击按钮时,我看到第一个警报窗口。当我单击确定时,我看到一个窗口,显示以下错误消息:

TypeError: Components.classes[cid] is undefined

我在做什么错?我应该放置.dll文件的xpi文件的位置在哪里? .idl文件? .xpt文件?

回答

2

我想你是一个过时的教程,大多数教程还没有更新到XPCOM changes in Gecko 2.0。但是您的问题:

Where within the xpi file am I supposed to place the .dll file?

您想要的位置。传统的放置位于components/子目录中。

the .idl file?

它不应该是你的延伸,只有.xpt文件的一部分。

the .xpt file?

再次你喜欢的任何地方,但通常在与.dll文件相同的目录中。

您添加必要的声明,以您的chrome.manifest文件然而,通常这样的事情是很重要的:

interfaces components/mycomponent.xpt 
binary-component components/mycomponent.dll ABI=WINNT_x86-msvc 

又见documentation什么需要的组件代码改变(没有在一个example Firefox的源代码树,如果你喜欢它)。请注意,您必须使用Gecko SDK版本编译组件,该版本与您要使用该组件的Firefox版本匹配。因此,对于Firefox 14,您需要使用Gecko SDK 14.因为不再有任何二进制兼容性,您的组件将只能在Firefox 14中使用,而不能在Firefox 13或Firefox 15中使用。

+0

迄今为止您的回答非常有帮助,但是我仍然缺少一件事:鉴于最新的Gecko SDK不包含xpidl二进制文件,我如何编译IDL文件?你知道我在哪里可以找到当前的教程吗? – 2012-08-09 16:57:46

+0

@JohnSmith:正如'xpidl'文档中提到的,你应该使用['pyxpidl'](https://developer.mozilla.org/en-US/docs/XPIDL/pyxpidl)。 – 2012-08-09 18:04:11