Find the secrets to infinite income, and automate it!
6 Dec
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.
2 Responses for "Firefox Extension - Preference Handeling"
Excuse me for my stupid question, I’m trying to create a Firefox Extension, your article is VERY VERY useful!
But I’m a noob and I’d like to know what I must enter in the usage on Preference.setPref(’this_works’, true); instead of ” ‘this_works’ “.
Greetings,
Gianluca
Gianluca, “this_works” is the variable name you want to store and the second param “true” is the value. So if you wanted to store the users name it would be Preference.setPref(’name’, ‘Gianluca’);
Hope that helps!
Leave a reply