Question

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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top