/
prefs.es
57 lines (49 loc) · 1.54 KB
/
prefs.es
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
const prefs = Components.classes['@mozilla.org/preferences-service;1']
.getService(Components.interfaces.nsIPrefService).getBranch('');
function prefixPref(pref, prefix = 'extensions.cliqz.') {
return `${prefix}${pref}`;
}
export function getPref(key, defaultValue, prefix) {
const pref = prefixPref(key, prefix);
try {
switch (prefs.getPrefType(pref)) {
case 128: return prefs.getBoolPref(pref);
case 32: {
let charVal = prefs.getCharPref(pref);
// it might be a complex value
if (charVal === 'chrome://global/locale/intl.properties') {
try {
charVal = prefs.getComplexValue(
pref,
Components.interfaces.nsIPrefLocalizedString
).data;
} catch (e) {
CLIQZEnvironment.log(`Error fetching pref: ${pref}`);
}
}
return charVal;
}
case 64: return prefs.getIntPref(pref);
default: return defaultValue;
}
} catch (e) {
return defaultValue;
}
}
export function setPref(key, value, prefix) {
const pref = prefixPref(key, prefix);
switch (typeof value) {
case 'boolean': prefs.setBoolPref(pref, value); break;
case 'number': prefs.setIntPref(pref, value); break;
case 'string': prefs.setCharPref(pref, value); break;
default: break;
}
}
export function hasPref(key, prefix) {
const pref = prefixPref(key, prefix);
return prefs.getPrefType(pref) !== 0;
}
export function clearPref(key, prefix) {
const pref = prefixPref(key, prefix);
prefs.clearUserPref(pref);
}