有没有办法在无需重启的插件中实现 nsICommandLineHandler ?
https://addons.mozilla.org/en-US/developers/docs/sdk/latest/modules/sdk/platform/xpcom.html似乎有可能,但是这段代码(从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 个错误:
错误:[异常...“调用方法时的“失败”:[nsIFactory::createInstance]”nsresult:“0x80004005 (NS_ERROR_FAILURE)”位置:“本机框架 :: :: :: line 0”数据:否]
合同 ID '@mozilla.org/commandlinehandler/general-startup;1?type=webappfind' 已注册为条目 'm-webappfind' 的命令行处理程序,但无法创建。
错误:警告:无法识别的命令行标志 -webappfind
源文件:resource://app/components/nsBrowserContentHandler.js 行:765