Search code examples
firefox-addonxulpreferences

Using preferences in XUL


I'm trying to figure out how to use a preference (the ones you can access by going to about:config) in an XUL file. I'll have a few developers working on this and we all have different "local" hosts configured. So I'd like to make things easier for everyone by making the "example.local" part belos come from a preference setting (string). That way I can use example.com as the default and each developer simply has to edit it to whatever their hosts file is set to.

<?xml version="1.0" encoding="UTF-8"?>
<overlay id="my-overlay" xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
    <script src="http://example.local/some/script.js"/>
</overlay>

I know how to set the default and how to access it from javascript but this particular xul above is the one loading the javascript and I need it to load from the server.

Thank you very much for your help!

Luis


Solution

  • You need to create a prefs.js file with the default values you want.

    See the documentation here.

    Then, you can reference the preference using nsiPrefService, like this, if it is a boolean stored as mybranch.mybooleanpreference:

    var prefs = Components.classes["@mozilla.org/preferences-service;1"]
                        .getService(Components.interfaces.nsIPrefService).getBranch("mybranch.");
    
    var value = prefs.getBoolPref("mybooleanpreference"); 
    

    See Preference code snippet here, or