1
votes

I have a Firefox extension that implements a bunch of little functions that can be called from a context menu in Firefox. I made a preferences window that shows a radiogroup for each preference, allowing the function to be shown in the main Firefox menu, to be shown in a submenu created by the extension, or to be hidden. This works exactly the same way for each extension function with only the following things changing: the preference id, the preference name, the called function name, and the label of each radio group. Is it possible to create an array containing those four strings for each function and then to define all of the preferences dynamically, rather than copying and pasting the same code for each preference?

I am pretty sure it is possible to define the XUL in my preferences window dynamically. However, I am not sure about defining the default preferences in the defaults folder using the pref() command (I am not sure how to access strings defined somewhere else in the extension). I am also not sure if the definitions of the preferences.xul file can be defined dynamically or if they need to be typed out.

Also, do you think this is a good or bad idea? I was thinking it would make adding/removing functions easier.

1

1 Answers

1
votes

Sounds like a good idea to me. Anything that you can do in a static XUL file you can do dynamically by creating the contents of the XUL window using DOM functions (including the contents of preferences.xul, whatever they may be). As far as default values are concerned, you can set them manually using the preferences API on browser startup if there are no values already set. You can use the observer service to handle the profile-after-change notification. Use the preferences API to check whether your prefs already have a value set and, if not, set them to the default value of your choice.

To check whether a pref already exists, use the appropriate nsIPrefBranch method to try to get it. If an exception is thrown then it doesn't exist.