simple-prefs
The simple-prefs
module lets you store preferences across
application restarts. You can store booleans, integers, and string
values, and users can configure these preferences in the
Add-ons Manager.
This gives users a consistent way to access and modify preferences across different add-ons.
To define preferences and give them initial values, add a new JSON array
called preferences
to your package.json file,
and give it one entry for each preference:
{
"fullName": "Example Add-on",
...
"preferences": [{
"name": "somePreference",
"title": "Some preference title",
"description": "Some short description for the preference",
"type": "string",
"value": "this is the default string value"
},
{
"description": "How many of them we have.",
"name": "myInteger",
"type": "integer",
"value": 8,
"title": "How Many?"
}]
}
Setting Attributes
Mandatory Common Attributes
These are attributes that all settings must have.
Attribute | Description |
---|---|
type |
The type of setting, as defined in the "Setting Types" section below. |
name |
An identifier for the setting. This is used to access the setting from your add-on:
This means that it must be a valid JavaScript identifier. |
title |
This is used as a label for the setting in the Add-ons Manager user interface. |
Optional Common Attributes
These are attributes that all settings may have:
Attribute | Description |
---|---|
description |
This appears below the setting title in the Add-ons Manager UI. |
value |
A default value for the setting. Depending on the setting type, this may be an integer, string, or boolean value. |
Setting-Specific Attributes
These are settings that are only applicable to certain setting types. They are documented along with the setting types themselves.
Setting Types
The setting types map to the inline settings types used by the Add-on Manager. All the inline settings are supported.
Type | Description | Example Specification |
---|---|---|
bool |
Displayed as a checkbox and stores a boolean. | { "description": "Does it have tentacles?", "type": "bool", "name": "hasTentacles", "value": true, "title": "Tentacles" } |
boolint |
Displayed as a checkbox and stores an integer. A boolint is presented to the user as a checkbox,
but instead of storing To provide this mapping the boolint requires two mandatory attributes called "on" and "off", both of which are supplied as strings. Note that even so, the "value" property is supplied as an integer. |
{ "type": "boolint", "name": "myBoolint", "on": "1", "off": "2", "value": 1, "title": "My Boolint" } |
integer |
Displayed as a textbox and stores an integer. | { "description": "How many eyes?", "type": "integer", "name": "eyeCount", "value": 8, "title": "Eye count" } |
string |
Displayed as a textbox and stores a string. | { "type": "string", "name": "monsterName", "value": "Kraken", "title": "Monster name" } |
color |
Displayed as a colorpicker and stores a string
in the #123456 format. |
{ "type": "color", "name": "highlightColor", "value": "#6a5acd", "title": "Highlight color" } |
file |
Displayed as a "Browse" button that opens a file picker and stores the full path and name of the file selected. | { "type": "file", "name": "myFile", "title": "Select a file" } |
directory |
Displayed as a "Browse" button that opens a directory picker and stores the full path and name of the directory selected. | { "type": "directory", "name": "myDirectory", "title": "Select a directory" } |
menulist |
Displayed as a drop-down list. The type of the stored value depends on the default value. The options are specified by a mandatory "options" attribute, that is an array of objects with mandatory attributes "label" and "value"
The values of the "value" attributes must be supplied as strings. The values of the "label" attributes prefixed with "{name}_options.", where {name} is the name of the preference, are used as localization keys. If no matching entries are found, the value of the "label" attributes is used verbatim as labels. |
{ "name": "typeOfBreath", "type": "menulist", "title": "Type of breath", "value": 0, "options": [ { "value": "0", "label": "Fire" }, { "value": "1", "label": "Cold" }, { "value": "2", "label": "Disintegration" } ] } |
radio |
Displayed as radio buttons. The type of the stored value depends on the default value. The options are specified by a mandatory "options" attribute, that is an array of objects with mandatory attributes "label" and "value"
The values of the "value" attributes must be supplied as strings. The values of the "label" attributes prefixed with "{name}_options.", where {name} is the name of the preference, are used as localization keys. If no matching entries are found, the value of the "label" attributes is used verbatim as labels. |
{ "name": "alignment", "type": "radio", "title": "Alignment", "value": "N", "options": [ { "value": "L", "label": "Lawful" }, { "value": "N", "label": "Neutral" }, { "value": "C", "label": "Chaotic" } ] } |
control |
Displays a button. When the user clicks the button,
the function listening to the This type requires an mandatory attribute called "label" which is provided as a string. It is used to label the button. |
In "package.json": { "type": "control", "label": "Click me!", "name": "sayHello", "title": "Say Hello" } In "main.js":
|
Localization
Using the SDK's localization system, you can provide translated forms
of the title
and description
attributes. See the
localization tutorial
for more details.
API Reference
Functions
on(prefName, listener)
experimental Registers an event listener
that will be called when a preference is changed.
Example:
function onPrefChange(prefName) {
console.log("The " + prefName + " preference changed.");
}
require("sdk/simple-prefs").on("somePreference", onPrefChange);
require("sdk/simple-prefs").on("someOtherPreference", onPrefChange);
// `""` listens to all changes in the extension's branch
require("sdk/simple-prefs").on("", onPrefChange);
The name of the preference to watch for changes.
The listener function that processes the event.
removeListener(prefName, listener)
experimental Unregisters an event listener
for the specified preference.
The name of the preference to watch for changes.
The listener function that processes the event.
Properties
prefs : object
experimental A persistent object private to your add-on. Properties with boolean, number, and string values will be persisted in the Mozilla preferences system.