簡體   English   中英

無法為Firefox擴展創建javascript XPCOM服務

[英]Can't create javascript XPCOM service for Firefox extension

我已經在這個特殊的磚牆上撞了我的頭,現在已經超過兩天了。 我正在嘗試創建一個用於Firefox擴展的XPCOM服務,但無法初始化該組件,並在Firefox的錯誤控制台中顯示以下錯誤。

Timestamp: 07/06/2012 09:23:28 Error: uncaught exception: [Exception... 
"Component returned failure code: 0x80570016 (NS_ERROR_XPC_GS_RETURNED_FAILURE)
[nsIJSCID.getService]"  nsresult: "0x80570016 (NS_ERROR_XPC_GS_RETURNED_FAILURE)"
location: "JS frame :: chrome://logger/content/logger.js :: <TOP_LEVEL> :: line 21"
data: no]

我使用ted.mielczarek.org上的優秀樣板生成器將組件減少到最低限度。 組件代碼如下......

const nsISupports = Components.interfaces.nsISupports;
const CLASS_ID = Components.ID("808e1607-caea-418c-b563-d9fe1df6ee08");
const CLASS_NAME = "Test component";
const CONTRACT_ID = "@test/loggerservice;1";

function LoggerService() {
  this.wrappedJSObject = this;
}

LoggerService.prototype = {
  QueryInterface: function(aIID)
  {
    if (!aIID.equals(nsISupports))
      throw Components.results.NS_ERROR_NO_INTERFACE;
    return this;
  }
}

創建模塊和工廠接口的樣板的其余部分保持不變。

chrome.manifest文件看起來像這樣......

content   logger                 chrome/content/
skin      logger   classic/1.0   chrome/skin/
locale    logger   en-US         chrome/locale/en-US/

component {808e1607-caea-418c-b563-d9fe1df6ee08} components/loggerservice.js
contract @test/loggerservice;1 {808e1607-caea-418c-b563-d9fe1df6ee08}

overlay chrome://browser/content/browser.xul chrome://logger/content/logger-overlay.xul
style   chrome://global/content/customizeToolbar.xul chrome://logger/skin/overlay.css

最后, logger-overlay.xul logger.js文件包含一個腳本文件 - logger.js - 它嘗試使用以下代碼獲取對LoggerService組件的引用...

this.loggerService = Components.classes["@test/logger;1"].getService().wrappedJSObject;

這是在firefox錯誤控制台中報告的這一行。

我看不出我能做到多么簡單 - 任何見解都會非常感激。

這是一個很好的樣板生成器,但遺憾的是它已經過時了。 首先,你應該使用XPCOMUtils ,這將擺脫大部分樣板。 更重要的是,這個boilerplace生成器尚未更新為Gecko 2.0中的XPCOM更改,並定義了NSGetModule函數而不是NSGetFactory 這樣的模塊代碼應該可行:

Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");

function LoggerService() {
  this.wrappedJSObject = this;
}

LoggerService.prototype = {
  classID: Components.ID("808e1607-caea-418c-b563-d9fe1df6ee08"),
  classDescription: "Test component",
  contractID: "@test/loggerservice;1",

  QueryInterface: XPCOMUtils.generateQI([])
}

if ("generateNSGetFactory" in XPCOMUtils)
  var NSGetFactory = XPCOMUtils.generateNSGetFactory([LoggerService]);  // 2.0+
else
  var NSGetModule = XPCOMUtils.generateNSGetModule([LoggerService]);    // 1.9.x

如果您的擴展不需要與Firefox 3.6兼容,則可以刪除NSGetModule代碼。 您也可以刪除classDescriptioncontractID屬性,這些屬性已在chrome.manifest中指定。

注意 :如果您只需要一個可以在整個瀏覽會話中使用的對象,並且可以從任何地方訪問,那么JavaScript代碼模塊將是更好的選擇 - 沒有XPCOM樣板,也沒有wrappedJSObject黑客。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM