Default Preferences

Introduction

A key part of any extension is the default settings that come with it. Mozilla provides a simple way of shipping default settings by allowing default preferences.

Remember that preferences are held in two separate trees. One with user supplied prefs, another with default prefs. User supplied prefs are set using the GUI and also by modifying a profile's prefs.js file (which uses the user_pref() function). Default preferences are set similarly but inside an extension and use the pref() function.

Setting default preferences

While most of an extension's directories can be named arbitrarily and mapped using a chrome manifest, default preferences must be in very particular spot:

| Extension Root
+--|content
+--|skin
+--|defaults 
   +--|preferences

To add preferences simply drop a .js file into that directory and Mozilla will read it and set the appropriate preferences.

The actual file, despite having .js extension, is not a JavaScript file. You may not set variables inside of it, nor may do any kind of program flow control (ifs, loops etc.) nor even calculated values (i.e. 3600 * 24 * 5). Doing so will cause Mozilla to stop processing your preferences file without any notification, warning, error, or exception. Think of it more as an .ini file. Comments are perfectly acceptable.

Inside your file you set preferences using the pref() function:

pref("name", "value")

example:

pref('extensions.defaultPrefs.example.int', 1);
pref('extensions.defaultPrefs.example.float', 0.1);
pref('extensions.defaultPrefs.example.string', 'fadf');
pref('extensions.defaultPrefs.example.bool', true);

Notice that unlike when you're reading preferences, writing default preferences uses the same function no matter the data type of the preference. You simply pass it directly to the function.

Original Document Information

  • Author(s): Tom Aratyn, Security Compass
  • Permission granted to license under the CC:By-SA.

Document Tags and Contributors

 Contributors to this page: GG, Themystic
 Last updated by: GG,