- Extend Firefox!
- Development Overview
- Environment Setup
- Configuration Files
- Creating GUIs
- The Backend
- Setting Defaults
- Creating a Preferences Pane
- Making XUL Elements bind to Preferences
- More Difficult Preferences interfaces
- Further Reading
In many cases your extension will need to be able to store data or simple user preferences for future use. In the Home Page Scheduler, for example, the Default Home Page URL and the scheduled Home Pages all need to be saved somewhere so that each time the user opens Firefox the data is available.
Firefox provides you with the Preferences System for these tasks. To see all of the currently stored preferences in your Firefox, type
about:config into the location bar and press return. This page will show you a listing of all the current preferences and their values. For example,
browser.startup.homepage tells the browser what page to load when the user wishes to visit their home page.
A preference can be an
The preference strings that you create for your extension should follow a simple naming convention. Here is the preference that holds the Default Home Page for our extension:
Begin your strings with ‘extensions’, followed by your package name, and then the rest can be whatever makes the most sense to you. This just makes sure no two extensions want to create the same preference string for two different reasons.
In most cases you will want to set what should be the default setting for a preference. For example, in the Home Page Scheduler www.google.com is the default startupHomePage if the user does not provide one. You may create these default preferences in a file within your defaults/preferences directory (you created this directory earlier). All
.js files within this directory will be loaded by the Preference System when necessary, so the name of your file does not matter. We chose to use our package name:
Within this file you may add lines like this:
Notice that, by default, no scheduled events are created, so we just initialized this to an empty string.
Now that you have access to the preference manager you can get and set preferences using
setIntPref()/setBoolPref()/setCharPref(). These functions are very straight forward and have been used in the code samples above.
Creating a Preferences Pane
Before we begin using our preference strings to store data, let’s learn how to create a Preferences pane. Select Options from the Tools menu in Firefox to open up your preferences. (Note: options and preferences means the same thing in Firefox). You should see General, Privacy, Content, Download, Advanced, and maybe other tabs. If you would like to have a tab for your extension preferences, you must create an XUL overlay onto the
preferences.xul file that contains your pane information.
The code below shows how to create an XUL overlay for your pane:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
Notice that the merge point is
<prefwindow id="BrowserPreferences">. If you did not know what you want to merge with, the DOM Inspector is your best tool for looking through browser code and finding element IDs. The
<prefpane> tag requires an id, label, and image attributes. This image will show up in the Options window. The
onpaneload attribute allows you to run code when the options pane is first loaded.
Here is a screenshot of the Home Page Scheduler Preference Pane. Notice that
textboxes, and all other GUI elements work just fine in the Options window.
Making XUL Elements bind to Preferences
There are two steps to making your XUL element interface with a preference string. We will use the Default Home Page text box as an example.
First, within the
<preferences> tag shown in the sample above, you will need to define the preference that will be used:
1 2 3 4 5 6 7
This line creates an element with the defaultStartupHomepage DOM ID, and associates itself with the extensions.hpsched.defaultHomepage preference string. It also lets the preference system know that the data should be stored as a string (not an int or bool). We can now use this element’s ID in the XUL GUI element:
For simple elements, such as
menulist, all you have to do is add the preference attribute to the element. The Preference System will automatically update the preference string anytime this element is modified! This was a major Firefox 1.5 improvement.
More Difficult Preferences interfaces
In some cases your GUI elements will not have a one-to-one correspondence with the preferences strings. In the Home Page Scheduler, for example, the tree element that holds our scheduled home pages cannot automatically store its data as an int, string, or bool – it is much too complicated. The Firefox Preference Manager provides a way for you to interface with more complicated GUI elements. There are three basic steps:
onpaneload: Add this attribute to your prefpane tag so that a function can be called to initialize your GUI elements. At this point in the code you can access the preferences strings manually and set the GUI elements as desired.
onsynctopreference="return myFunc();": This attribute must also be inserted into the element that will be altering the preference’s value. Anytime the user changes the element, the Preference System will call this function to update the preference string. This function must return the new value of the preference string, which will then be stored immediately.
preference-editable="true": You must add this attribute to the XUL elements that should cause the preference string to be modified. That way, the Preference System can call your update code as soon as the user modifies the element.
Let’s look at the
onpaneload code for the Home Page Scheduler. You will see that it manually retrieves the preference and builds the tree’s child nodes to match the information:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
Here is our tree element in the XUL file:
1 2 3 4
Last we must provide the function that converts the information from the tree into a string suitable for preference storage:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
- Preferences Overview: http://developer.mozilla.org/en/docs/Preferences_System:preference