无法为Firefox扩展程序创建JavaScript XPCOM服务 [英] Can't create javascript XPCOM service for Firefox extension

查看:225
本文介绍了无法为Firefox扩展程序创建JavaScript XPCOM服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在我已经把头撞在这堵砖墙上了两天以上。我正在尝试创建一个用于Firefox扩展的XPCOM服务,但无法在Firefox中的错误控制台中显示以下错误来初始化组件。

组件返回失败代码: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:我已经使用优秀的模板生成器将组件减少到了最低限度,。组件代码如下...

pre $ const const nsISupports = Components.interfaces.nsISupports;
const CLASS_ID = Components.ID(808e1607-caea-418c-b563-d9fe1df6ee08);
const CLASS_NAME =测试组件;
const CONTRACT_ID =@ test / loggerservice; 1;

函数LoggerService(){
this.wrappedJSObject = this;

$ b $ LoggerService.prototype = {
QueryInterface:function(aIID)
{
if(!aIID.equals(nsISupports))
抛出Components.results.NS_ERROR_NO_INTERFACE;
返回这个;






创建模块和工厂的样板的其余部分接口是不变的。



chrome.manifest文件看起来像这样...

 内容记录器chrome / content / 
皮肤记录器classic / 1.0 chrome / skin /
语言环境记录器en-US chrome / locale / en-US /

组件{ 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 - 使用下面的代码尝试获取对 LoggerService 组件的引用...

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

,这是firefox错误控制台中的报告。



我看不出有多简单,我可以做到 - 任何洞察力将非常感激。

>

这是一个很好的样板发生器,但不幸的是一个过时的。首先,你应该使用 XPCOMUtils ,这将摆脱大部分的样板。更重要的是,这个boilerplace生成器还没有被更新到 GeoCo 2.0中的XPCOM变化< a>并定义 NSGetModule 函数,而不是 NSGetFactory 。这样的模块代码应该可以工作:

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

函数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(在XPCOMUtils中生成NSGetFactory)
var NSGetFactory = XPCOMUtils.generateNSGetFactory([LoggerService]); // 2.0+
else
var NSGetModule = XPCOMUtils.generateNSGetModule([LoggerService]); // 1.9.x

您可以删除 NSGetModule 代码,如果你的扩展不需要与Firefox 3.6兼容。您也可以删除 classDescription contractID 属性,然后在 chrome.manifest注意:如果您只需要一个可以在整个浏览会话中保留的对象,并且可以使用从任何地方访问,然后 JavaScript代码模块将是一个更好的选择 - 没有XPCOM样板和no wrappedJSObject hacks。


I've been banging my head a against this particular brick wall now for more than two days. I am attempting to create an XPCOM service for use in a Firefox extension but am unable to initialise the component with the following error displayed in the error console in 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]

I have reduced the component to the bare minimum using the excellent boilerplate generator at ted.mielczarek.org. The component code is as follows...

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;
  }
}

The remainder of the boilerplate that creates the module and factory interfaces is unchanged.

The chrome.manifest file looks like this...

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

Finally, the logger-overlay.xul file includes a script file - logger.js - which attempts to get a reference to the LoggerService component using the following code...

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

and it is this line that is reporting in the firefox error console.

I can't see how much simpler I can make it - any insight would be very much appreciated.

解决方案

This is a nice boilerplate generator but unfortunately an outdated one. For one, you should be using XPCOMUtils, this will get rid of most of the boilerplate. More importantly, this boilerplace generator hasn't been updated to XPCOM changes in Gecko 2.0 and defines NSGetModule function instead of NSGetFactory. Module code like this should work however:

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

You can remove the NSGetModule code if your extension doesn't need to be compatible with Firefox 3.6. You can also remove the classDescription and contractID properties then, these are specified in chrome.manifest already.

Note: If you only need an object that will stay around for the entire browsing session and can be accessed from anywhere then a JavaScript code module would be a better choice - no XPCOM boilerplate and no wrappedJSObject hacks.

这篇关于无法为Firefox扩展程序创建JavaScript XPCOM服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆