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”);

Advertisements