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);
|
|
|
|
|
2009-02-12 20:41:15 -05:00
|
|
|
lisence: The MIT License, Copyright (c) 2009 SHIMODA "Piro" Hiroshi
|
|
|
|
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
|
2009-02-12 20:41:15 -05:00
|
|
|
*/
|
|
|
|
(function() {
|
2009-07-07 20:09:13 -04:00
|
|
|
const currentRevision = 2;
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
window['piro.sakura.ne.jp'].prefs = {
|
|
|
|
revision : currentRevision,
|
|
|
|
|
|
|
|
get Prefs()
|
|
|
|
{
|
|
|
|
if (!this._Prefs) {
|
|
|
|
this._Prefs = Components.classes['@mozilla.org/preferences;1'].getService(Components.interfaces.nsIPrefBranch);
|
|
|
|
}
|
|
|
|
return this._Prefs;
|
|
|
|
},
|
|
|
|
_Prefs : null,
|
2009-07-07 20:09:13 -04:00
|
|
|
|
|
|
|
get DefaultPrefs()
|
|
|
|
{
|
|
|
|
if (!this._DefaultPrefs) {
|
|
|
|
this._DefaultPrefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces.nsIPrefService).getDefaultBranch(null);
|
|
|
|
}
|
|
|
|
return this._DefaultPrefs;
|
|
|
|
},
|
|
|
|
_DefaultPrefs : 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;
|
2009-02-12 20:41:15 -05:00
|
|
|
try {
|
2009-07-07 20:09:13 -04:00
|
|
|
switch (aBranch.getPrefType(aPrefstring))
|
2009-02-12 20:41:15 -05:00
|
|
|
{
|
|
|
|
case this.Prefs.PREF_STRING:
|
2009-07-07 20:09:13 -04:00
|
|
|
return decodeURIComponent(escape(aBranch.getCharPref(aPrefstring)));
|
2009-02-12 20:41:15 -05:00
|
|
|
break;
|
|
|
|
case this.Prefs.PREF_INT:
|
2009-07-07 20:09:13 -04:00
|
|
|
return aBranch.getIntPref(aPrefstring);
|
2009-02-12 20:41:15 -05:00
|
|
|
break;
|
|
|
|
default:
|
2009-07-07 20:09:13 -04:00
|
|
|
return aBranch.getBoolPref(aPrefstring);
|
2009-02-12 20:41:15 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch(e) {
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
},
|
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;
|
|
|
|
|
2009-02-12 20:41:15 -05:00
|
|
|
var type;
|
|
|
|
try {
|
|
|
|
type = typeof aNewValue;
|
|
|
|
}
|
|
|
|
catch(e) {
|
|
|
|
type = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case 'string':
|
2009-07-07 20:09:13 -04:00
|
|
|
aBranch.setCharPref(aPrefstring, unescape(encodeURIComponent(aNewValue)));
|
2009-02-12 20:41:15 -05:00
|
|
|
break;
|
|
|
|
case 'number':
|
2009-07-07 20:09:13 -04:00
|
|
|
aBranch.setIntPref(aPrefstring, parseInt(aNewValue));
|
2009-02-12 20:41:15 -05:00
|
|
|
break;
|
|
|
|
default:
|
2009-07-07 20:09:13 -04:00
|
|
|
aBranch.setBoolPref(aPrefstring, aNewValue);
|
2009-02-12 20:41:15 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
},
|
2009-07-07 20:09:13 -04:00
|
|
|
|
|
|
|
setDefaultPref : function(aPrefstring)
|
|
|
|
{
|
|
|
|
return this.setPref(aPrefstring, aNewValue, this.DefaultPrefs);
|
|
|
|
},
|
2009-02-12 20:41:15 -05:00
|
|
|
|
|
|
|
clearPref : function(aPrefstring)
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
this.Prefs.clearUserPref(aPrefstring);
|
|
|
|
}
|
|
|
|
catch(e) {
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
},
|
|
|
|
|
|
|
|
addPrefListener : function(aObserver)
|
|
|
|
{
|
|
|
|
var domains = ('domains' in aObserver) ? aObserver.domains : [aObserver.domain] ;
|
|
|
|
try {
|
|
|
|
var pbi = this.Prefs.QueryInterface(Components.interfaces.nsIPrefBranchInternal);
|
|
|
|
for (var i = 0; i < domains.length; i++)
|
|
|
|
pbi.addObserver(domains[i], aObserver, false);
|
|
|
|
}
|
|
|
|
catch(e) {
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
removePrefListener : function(aObserver)
|
|
|
|
{
|
|
|
|
var domains = ('domains' in aObserver) ? aObserver.domains : [aObserver.domain] ;
|
|
|
|
try {
|
|
|
|
var pbi = this.Prefs.QueryInterface(Components.interfaces.nsIPrefBranchInternal);
|
|
|
|
for (var i = 0; i < domains.length; i++)
|
|
|
|
pbi.removeObserver(domains[i], aObserver, false);
|
|
|
|
}
|
|
|
|
catch(e) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
})();
|