How do I programmatically modify Firefox preferences without restarting?

With GCLI, this was as simple as

pref set <setting> <value>

Loading a user.js file requires restarting Firefox. I want to toggle settings back and forth within a single session.


Programmatically manage preferences

I wanted to programatically modify the browser preference for managing the proxy configuration. Trial and error showed that network.proxy.type) was the preference which controlled the proxy configuration and Internet research (how I found this question) showed that the way to dynamically modify browser preferences is to run JavaScript statements in Firefox’s Browser Console command line.

Using the Browser Console command line

Note that the Browser Console is not the same as the Web Console:

The Browser Console is like the Web Console, but applied to the whole browser rather than a single content tab.

Also, note that the command line UI has to be enabled before use:

The Browser Console command line is disabled by default. To enable it set the devtools.chrome.enabled preference to true in about:config, or set the "Enable chrome debugging" option in the developer tool settings.

The Browser Console can be opened with the following keyboard shortcuts:

  • Ctrl-Shift-J (PC keyboard)
  • Ctrl-Shift-J (Mac keyboard)

Working with the Preferences API

My research showed that Firefox provides a Services.prefs object which has a number of methods for getting and setting preferences – depending on the type of the preference, e.g., Boolean, Integer, String.

I suspected that the network.proxy.type preference was most likely an Integer type as it can have useful values ranging from 0 to 5 (see Network.proxy.type - MozillaZine Knowledge Base). I verified this by using the getPrefType() method and comparing its return value with that of PREF_INT:

>> Services.prefs.getPrefType("network.proxy.type") === Services.prefs.PREF_INT
True

I then used the getIntPref() method to see what the preference was set to:

>> Services.prefs.getIntPref("network.proxy.type")
5

The MozillaZine Knowledge Base for Network.proxy.type describes 5 as corresponding to

Use system proxy settings. (Default in Linux; default for all platforms, starting in 1.9.2.4 /Firefox 3.6.4).

I was able to use the setIntPref() to change this value to 1 (use the Manual proxy configuration) by running:)

>> Services.prefs.setIntPref("network.proxy.type", 1)