• Skip to main content
  • Select language
  • Skip to search
MDN Web Docs
  • Technologies
    • HTML
    • CSS
    • JavaScript
    • Graphics
    • HTTP
    • APIs / DOM
    • WebExtensions
    • MathML
  • References & Guides
    • Learn web development
    • Tutorials
    • References
    • Developer Guides
    • Accessibility
    • Game development
    • ...more docs
Add-ons
  1. MDN
  2. Mozilla
  3. Add-ons
  4. Add-on SDK
  5. High-Level APIs
  6. simple-prefs

simple-prefs

In This Article
  1. Usage
    1. Defining and initializing preferences
      1. Mandatory Common Attributes
      2. Optional Common Attributes
      3. Type-Specific Attributes
      4. Preference Types
      5. Localization
    2. Getting and setting preferences
    3. Simple-prefs in the preferences system
  2. Globals
    1. Functions
      1. on(prefName, listener)
      2. removeListener(prefName, listener)
    2. Properties
      1. prefs

Add-ons using the techniques described in this document are considered a legacy technology in Firefox. Don't use these techniques to develop new add-ons. Use WebExtensions instead. If you maintain an add-on which uses the techniques described here, consider migrating it to use WebExtensions.

From Firefox 53 onwards, no new legacy add-ons will be accepted on addons.mozilla.org (AMO).

From Firefox 57 onwards, WebExtensions will be the only supported extension type, and Firefox will not load other types.

Even before Firefox 57, changes coming up in the Firefox platform will break many legacy extensions. These changes include multiprocess Firefox (e10s), sandboxing, and multiple content processes. Legacy extensions that are affected by these changes should migrate to WebExtensions if they can. See the "Compatibility Milestones" document for more.

A wiki page containing resources, migration paths, office hours, and more, is available to help developers transition to the new technologies.

Experimental

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.

This API is for your add-on's preferences. If you need to get and set the general browser preferences, use preferences/service.

Usage

Defining and initializing preferences

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?"
    }]
}

Each preference is defined by a group of attributes. There are:

  • mandatory attributes that all preferences must have
  • optional attributes
  • attributes that are specific to the preference's data type

Mandatory Common Attributes

These are attributes that all preferences must have.

Attribute Description
type The type of preference, as defined in the "Preference Types" section below.
name

An identifier for the preference. This is used to access the preference from your add-on:

console.log(require("sdk/simple-prefs").prefs.mySettingName);

This means that it must be a valid JavaScript identifier.

title This is used as a label for the preference in the Add-ons Manager user interface.

Optional Common Attributes

These are attributes that all preferences may have:

Attribute Description
description This appears below the preference title in the Add-ons Manager UI.
value A default value for the preference. Depending on the preference type, this may be an integer, string, or boolean value.
hidden

A Boolean value which, if present and set to true, means that the preference won't appear in the Add-ons Manager interface, so users of your add-on won't be able to see or alter it.

{
    "name": "myHiddenInteger",
    "type": "integer",
    "title": "How Many?",
    "hidden": true
}

Your add-on's code will still be able to access and modify it, just like any other preference you define.

Type-Specific Attributes

These are settings that are only applicable to certain preference types. They are documented along with the preference types themselves.

Preference Types

The setting types map to the inline settings types used by the Add-on Manager. All the inline preferences 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 true or false, the "on" or "off" checkbox states are mapped to integers using "on" or "off" properties in the specification.

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 on() function for this preference is called.

This type requires a 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":

var sp = require("sdk/simple-prefs");
sp.on("sayHello", function() {
  console.log("hello");
});

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.

Getting and setting preferences

Unless you've marked them as hidden, the user will be able to see and change the preferences in the Add-on Manager.

You can also see them and change them programmatically using the prefs property, and listen for changes to a preference using on().

Simple-prefs in the preferences system

Preferences defined using simple-prefs are stored in the Firefox preferences system alongside all the other preferences. This means that they're visible in about:config, and they can be accessed using the global simple-preferences module. By default, simple preferences are stored in a preference like:

extensions.<addon-id>.<preference-name>

For example, if you had a simple-pref named "somePreference" then you could get its value like so:

require('sdk/preferences/service').get(['extensions', require('sdk/self').id, 'somePreference'].join('.'))

This would give you the same value as:

require('sdk/simple-prefs').prefs['somePreference']

The ability to change the default preferences branch is new in Add-on SDK 1.15.

The branch of the preferences tree under which simple-prefs are stored is, by default, the add-on ID. Sometimes you might need to use a different branch, especially if you are porting an add-on to the SDK. You can change the sub-branch of extensions using the preferences-branch key in your add-on's package.json file.

Globals

Functions

on(prefName, listener)

Registers an event listener that will be called when a preference is changed.

The event listener may take an optional parameter that specifies the name of the property which changed.

Preference change events are triggered for every character typed by the user. They are also triggered once during add-on initialization.

Example:

function onPrefChange(prefName) {
  console.log("The preference " + 
              prefName + 
              " value has 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);
Parameters

prefName : String
The name of the preference to watch for changes.

listener : Function
The listener function that processes the event.

removeListener(prefName, listener)

Unregisters an event listener for the specified preference.

Parameters

prefName : String
The name of the preference to watch for changes.

listener : Function
The listener function that processes the event.

Properties

prefs

This property is an object containing the simple-prefs you have defined for your add-on. You can use it to access preference values and to change them. Suppose you've defined a preference like this:

  "preferences": [{
      "name": "somePreference",
      "title": "Some preference title",
      "description": "Some short description for the preference",
      "type": "string",
      "value": "this is the default string value"
  }]

You can access somePreference using the prefs property:

var preferences = require("sdk/simple-prefs").prefs;
console.log(preferences.somePreference);
preferences.somePreference = "this is a new value";
console.log(prefs["somePreference"]); // bracket notation
preferences["somePreference"] = "this is the default string value";
console.log: my-addon: this is the default string value
console.log: my-addon: this is a new value

Document Tags and Contributors

Tags: 
  • Add-on SDK
 Contributors to this page: wbamberg, freaktechnik, rolfedh, PushpitaPikuDey, carlin-scott, PixnBits, Null
 Last updated by: wbamberg, Dec 1, 2016, 10:24:45 AM
See also
  1. WebExtensions
  2. Getting started
    1. What are WebExtensions?
    2. Your first WebExtension
    3. Your second WebExtension
    4. Anatomy of a WebExtension
    5. Example WebExtensions
  3. How to
    1. Intercept HTTP requests
    2. Modify a web page
    3. Add a button to the toolbar
    4. Implement a settings page
  4. Concepts
    1. Using the JavaScript APIs
    2. User interface components
    3. Content scripts
    4. Match patterns
    5. Internationalization
    6. Content Security Policy
    7. Native messaging
  5. Porting
    1. Porting a Google Chrome extension
    2. Porting a legacy Firefox add-on
    3. Embedded WebExtensions
    4. Comparison with the Add-on SDK
    5. Comparison with XUL/XPCOM extensions
    6. Chrome incompatibilities
  6. Firefox workflow
    1. Temporary Installation in Firefox
    2. Debugging
    3. Getting started with web-ext
    4. web-ext command reference
    5. WebExtensions and the Add-on ID
    6. Publishing your WebExtension
  7. JavaScript APIs
    1. Browser support for JavaScript APIs
    2. alarms
    3. bookmarks
    4. browserAction
    5. browsingData
    6. commands
    7. contextMenus
    8. contextualIdentities
    9. cookies
    10. downloads
    11. events
    12. extension
    13. extensionTypes
    14. history
    15. i18n
    16. identity
    17. idle
    18. management
    19. notifications
    20. omnibox
    21. pageAction
    22. runtime
    23. sessions
    24. sidebarAction
    25. storage
    26. tabs
    27. topSites
    28. webNavigation
    29. webRequest
    30. windows
  8. Manifest keys
    1. applications
    2. author
    3. background
    4. browser_action
    5. chrome_url_overrides
    6. commands
    7. content_scripts
    8. content_security_policy
    9. default_locale
    10. description
    11. developer
    12. homepage_url
    13. icons
    14. manifest_version
    15. name
    16. omnibox
    17. options_ui
    18. page_action
    19. permissions
    20. short_name
    21. sidebar_action
    22. version
    23. web_accessible_resources
  9. Add-on SDK
  10. Getting started
    1. Installation
    2. Getting started
    3. Troubleshooting
  11. High-Level APIs
    1. addon-page
    2. base64
    3. clipboard
    4. context-menu
    5. hotkeys
    6. indexed-db
    7. l10n
    8. notifications
    9. page-mod
    10. page-worker
    11. panel
    12. passwords
    13. private-browsing
    14. querystring
    15. request
    16. selection
    17. self
    18. simple-prefs
    19. simple-storage
    20. system
    21. tabs
    22. timers
    23. ui
    24. url
    25. webextension
    26. widget
    27. windows
  12. Low-Level APIs
    1. /loader
    2. chrome
    3. console/plain-text
    4. console/traceback
    5. content/content
    6. content/loader
    7. content/mod
    8. content/symbiont
    9. content/worker
    10. core/heritage
    11. core/namespace
    12. core/promise
    13. dev/panel
    14. event/core
    15. event/target
    16. frame/hidden-frame
    17. frame/utils
    18. fs/path
    19. io/byte-streams
    20. io/file
    21. io/text-streams
    22. lang/functional
    23. lang/type
    24. loader/cuddlefish
    25. loader/sandbox
    26. net/url
    27. net/xhr
    28. places/bookmarks
    29. places/favicon
    30. places/history
    31. platform/xpcom
    32. preferences/event-target
    33. preferences/service
    34. remote/child
    35. remote/parent
    36. stylesheet/style
    37. stylesheet/utils
    38. system/child_process
    39. system/environment
    40. system/events
    41. system/runtime
    42. system/unload
    43. system/xul-app
    44. tabs/utils
    45. test/assert
    46. test/harness
    47. test/httpd
    48. test/runner
    49. test/utils
    50. ui/button/action
    51. ui/button/toggle
    52. ui/frame
    53. ui/id
    54. ui/sidebar
    55. ui/toolbar
    56. util/array
    57. util/collection
    58. util/deprecate
    59. util/list
    60. util/match-pattern
    61. util/object
    62. util/uuid
    63. window/utils
  13. Firefox for Android
  14. Getting started
    1. Walkthrough
    2. Debugging
    3. Code snippets
  15. APIs
    1. Accounts.jsm
    2. BrowserApp
    3. HelperApps.jsm
    4. Home.jsm
    5. HomeProvider.jsm
    6. JavaAddonManager.jsm
    7. NativeWindow
    8. Notifications.jsm
    9. PageActions.jsm
    10. Prompt.jsm
    11. RuntimePermissions.jsm
    12. Snackbars.jsm
    13. Sound.jsm
    14. Tab
  16. Legacy
  17. Restartless extensions
    1. Overview
  18. Overlay extensions
    1. Overview
  19. Themes
  20. Lightweight themes
    1. Overview
  21. Complete themes
    1. Overview
  22. Publishing add-ons
  23. Guides
    1. Signing and distribution overview
    2. Submit an add-on
    3. Review policies
    4. Developer agreement
    5. Featured add-ons
    6. Contact addons.mozilla.org
  24. Community and support
  25. Channels
    1. Add-ons blog
    2. Add-on forums
    3. Stack Overflow
    4. Development newsgroup
    5. IRC Channel