views:

377

answers:

2

How do I access user preferences in Firefox? I have the following code:

var control = document.getElementById(control_id);
if (control) {
    control.setAttribute('color', nsPreferences.copyUnicharPref(prefstr, default_val));
}

But when I run this, I get the following:

Error: nsPreferences is not defined
Source file: chrome://backgroundtoggle/content/options.xul
Line: 9

I'm having trouble wading through the Mozilla documentation. How do I make this do what I want?

Thanks.

A: 

I am getting the same error. Any help would be appreciated

+3  A: 

It looks like you need to include nsUserSettings.js in your .xul file:

<script type="application/x-javascript" src="chrome://global/content/nsUserSettings.js" />

This is where nsUserPreferences is defined.

See here for an example options.xul file. The script tag should be the first child element of prefwindow to ensure it gets loaded before your own code does.


Additional Information

This looks like a decent tutorial on how to write Firefox extensions. It seems to do a good job of consolidating all the relevant information on mozilla.org in one place, and contains links to mozilla.org when you need more details on a topic being covered. I do wish the navigation was a little better, but what can you do? Use the navigation pane on the left-hand side and click Tutorial to get started. You have to move through each section using the navigation pane, which took me few seconds to figure out, as I was looking for 'Next' and 'Previous' links on the page.

Mike Spross
Sometimes you are able to use nsPreferences, even without including the script. My guess is that this is because some other extension has loaded that script for you, leading you to believe that everything is okay. Then, a user who has far less add-ons than you do, tries to install your extension, and it doesn't work. Sneaky.
MatrixFrog
@Matrix: That is sneaky.
Mike Spross