Command line 有没有办法在无重启插件中实现NSIComandLineHandler?

Command line 有没有办法在无重启插件中实现NSIComandLineHandler?,command-line,firefox-addon,command-line-arguments,firefox-addon-sdk,xpcom,Command Line,Firefox Addon,Command Line Arguments,Firefox Addon Sdk,Xpcom,有没有办法在无重启插件中实现NSIComandLineHandler 从中似乎可以,但此代码(从exports.main中运行)不适用于我: var { Class } = require('sdk/core/heritage'); var { Unknown, Factory } = require('sdk/platform/xpcom'); var { Cc, Ci } = require('chrome'); var contractId = '@mozilla.org/command

有没有办法在无重启插件中实现NSIComandLineHandler

从中似乎可以,但此代码(从exports.main中运行)不适用于我:

var { Class } = require('sdk/core/heritage');
var { Unknown, Factory } = require('sdk/platform/xpcom');
var { Cc, Ci } = require('chrome');

var contractId = '@mozilla.org/commandlinehandler/general-startup;1?type=webappfind';

// Define a component
var CommandLineHandler = Class({
  extends: Unknown,
  get wrappedJSObject() this,

  classDescription: "webAppFinder",
  /* Not used by SDK, so commenting out
  _xpcom_categories: [{  
    category: "command-line-handler",  
    // category names are sorted alphabetically. Typical command-line handlers use a  
    // category that begins with the letter "m".  
    entry: "m-webappfind"  
  }],
  */
  helpInfo : "  -webappfind               Open My Application\n",
  // nsICommandLineHandler
  handle : function clh_handle(cmdLine) {
    try {
        console.log('good so far'); // Doesn't actually reach here
        var fileStr = cmdLine.handleFlagWithParam("webappfind", false);
        if (fileStr) {
          console.log('made it');
        }
    }
    catch (e) {
        Cu.reportError("incorrect parameter passed to -webappfind on the command line.");
    }

    if (cmdLine.handleFlag("webappfind", false)) { // no argument
        cmdLine.preventDefault = true;
        throw 'A valid ID must be provided to webappfind';
    }
  },
  hello: function() {return 'Hello World';}
});

// Create and register the factory
var factory = Factory({
  contract: contractId,
//  id: '{7f397cba-7a9a-4a05-9ca7-a5b8d7438c6c}', // Despite the docs saying one can add both, this doesn't work
  Component: CommandLineHandler
});
我有下面的代码之后,它的工作

// XPCOM clients can retrieve and use this new
// component in the normal way
var wrapper = Cc[contractId].createInstance(Ci.nsISupports);
var helloWorld = wrapper.wrappedJSObject;
console.log(helloWorld.hello());
…但Firefox不接受命令行参数,因为出现以下错误:

错误:警告:无法识别的命令行标志-webappfind

源文件:resource://app/components/nsBrowserContentHandler.js 电话号码:765

更新

我现在采纳@nmaier的建议添加类别,因此在后面添加了以下几行:

var catMan = Cc['@mozilla.org/categorymanager;1'].getService(Ci.nsICategoryManager); //
catMan.addCategoryEntry('command-line-handler', 'm-webappfind' /*contractId*/, contractId, false, true);
但我在从命令行调用时遇到以下3个错误:

错误:[调用方法时出现异常…”“失败”: [NSIFORY::createInstance]“nsresult:”0x80004005 (NS_错误_失败)“位置:”本机帧::: ::第0行“数据:否]

合同ID '@mozilla.org/commandlinehandler/general-startup;1?类型=webappfind' 已注册为条目“m-webappfind”的命令行处理程序,但 无法创建

错误:警告:无法识别的命令行标志-webappfind

源文件:resource://app/components/nsBrowserContentHandler.js 电话号码:765


SDK不会为您注册类别

有关类别的一些备注可在此处找到:

但是,我仍然不确定是否在处理初始命令行之前启动了引导扩展。我想是反复试验吧

编辑: 您的组件没有指定任何接口,因此它只支持
nsISupports

SDK模块文档说明您应该添加
接口:['nsicomandlinehandler']
属性。

SDK不会为您注册类别

有关类别的一些备注可在此处找到:

但是,我仍然不确定是否在处理初始命令行之前启动了引导扩展。我想是反复试验吧

编辑: 您的组件没有指定任何接口,因此它只支持
nsISupports

SDK模块文档声明您应该添加一个
接口:['nsicomandlinehandler']
属性。

谢谢……我已经更新了我的主要帖子,指出了我看到的3个错误。基于这些错误,我的猜测是,时间安排可能还可以,但其他方面有问题,尽管我不确定。有什么想法吗?更新了答案,关于缺少的
接口
。很有效!当我通过构建器更新加载项时,即使没有重新启动,它也会使用最新的处理程序实现。我明白了,由于第二个示例没有使用接口,我被遗漏了。我爱你,男人!:)作为参考,根据,Firefox似乎没有使用
helpInfo
。此外,还需要额外的类别管理器代码,但如果您在Firefox仍处于打开状态时重新安装该插件,则可能不会显示该代码。但是我已经从我的主要帖子中注释掉了类别,因为这是不需要的,因为正如你所说的,SDK没有使用它。正如我在中所写的,你真的需要在卸载时删除类别,由于您的无重启插件需要在更新后进行清理,您不希望在更新后泄漏并保留组件的旧实现,因此您需要以一种笨拙(延迟)的方式进行。您应该通过禁用加载项并检查注释是否确实已从NSCategoryManager枚举中删除来测试代码。谢谢…我已更新了我的主要帖子,以指出我看到的3个错误。基于这些错误,我的猜测是,时间安排可能还可以,但其他方面有问题,尽管我不确定。有什么想法吗?更新了答案,关于缺少的
接口
。很有效!当我通过构建器更新加载项时,即使没有重新启动,它也会使用最新的处理程序实现。我明白了,由于第二个示例没有使用接口,我被遗漏了。我爱你,男人!:)作为参考,根据,Firefox似乎没有使用
helpInfo
。此外,还需要额外的类别管理器代码,但如果您在Firefox仍处于打开状态时重新安装该插件,则可能不会显示该代码。但是我已经从我的主要帖子中注释掉了类别,因为这是不需要的,因为正如你所说的,SDK没有使用它。正如我在中所写的,你真的需要在卸载时删除类别,由于您的无重启插件需要在更新后进行清理,您不希望在更新后泄漏并保留组件的旧实现,因此您需要以一种笨拙(延迟)的方式进行。您应该通过禁用加载项并检查注释是否确实已从NSCategoryManager枚举中删除来测试代码。