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.in开发者_StackOverflow中文版terfaces.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.
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/
精彩评论