Referencing Firefox Default Preferences from JavaScript

Firefox preferences denote configuratiosn or some other information saved for Firefox. It can be accessed with URL (about:config) manually.

Steps to view preferences manually:

  1. Open firefox
  2. Enter ‘about:config’ in address bar
  3. Press Enter key
  4. Preferences table will be displayed

Referencing from JavaScript

In some occasion, it is necessary to modify the preferences values by automatically.
The steps necessary to reference the preferences system from within the JavaScript code could have been made simpler, but it is not too bad. First, your file must have access to the Preference Manager component. To do that, you must add the following line of code to your file:

var prefManager = Components.classes["@mozilla.org/preferences-service;1"]
.getService(Components.interfaces.nsIPrefBranch);

Now that you have access to the preference manager you can get and set preferences using getIntPref()/getBoolPref()/getCharPref() and setIntPref()/setBoolPref()/setCharPref(). These functions are very straight forward and have been used in the code samples above.
Following example shows to modify the default home page in Firefox

To get preference value:

var prefString = prefManager.getCharPref("browser.startup.homepage");

To set preferences value:

var prefString = prefManager.getCharPref("browser.startup.homepage", “https://dlak.wordpress.com”);

One comment

  1. I faced this error: Permission denied for … to get property XPCComponents.classes.
    Can you help me to fix this error.
    (I’m a amateur).

    Thanks.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s