Pergunta

I have the following preferences in my package.json file

"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"
        }
]

I have the following code in my main.js file.

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"));

When i execute the code, the following error is displayed.

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]

Is there something that i am missing out on?

Foi útil?

Solução

Your bug is on this line: prefs = prefs.getBranch("extensions.myExtension.");

You have to look in the file and find the id of your addon. It should start with jid Then go like this:

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

but if you're using addon sdk use the simple pref service:

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

require('sdk/simple-prefs').prefs['api_url']

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top