2009-12-25 11:38:14 -05:00
|
|
|
var EXPORTED_SYMBOLS = ['window'];
|
|
|
|
var window = {};
|
|
|
|
|
2009-02-12 20:41:15 -05:00
|
|
|
/*
|
2009-04-06 22:38:07 -04:00
|
|
|
Preferences Library
|
|
|
|
|
|
|
|
Usage:
|
|
|
|
var value = window['piro.sakura.ne.jp'].prefs.getPref('my.extension.pref');
|
|
|
|
window['piro.sakura.ne.jp'].prefs.setPref('my.extension.pref', true);
|
|
|
|
window['piro.sakura.ne.jp'].prefs.clearPref('my.extension.pref');
|
|
|
|
var listener = {
|
|
|
|
domains : [
|
|
|
|
'browser.tabs',
|
|
|
|
'extensions.someextension'
|
|
|
|
],
|
|
|
|
observe : function(aSubject, aTopic, aData)
|
|
|
|
{
|
|
|
|
if (aTopic != 'nsPref:changed') return;
|
|
|
|
var value = window['piro.sakura.ne.jp'].prefs.getPref(aData);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
window['piro.sakura.ne.jp'].prefs.addPrefListener(listener);
|
|
|
|
window['piro.sakura.ne.jp'].prefs.removePrefListener(listener);
|
|
|
|
|
2010-01-24 23:36:08 -05:00
|
|
|
lisence: The MIT License, Copyright (c) 2009-2010 SHIMODA "Piro" Hiroshi
|
2009-02-12 20:41:15 -05:00
|
|
|
http://www.cozmixng.org/repos/piro/fx3-compatibility-lib/trunk/license.txt
|
|
|
|
original:
|
2009-04-06 22:38:07 -04:00
|
|
|
http://www.cozmixng.org/repos/piro/fx3-compatibility-lib/trunk/prefs.js
|
2010-01-24 23:36:08 -05:00
|
|
|
http://www.cozmixng.org/repos/piro/fx3-compatibility-lib/trunk/prefs.test.js
|
2009-02-12 20:41:15 -05:00
|
|
|
*/
|
|
|
|
(function() {
|
2010-01-24 23:36:08 -05:00
|
|
|
const currentRevision = 4;
|
2009-02-12 20:41:15 -05:00
|
|
|
|
|
|
|
if (!('piro.sakura.ne.jp' in window)) window['piro.sakura.ne.jp'] = {};
|
|
|
|
|
|
|
|
var loadedRevision = 'prefs' in window['piro.sakura.ne.jp'] ?
|
|
|
|
window['piro.sakura.ne.jp'].prefs.revision :
|
|
|
|
0 ;
|
|
|
|
if (loadedRevision && loadedRevision > currentRevision) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-01-24 23:36:08 -05:00
|
|
|
const Cc = Components.classes;
|
|
|
|
const Ci = Components.interfaces;
|
|
|
|
|
2009-02-12 20:41:15 -05:00
|
|
|
window['piro.sakura.ne.jp'].prefs = {
|
|
|
|
revision : currentRevision,
|
|
|
|
|
2010-01-24 23:36:08 -05:00
|
|
|
Prefs : Cc['@mozilla.org/preferences;1']
|
|
|
|
.getService(Ci.nsIPrefBranch)
|
|
|
|
.QueryInterface(Ci.nsIPrefBranch2),
|
2009-07-07 20:09:13 -04:00
|
|
|
|
2010-01-24 23:36:08 -05:00
|
|
|
DefaultPrefs : Cc['@mozilla.org/preferences-service;1']
|
|
|
|
.getService(Ci.nsIPrefService)
|
|
|
|
.getDefaultBranch(null),
|
2009-02-12 20:41:15 -05:00
|
|
|
|
2009-07-07 20:09:13 -04:00
|
|
|
getPref : function(aPrefstring, aBranch)
|
2009-02-12 20:41:15 -05:00
|
|
|
{
|
2009-07-07 20:09:13 -04:00
|
|
|
if (!aBranch) aBranch = this.Prefs;
|
2010-01-24 23:36:08 -05:00
|
|
|
switch (aBranch.getPrefType(aPrefstring))
|
|
|
|
{
|
|
|
|
case aBranch.PREF_STRING:
|
|
|
|
return decodeURIComponent(escape(aBranch.getCharPref(aPrefstring)));
|
2009-02-12 20:41:15 -05:00
|
|
|
|
2010-01-24 23:36:08 -05:00
|
|
|
case aBranch.PREF_INT:
|
|
|
|
return aBranch.getIntPref(aPrefstring);
|
|
|
|
|
|
|
|
case aBranch.PREF_BOOL:
|
|
|
|
return aBranch.getBoolPref(aPrefstring);
|
|
|
|
|
|
|
|
case aBranch.PREF_INVALID:
|
|
|
|
default:
|
|
|
|
return null;
|
|
|
|
}
|
2009-02-12 20:41:15 -05:00
|
|
|
},
|
2009-07-07 20:09:13 -04:00
|
|
|
|
|
|
|
getDefaultPref : function(aPrefstring)
|
|
|
|
{
|
|
|
|
return this.getPref(aPrefstring, this.DefaultPrefs);
|
|
|
|
},
|
2009-02-12 20:41:15 -05:00
|
|
|
|
2009-07-07 20:09:13 -04:00
|
|
|
setPref : function(aPrefstring, aNewValue, aBranch)
|
2009-02-12 20:41:15 -05:00
|
|
|
{
|
2009-07-07 20:09:13 -04:00
|
|
|
if (!aBranch) aBranch = this.Prefs;
|
2010-01-24 23:36:08 -05:00
|
|
|
switch (typeof aNewValue)
|
2009-02-12 20:41:15 -05:00
|
|
|
{
|
|
|
|
case 'string':
|
2010-01-24 23:36:08 -05:00
|
|
|
return aBranch.setCharPref(aPrefstring, unescape(encodeURIComponent(aNewValue)));
|
|
|
|
|
2009-02-12 20:41:15 -05:00
|
|
|
case 'number':
|
2010-01-24 23:36:08 -05:00
|
|
|
return aBranch.setIntPref(aPrefstring, parseInt(aNewValue));
|
|
|
|
|
2009-02-12 20:41:15 -05:00
|
|
|
default:
|
2010-01-24 23:36:08 -05:00
|
|
|
return aBranch.setBoolPref(aPrefstring, aNewValue);
|
2009-02-12 20:41:15 -05:00
|
|
|
}
|
|
|
|
},
|
2009-07-07 20:09:13 -04:00
|
|
|
|
2010-01-24 23:36:08 -05:00
|
|
|
setDefaultPref : function(aPrefstring, aNewValue)
|
2009-07-07 20:09:13 -04:00
|
|
|
{
|
|
|
|
return this.setPref(aPrefstring, aNewValue, this.DefaultPrefs);
|
|
|
|
},
|
2009-02-12 20:41:15 -05:00
|
|
|
|
|
|
|
clearPref : function(aPrefstring)
|
|
|
|
{
|
2010-01-24 23:36:08 -05:00
|
|
|
if (this.Prefs.prefHasUserValue(aPrefstring))
|
2009-02-12 20:41:15 -05:00
|
|
|
this.Prefs.clearUserPref(aPrefstring);
|
|
|
|
},
|
|
|
|
|
|
|
|
addPrefListener : function(aObserver)
|
|
|
|
{
|
|
|
|
var domains = ('domains' in aObserver) ? aObserver.domains : [aObserver.domain] ;
|
|
|
|
try {
|
2010-01-24 23:36:08 -05:00
|
|
|
for each (var domain in domains)
|
|
|
|
this.Prefs.addObserver(domain, aObserver, false);
|
2009-02-12 20:41:15 -05:00
|
|
|
}
|
|
|
|
catch(e) {
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
removePrefListener : function(aObserver)
|
|
|
|
{
|
|
|
|
var domains = ('domains' in aObserver) ? aObserver.domains : [aObserver.domain] ;
|
|
|
|
try {
|
2010-01-24 23:36:08 -05:00
|
|
|
for each (var domain in domains)
|
|
|
|
this.Prefs.removeObserver(domain, aObserver, false);
|
2009-02-12 20:41:15 -05:00
|
|
|
}
|
|
|
|
catch(e) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
})();
|