Programatically modify firefox settings

I refuse to believe that nobody has needed to silently push firefox setting / configuration in an unattended manner, but I was unable to find any directions on how to do so. In particular, I need to automatically, using a script, modify the no-proxy exclusion list.

Here is the way I eventually ended up doing it.

Firefox stores it’s configuration in prefs.js in the Profile folder for the user.
That is (on XP anyway) C:\Documents and Settings\[user]\Application Data\Mozilla\Firefox\Profiles\[randomtext].default\prefs.js
Which can be specified as %APPDATA%\Mozilla\Firefox\Profiles\[randomtext].default\prefs.js

Since it contains a random string, how the heck do you find it??
Well, you can parse the profiles.ini file in %APPDATA\Mozilla\Firefox. It looks like this:

[General]
StartWithLastProfile=1

[Profile0]
Name=default
IsRelative=1
Path=Profiles/cutfy0h4.default

Luckily, you can use a FOR loop within a winxp/2k batch file to grab the profile name(s).

FOR /F "delims==/ tokens=3 usebackq" %%i IN ("%APPDATA%\Mozilla\Firefox\profiles.ini") DO (
 echo %%i
)

This will echo just the “cutfy0h4.default”, and it will only return for lines of the file that contain 3 fields.
(For more information about the FOR loop, see here: http://www.robvanderwoude.com/ntfor.html.)

Now that we can locate the profile, how the heck do we modify the setting?

Turns out, firefox looks for a file called user.js, and applies those settings AFTER applying prefs.js. Any settings in user.js will be applied to prefs.js.
So you put your settings in user.js, copy it into the profile folder, start and stop firefox, then delete user.js. (If you don’t delete user.js, the user will be unable to modify the setting).

Warnings:
1. I am not responsible if this breaks anything. USE AT YOUR OWN RISK!!
2. If there are multiple profiles, it will not work right. In most cases there should only be one.
3. If firefox adds more stuff to profiles.ini, or fixes the direction of the slash in the profile path, this script will break.
4. This was developed with Firefox 2.0.0.13

Here is my final script:

You will need a file called “user.js.install” in the same folder as the batch file containing whatever settings you want to change. (Use prefs.js as your guide) Like this one:

user_pref("network.proxy.no_proxies_on", "localhost, 127.0.0.1, superserver, 192.168.0.1");

Here is the batch script:

taskkill /IM firefox.exe > javainst.log 2>&1
FOR /F "delims==/ tokens=3 usebackq" %%i IN ("%APPDATA%\Mozilla\Firefox\profiles.ini") DO (
  echo "%APPDATA%\Mozilla\Firefox\Profiles\%%i\prefs.js"  >> proclog.log 2>&1
  IF EXIST "%APPDATA%\Mozilla\Firefox\Profiles\%%i\prefs.js" (
    copy /Y user.js.install "%APPDATA%\Mozilla\Firefox\Profiles\%%i\user.js"  >> proclog.log 2>&1
    start firefox.exe
    rem wait 7 seconds for firefox to start
    ping 1.1.1.1 -n 1 -w 7000
    taskkill /IM firefox.exe  >> proclog.log 2>&1
    del "%APPDATA%\Mozilla\Firefox\Profiles\%%i\user.js"  >> proclog.log 2>&1
  )
)

Leave a Reply