如何设置和访问package.json文件中定义的firefox附加组件首选项。

时间:2014-03-20 16:52:16

标签: firefox firefox-addon firefox-addon-sdk

我的package.json文件中有以下首选项

"preferences": [
        {
            "name": "api_url", 
            "title": "Install url", 
            "type": "string", 
            "value": "https://google.com"
        }, 
        {
            "name": "api_username", 
            "title": "Install username", 
            "type": "string"
        }, 
        {
            "name": "api_key", 
            "title": "API key", 
            "type": "string"
        }
]

我的main.js文件中有以下代码。

const {Cc,Ci} = require('chrome');

var prefs = Cc["@mozilla.org/preferences-service;1"].getService(Ci.nsIPrefService);
    prefs = prefs.getBranch("extensions.myExtension.");

console.log(prefs.getCharPref("api_url"));

执行代码时,会显示以下错误。

Message: [Exception... "Component returned failure code: 0x8000ffff (NS_ERROR_UNEXPECTED) [nsIPrefBranch.getCharPref]"  nsresult: "0x8000ffff (NS_ERROR_UNEXPECTED)"  location: "JS frame :: resource://gre/modules/XPIProvider.jsm -> jar:file:///tmp/tmpJvzkDz.mozrunner/extensions/jid1-zsQS9x5zIenFuw@jetpack.xpi!/bootstrap.js -> resource://gre/modules/commonjs/toolkit/loader.js -> resource://jid1-zsqs9x5zienfuw-at-jetpack/myExtension/lib/main.js :: exports.main/<.onLoad/</<.success :: line 89"  data: no]

有什么东西我错过了吗?

1 个答案:

答案 0 :(得分:3)

您的错误就在这一行: prefs = prefs.getBranch("extensions.myExtension.");

您必须查看该文件并找到您的插件的ID。它应该以{{1​​}}开头 然后就这样:

jid

但如果您使用的是addon sdk,请使用简单的pref服务:

https://developer.mozilla.org/en-US/Add-ons/SDK/High-Level_APIs/simple-prefs

prefs = prefs.getBranch("extensions.jid1-lwNbwJJiiMXM4A@jetpack.");