Need help understanding Extension Preferences
15 posts • Page 1 of 1
December 1st, 2004, 5:18 pm
Does anyone have any links that show how to handle preferences when building an extension for FireFox?
I know how to access and store preferences: http://www.xulplanet.com/tutorials/xulqa/q_prefs.html but, I am not sure if my extension is supposed to use this. i.e. do i just set my own name space and have my extension's preferences mingle with FireFox's preferences? Also, is there a way to set the default preferences for my extension? I notice that a couple of other extensions have a defaults/preferences/appnameprefs.js file in their install that contains preferences. Does that automatically get parsed on install? (my tests seem to indicate that it doesnt)? thanks for any help or tips... mike chambers
December 2nd, 2004, 3:36 am
Yes, you should use nsIPrefBranch or its JS wrappers, like nsPreferences or prefs utils from JSLib.
Be sure to make your prefs names begin with an unique string, like extensions.myextensionname.* (you should then get an nsIPrefBranch for "extensions.myextensionname."). Prefs from defaults/preferences/appnameprefs.js are parsed into "default" branch (returned by nsIPrefService.getDefaultBranch()). Most people use nsIPrefBranch or its js wrapper, nsPreferences. --------- I think the way prefs are implemented now is not very convenient for JS extensions authors. I'm now trying to write a good wrapper for prefs. (I have already had a few unsuccessful attempts.) (nsPreferences is not very good, because default values for prefs should be passed as params to getPref(), and default branch is not used.) Here's what I have now:
A few comments:
I would like to hear suggestions from other extension devs. It would be nice to have a standart and really convenient prefs wrapper for everybody to use.
December 2nd, 2004, 11:31 am
I don't see that your wrapper offers any advantages over creating an nsIPrefBranch in the usual way, other than the slightly nicer syntax for getting a pref branch (or an object rather like one, anyway). Having built-in try-catch blocks strikes me as a flaw, since, now that extensions can easily provide their own default pref files, a failure to read a pref is probably a programming error.
The following might make a nice minimal wrapper: (i.e. it's just a shorthand way of creating a normal nsIPrefBranch)
(I haven't tested it, but setting __proto__ in a constructor does work for non-XPCOM objects at least.)
December 2nd, 2004, 12:02 pm
Well, here is what I am doing right now:
The gets run whenever the extension loads. Basically, it checks to see if the preferences is in the preferences, if not, it saves the preference to the default value. If I need to reference the preference, it is now stored locally, and I don't have to go back to the preferences system. Of course, this means that if someone changes the prefs in any way aside from my preferences window (such as about:config) the settings won't take affect until a restart. This is a downside, but I wanted to not have to go to the pref system every time i needed a setting as that seemed like it would be a pretty expensive process. I had to use a try / catch because I could not figure out how to check to see if a name / value existed in the preferences. Again, not sure if this is a "good" practice. I am trying to figure out the best way, which is why I started the thread. mike chambers
December 2nd, 2004, 12:08 pm
[quote="asqueella"
Prefs from defaults/preferences/appnameprefs.js are parsed into "default" branch (returned by nsIPrefService.getDefaultBranch()).[/quote] Are these parsed just once? on install? or every time the app starts? Is the main reason for having the default prefs, is to allow the developer seperate the prefs from the actual code? Also, what is this how you would get the branch:
mike chambers
December 2nd, 2004, 3:57 pm
every time app starts, I think.
----- clav: var branch = prefsvc.getBranch("whatever"); branch.getCharPref("whatever"); - doesn't read default prefs, does it? That is my main problem with nsIPrefBranch.
December 3rd, 2004, 4:11 am
oops. Nevermind. It seems to work.
I swear it didn't work last time I tried :/ Anyway, sorry for wasting everybody's time. So mesh, the correct way to use preferences (in Fx/Tb) seems to be this: 1) put default values for all/most of your preferences in xpi/defaults/preferences 2) in the code use nsIPrefService.getBranch("extenions.yourextensionname.") to get nsIPrefBranch object 3) use nsIPrefBranch.get calls without try/catch when you get a pref with specified default value; wrap it in try/catch or use nsPreferences when you get a pref without default value. clav, is this correct?
December 3rd, 2004, 5:24 am
Also you can use a pref listener if you expect the preference to be changed and you need notified of that change.
My Extensions:
<a href="http://forecastfox.mozdev.org">Forecastfox</a> <a href="http://tipbar.mozdev.org">Tip of the Day</a> <a href="http://urlnav.mozdev.org">Location Navigator</a> <a href="http://finder.mozdev.org">Finder</a> <a href="http://rsszilla.mozdev.org">RSSzilla</a>
December 3rd, 2004, 10:56 am
Any chance you have a simple example of that? mike chambers
December 4th, 2004, 8:20 am
Here is a stripped down version of what we're doing in forecastfox.
My Extensions:
<a href="http://forecastfox.mozdev.org">Forecastfox</a> <a href="http://tipbar.mozdev.org">Tip of the Day</a> <a href="http://urlnav.mozdev.org">Location Navigator</a> <a href="http://finder.mozdev.org">Finder</a> <a href="http://rsszilla.mozdev.org">RSSzilla</a>
December 4th, 2004, 12:13 pm
Thanks. I c an't get this to work.
Here is what I am doing:
However, when the last line is executed, the following error is thrown: Error: [Exception... "Could not convert JavaScript argument arg 1 [nsIPrefBranchInternal.addObserver]" nsresult: "0x80570009 (NS_ERROR_XPC_BAD_CONVERT_JS)" location: "JS frame :: chrome://appname/content/appnameOverlay.js :: initializePreferences :: line 583" data: no] Source File: chrome://appname/content/appnameOverlay.js Line: 583 Any thoughts on what is going on? Are there any resources online for figuring this stuff out? I have not been able to find any docs on this. mike chambers
December 5th, 2004, 8:04 am
your problem is in the addObserver. Your observe function needs to be part of an object. Replace the "this" in the addObserver with your object that has an observe function.
My Extensions:
<a href="http://forecastfox.mozdev.org">Forecastfox</a> <a href="http://tipbar.mozdev.org">Tip of the Day</a> <a href="http://urlnav.mozdev.org">Location Navigator</a> <a href="http://finder.mozdev.org">Finder</a> <a href="http://rsszilla.mozdev.org">RSSzilla</a>
December 5th, 2004, 10:44 am
Thanks. This works:
But, to be honest I am not sure why that works, and passing in "this" does not work, as "this" is an object which also contains an observe method. Weird. mike c
December 7th, 2004, 8:54 am
"this" was your initializePreferences function. It did not have an observe function.
My Extensions:
<a href="http://forecastfox.mozdev.org">Forecastfox</a> <a href="http://tipbar.mozdev.org">Tip of the Day</a> <a href="http://urlnav.mozdev.org">Location Navigator</a> <a href="http://finder.mozdev.org">Finder</a> <a href="http://rsszilla.mozdev.org">RSSzilla</a>
December 7th, 2004, 10:02 am
That is interesting. Is that always the case? I expected "this" to refer the the scope that the function exists within, and not the function itself. mike chambers
15 posts • Page 1 of 1
Who is onlineUsers browsing this forum: No registered users and 0 guests |
|