Using content preferences

Firefox 3 introduces the concept of content preferences. This permits code running within chrome (in other words: extensions and the browser itself, not web sites) to locally save preferences on a per-site basis. This makes it possible to write an extension that lets the user customize the appearance of specific web sites (setting the font size larger on sites that use obnoxiously small fonts, for instance).

The content preferences service, implemented by nsIContentPrefService, offers functions for setting and retrieving preferences for specific sites or in the global preference space; global preferences are used whenever a site-specific preference isn't available.

Site-specific content preferences are stored in the profile folder, in the database file "content-prefs.sqlite".

Example: Setting and retrieving preferences

This example demonstrates how to save a preference and then retrieve its value.

var ioSvc = Components.classes["@mozilla.org/network/io-service;1"]
            .getService(Components.interfaces.nsIIOService);
var prefService = Components.classes["@mozilla.org/content-pref/service;1"]
                  .getService(Components.interfaces.nsIContentPrefService);
// Create a URI object referencing the site to save a preference for
var uri = ioSvc.newURI("http://developer.mozilla.org/", null, null);
// Set the value of the "devmo.somesetting" preference to "foo".
prefService.setPref(uri, "devmo.somesetting", "foo");
...
// Retrieve the value of the "devmo.somesetting" preference.
var value = prefService.getPref(uri, "devmo.somesetting");

Built-in site-specific preferences

Preference Name Menu Equivalent Values Notes

browser.content.full-zoom

View / Zoom Example: "1.10000002384186"
(Rounding variant of "1.1")
Related about:config preferences:
  • browser.zoom.full
    Boolean, set by the menu item
    View / Zoom / Zoom Text Only.
    Despite the confusion in names, this preference is not site-specific.
  • browser.zoom.siteSpecific
  • toolkit.zoomManager.zoomValues
  • zoom.maxPercent and zoom.minPercent

browser.download.lastDir

  Path of a filesystem directory Related about:config preferences:
  • browser.download.lastDir
    The last directory for any site

Use DownloadLastDir.jsm for access to these preferences.

browser.upload.lastDir
  Path of a filesystem directory This preference is stored and retrieved automatically by file upload controls.
spellcheck.lang   Language code (e.g., "en-US")  
(Firefox 9.0 / Thunderbird 9.0 / SeaMonkey 2.6)

Private browsing

Prior to Gecko 9.0 (Firefox 9.0 / Thunderbird 9.0 / SeaMonkey 2.6), the content preference service always stores preferences on disk. Because of this, in private browsing mode, use of the content preference service needed to be avoided while in private browsing mode; instead, information needed to be stored in memory or preferences had to be avoided. Starting in Gecko 9.0, when in private browsing mode, the content preference service stores preferences in memory instead of on disk, and automatically forgets them when leaving private browsing mode.

See also

Document Tags and Contributors

Tags: 
 Contributors to this page: Brettz9, ethertank, Sheppy, arenevier, darktrojan, kitchin, Mgjbot
 Last updated by: Brettz9,