I wrote a class to deal with preference handling in the PriceAdvance Firefox extension. I found the Mozilla documentation to be difficult to understand. So, I decided to share the class. Theres a little bug in Firefox that took me two days and borderline suicide to figure out… Extensions can have a default preference file that you can query using the preference interface. These preference files are “magically” loaded from a very specific file folder/location - “defaults\preferences\xxxxx.js”. To set a preference you simply put “pref(”branch_title.variable_name”, value);” in the file. If you use the class I made these preferences can be accessed like this: “Preference.getPref(”variable_name”)”. Now the bug I found is with the way you configure your extension. If the extension is bundled and installed “properly” it will work without a hitch. For development purposes I have my extension using a pointer from the extension folder to another folder location where I do all the work. Apparently when its setup as a pointer it wont load the preference files. Whats really annoying is theres no way to verify if the magical files have been loaded or not. Because of this I have avoided preference defaults and use my own variables class.

Preferences = {
initialize: function() {
  this.prefs = Components.classes['@mozilla.org/preferences-service;1']
  .getService(Components.interfaces.nsIPrefService)
  .getBranch('extensions.branch_title.');
 
  this.prefs.QueryInterface(Components.interfaces.nsIPrefBranch2);
},
 
getPref: function(name) {
  var type = this.prefs.getPrefType(name);
 
  if (type == this.prefs.PREF_STRING)
    return this.prefs.getCharPref(name);
  else if (type == this.prefs.PREF_INT)
    return this.prefs.getIntPref(name);
  else if (type == this.prefs.PREF_BOOL)
   return this.prefs.getBoolPref(name);
},
 
setPref: function(name, value) {
  var type = this.prefs.getPrefType(name);
 
  if(type == 0) {
    if(typeof(value) == 'number')
      type = 64;
    else if(typeof(value) == 'string')
      type = 32;
    else if(typeof(value) == 'boolean')
      type = 128;
  }
 
  if (type == this.prefs.PREF_STRING)
    this.prefs.setCharPref(name, value);
  else if (type == this.prefs.PREF_INT)
    this.prefs.setIntPref(name, value);
  else if (type == this.prefs.PREF_BOOL)
    this.prefs.setBoolPref(name, value);
}
};
 
// USAGE:
Preferences.initialize();
Preference.setPref('this_works', true);
alert(Preference.getPref('this_works'));
/******************/

To view preference variables that have been set, type “about:config” into the address bar and filter for your branch.