PrefUtils.sys.mjs (2595B)
1 /** 2 * Any copyright is dedicated to the Public Domain. 3 * http://creativecommons.org/publicdomain/zero/1.0/ 4 */ 5 6 export const PrefUtils = { 7 /** 8 * Retrieves the current values of specified preferences. 9 * 10 * Each preference is returned as a tuple with its name and value. If a 11 * preference does not have a user-defined value, its value in the result is 12 * `undefined`. 13 * 14 * @param {Array.<[string, *]>} prefs - An array of tuples where each tuple 15 * contains the preference name (string) and an example value (boolean, 16 * number, or string) to determine its type. 17 * @returns {Array.<[string, *]>} - An array of tuples, each containing the 18 * preference name and its current value or `undefined` if it does not 19 * have a user-defined value. 20 * @throws {Error} If a preference type is unsupported (not boolean, number, 21 * or string). 22 */ 23 getPrefs(prefs) { 24 return prefs.map(([pref, value]) => { 25 if (!Services.prefs.prefHasUserValue(pref)) { 26 return [pref, undefined]; 27 } 28 switch (typeof value) { 29 case "boolean": 30 return [pref, Services.prefs.getBoolPref(pref)]; 31 case "number": 32 return [pref, Services.prefs.getIntPref(pref)]; 33 case "string": 34 return [pref, Services.prefs.getStringPref(pref)]; 35 default: 36 throw new Error("Unsupported pref type!"); 37 } 38 }); 39 }, 40 41 /** 42 * Sets the values of specified preferences. 43 * 44 * Each preference in the input array is updated to the specified value based 45 * on its type. If a preference value is `undefined`, the user-defined value 46 * for that preference is cleared. 47 * 48 * @param {Array.<[string, *]>} prefs - An array of tuples, each containing 49 * the preference name (string) and the desired value (boolean, number, 50 * string, or `undefined`). 51 * - If the value is `undefined`, the user-defined preference is cleared. 52 * @throws {Error} If a preference type is unsupported (not boolean, number, 53 * or string). 54 */ 55 setPrefs(prefs) { 56 for (let [pref, value] of prefs) { 57 if (value === undefined) { 58 Services.prefs.clearUserPref(pref); 59 continue; 60 } 61 62 switch (typeof value) { 63 case "boolean": 64 Services.prefs.setBoolPref(pref, value); 65 break; 66 case "number": 67 Services.prefs.setIntPref(pref, value); 68 break; 69 case "string": 70 Services.prefs.setStringPref(pref, value); 71 break; 72 default: 73 throw new Error("Unsupported pref type!"); 74 } 75 } 76 }, 77 };