mozilla

Revision 71915 of Using content preferences

  • Revision slug: Using_content_preferences
  • Revision title: Using content preferences
  • Revision id: 71915
  • Created:
  • Creator: Sheppy
  • Is current revision? No
  • Comment /* Example: Setting and retrieving preferences */

Revision Content

{{template.Fx_minversion_header(3)}} 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 {{template.Interface("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.

Example: Setting and retrieving preferences

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);
var uri = ioSvc.newURI("http://developer.mozilla.org/", null, null);
prefService.setPref(uri, "devmo.somesetting", "foo");

...

var value = prefService.getPref(uri, "devmo.somesetting");

Revision Source

<p>{{template.Fx_minversion_header(3)}}
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).
</p><p>The content preferences service, implemented by {{template.Interface("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.
</p>
<h3 name="Example:_Setting_and_retrieving_preferences">Example: Setting and retrieving preferences</h3>
<pre class="eval">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);
var uri = ioSvc.newURI("http://developer.mozilla.org/", null, null);
prefService.setPref(uri, "devmo.somesetting", "foo");

...

var value = prefService.getPref(uri, "devmo.somesetting");
</pre>
Revert to this revision