views:

28

answers:

2

Hi, 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"&gt;
    <script src="http://example.local/some/script.js"/&gt;
</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

A: 

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

pc1oad1etter
Thanks! This is helpful for setting the default preference but how do I use the value in the XUL? The effect would allow me to do something like: <?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://[[value-from-preference]]/some/script.js"/> </overlay>Thanks again!
luisgo
That is... where [[value-from-preference]] is the value set in the preference.
luisgo
I"ve added more documentation and an example of getting a preference value in Javascript.
pc1oad1etter
That works great. I am able to read the preference set by default. But any idea how to use that preference in the XUL itself? I want developers to be able to change the preference so example.com above can be changed through it. Thanks.
luisgo
You need to refer to it in JavaScript. I don't think that you could access that directly from XUL, without Javascript. I guess you could set the src of that element in the onload event, which may accomplish what you want.
pc1oad1etter
A: 

It sounds like you want to do something like this:

In your XUL file add:

<body onload="return myFunc();">

In your javascript add a function:

function myFunc() {
var prefs = Components.classes["@mozilla.org/preferences-service;1"]
                    .getService(Components.interfaces.nsIPrefService).getBranch("mybranch.");
   var src = "http://"+prefs.getStringPref("mystringpreference")+"/some/script.js";
   var script = document.getElementsByTagName("script")[0];

   script.setAttribute("src", src);
}

Making sure you add the script include for the javascript file as well, something like:

<script src="chrome://myextension/content/myjavascript.js

Hope this helps!

Alex
was this of any help?
Alex