user366121
user366121

Reputation: 3271

firefox extension saving in preferences

I want to save a simple string into preferences in my firefox extension. The string should be accessible between separate browser sessions and after reboot. I found the following code but for some reason it is not working. I tried localStorage before but this was also not working:

Code for saving function:

var prefs = Components.classes["@mozilla.org/preferences-service;1"]
                .getService(Components.interfaces.nsIPrefBranch);

var str = Components.classes["@mozilla.org/supports-string;1"]
      .createInstance(Components.interfaces.nsISupportsString);

str.data = window['myglobalvariable'];

prefs.setComplexValue("myglobvar", 
      Components.interfaces.nsISupportsString, str);

Code for retrieval function:

var prefs = Components.classes["@mozilla.org/preferences-service;1"]
                .getService(Components.interfaces.nsIPrefBranch);

var value = prefs.getComplexValue("myglobvar",
  Components.interfaces.nsISupportsString).data;

I cannot figure out why is it not working.

Upvotes: 1

Views: 1063

Answers (1)

user1991627
user1991627

Reputation: 29

if your preference is a integer or boolean getCharPref wont work you would have to use

getBoolPref() or getIntPref()

http://blog.mozilla.org/addons/2009/01/28/how-to-develop-a-firefox-extension/

Upvotes: 2

Related Questions