Revision 133664 of Default Preferences

  • Revision slug: Default_Preferences
  • Revision title: Default Preferences
  • Revision id: 133664
  • Created:
  • Creator: GG
  • Is current revision? Yes
  • Comment 16 words added, 2 words removed

Revision Content

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.

Revision Source

<h2 id="Introduction" name="Introduction">Introduction</h2>
<p>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.</p>
<p>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 <code>user_pref()</code> function). Default preferences are set similarly but inside an extension and use the <code>pref()</code> function.</p>
<h2 id="Setting_default_preferences" name="Setting_default_preferences">Setting default preferences</h2>
<p>While most of an extension's directories can be named arbitrarily and mapped using a <a href="/en/Chrome_Registration" title="en/Chrome_Registration">chrome manifest</a>, default preferences must be in very particular spot:</p>
<pre class="eval">| Extension Root
+--|content
+--|skin
+--|defaults 
   +--|<strong>preferences</strong>

</pre>
<p>To add preferences simply drop a .js file into that directory and Mozilla will read it and set the appropriate preferences.</p>
<p>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 <strong>without any notification, warning, error, or exception</strong>. Think of it more as an .ini file. Comments are perfectly acceptable.</p>
<p>Inside your file you set preferences using the <code>pref()</code> function:</p>
<pre class="eval">pref("name", "value")
</pre>
<p>example:</p>
<pre class="eval">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);

</pre>
<p>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.</p>
<div class="originaldocinfo">
<h3 id="Original_Document_Information" name="Original_Document_Information">Original Document Information</h3>
<ul> <li>Author(s): Tom Aratyn, <a class="external" href="http://www.securitycompass.com">Security Compass</a></li> <li>Permission granted to license under the CC:By-SA.</li>
</ul>
</div>
Revert to this revision