2012-08-08 89 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