-
Notifications
You must be signed in to change notification settings - Fork 419
Expand file tree
/
Copy pathsettings.js
More file actions
679 lines (662 loc) · 19.7 KB
/
Copy pathsettings.js
File metadata and controls
679 lines (662 loc) · 19.7 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
/**
* @file wrap a relatively independent settings storage with its own functions
*/
/** @type {string} */
const storagePrefix = "US_";
/**
* Convert name to storage key
* @param {string} name
* @returns prefixed storage key
*/
const storageKey = (name) => storagePrefix + name.toUpperCase();
/**
* @typedef {"sync"|"local"|"managed"|"session"} PossibleAreas
* @typedef {"sync"|"local"} ParamArea
*
* @param {ParamArea} area - storage area
* @returns Dynamic storage reference
*/
const storageRef = async (area) => {
const storages = {
sync: {
area: "sync",
ref: browser.storage.sync,
},
local: {
area: "local",
ref: browser.storage.local,
},
};
// https://developer.apple.com/documentation/safariservices/safari_web_extensions/assessing_your_safari_web_extension_s_browser_compatibility#3584139
// since storage sync is not implemented in Safari, currently only returns using local storage
if (import.meta.env.BROWSER === "Safari") {
return storages.local;
}
if (area in storages) {
return storages[area];
} else if (area === undefined) {
const key = storageKey("settings_sync");
const result = await browser.storage.local.get(key);
return result?.[key] ? storages.sync : storages.local;
} else {
return Promise.reject(new Error(`invalid area ${area}`));
}
};
/**
* @typedef {Object} Platforms platform availability
* @property {any=} macos - overriding defaults
* @property {any=} ipados - overriding defaults
* @property {any=} ios - overriding defaults
*
* @typedef {"INTERNAL"|"general"|"editor"} Group group name
*
* @typedef {Object} Setting The setting item
* @property {string} name - setting's name
* @property {"string"|"number"|"boolean"|"object"|"array"} type - setting's value type
* @property {boolean=} local - local settings will not be synced
* @property {Array=} values - setting's values list
* @property {any} default - setting's default value
* @property {boolean=} disable - disabled settings not be displayed
* @property {boolean=} protect - protected settings cannot be reset
* @property {boolean=} confirm - double confirmation is required when change
* @property {Platforms=} platforms - platform availability and overriding defaults
* @property {Group} group - setting's group name
* @property {string=} legacy - setting's legacy name
* @property {"Toggle"|"select"|"textarea"=} nodeType - setting's node type
* @property {Object=} nodeClass - node class name with setting's value
*
* @typedef {Setting & {key: string}} SettingWithKey The setting item with storage key
*/
/** @type {Readonly<Setting>} - Read-only setting template and fallback defaults */
const settingDefault = deepFreeze({
name: "setting_default",
type: undefined,
local: false,
values: [],
default: undefined,
disable: false,
protect: false,
confirm: false,
platforms: { macos: undefined, ipados: undefined, ios: undefined },
group: "INTERNAL",
legacy: "",
nodeType: undefined,
nodeClass: {},
});
/** @type {Readonly<Setting[]>} - Read-only settings definition */
const settingsDefinition = /** @type {const} */ [
{
name: "error_native",
type: "object",
local: true,
default: { error: undefined },
group: "INTERNAL",
},
{
name: "legacy_imported",
type: "number",
local: true,
default: 0,
protect: true,
platforms: { macos: undefined },
group: "INTERNAL",
},
{
name: "language_code",
type: "string",
default: "en",
group: "INTERNAL",
legacy: "languageCode",
},
{
name: "settings_sync",
type: "boolean",
local: true,
default: false,
disable: true,
protect: true,
group: "general",
nodeType: "Toggle",
},
{
name: "theme_mode",
type: "string",
values: ["auto", "dark", "light"],
default: "auto",
group: "general",
nodeType: "select",
},
{
name: "global_active",
type: "boolean",
local: true,
default: true,
group: "general",
legacy: "active",
nodeType: "Toggle",
nodeClass: { warn: false },
},
{
name: "augmented_userjs_install",
type: "boolean",
default: true,
group: "general",
nodeType: "Toggle",
},
{
name: "toolbar_badge_count",
type: "boolean",
default: true,
platforms: { macos: true, ipados: false, ios: false },
group: "general",
legacy: "showCount",
nodeType: "Toggle",
},
{
name: "scripts_settings",
type: "object",
default: {},
disable: true,
group: "INTERNAL",
},
{
name: "scripts_update_check_interval",
type: "number",
values: [0, 1, 3, 7, 15, 30],
default: 0,
group: "general",
nodeType: "select",
},
{
name: "scripts_update_check_lasttime",
type: "number",
default: 0,
group: "INTERNAL",
legacy: "lastUpdateCheck",
},
{
name: "scripts_update_automation",
type: "boolean",
default: false,
disable: true,
confirm: true,
group: "general",
nodeType: "Toggle",
nodeClass: { warn: true },
},
{
name: "global_exclude_match",
type: "object",
default: [],
group: "general",
legacy: "blacklist",
nodeType: "textarea",
},
{
name: "editor_list_sort",
type: "string",
values: ["nameAsc", "nameDesc", "lastModifiedAsc", "lastModifiedDesc"],
default: "lastModifiedDesc",
platforms: { macos: undefined },
group: "editor",
legacy: "sortOrder",
nodeType: "select",
},
{
name: "editor_list_descriptions",
type: "boolean",
default: true,
platforms: { macos: undefined },
group: "editor",
legacy: "descriptions",
nodeType: "Toggle",
},
{
name: "editor_close_brackets",
type: "boolean",
default: true,
platforms: { macos: undefined },
group: "editor",
legacy: "autoCloseBrackets",
nodeType: "Toggle",
},
{
name: "editor_auto_hint",
type: "boolean",
default: true,
platforms: { macos: undefined },
group: "editor",
legacy: "autoHint",
nodeType: "Toggle",
},
{
name: "editor_javascript_lint",
type: "boolean",
default: false,
platforms: { macos: undefined },
group: "editor",
legacy: "lint",
nodeType: "Toggle",
},
{
name: "editor_show_whitespace",
type: "boolean",
default: true,
platforms: { macos: undefined },
group: "editor",
legacy: "showInvisibles",
nodeType: "Toggle",
},
{
name: "editor_tab_size",
type: "number",
values: [1, 2, 3, 4, 5, 6, 8, 10, 12],
default: 4,
platforms: { macos: undefined },
group: "editor",
legacy: "tabSize",
nodeType: "select",
},
];
/** @type {Readonly<{[key: string]: SettingWithKey}>} - Read-only settings dictionary */
export const settingsDictionary = deepFreeze(
settingsDefinition.reduce(settingsDefinitionReduceCallbackFn, {}),
);
/**
* populate the settings-define with setting-default
* and convert settings-define to storage-key object
* @param {{[key: string]: SettingWithKey}} settings settings dictionary
* @param {Setting} setting each setting define
* @returns // {US_GLOBAL_ACTIVE: {key: US_GLOBAL_ACTIVE, name: global_active, ... }, ...}
*/
function settingsDefinitionReduceCallbackFn(settings, setting) {
const key = storageKey(setting.name);
settings[key] = { ...settingDefault, ...setting, key };
return settings;
}
/**
* prevent settings define from being modified in any case
* otherwise user settings may be lost in the worst case
* @type {<T>(o: T) => Readonly<T>}
* @param {object} object any object
* @returns {object} deep frozen object
*/
function deepFreeze(object) {
for (const p in object) {
if (typeof object[p] == "object") {
deepFreeze(object[p]);
}
}
return Object.freeze(object);
}
/**
* compatibility polyfill for Safari < 15.4
* @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwn#browser_compatibility}
* @todo remove this polyfill when set safari strict_min_version 15.4
* @see {@link https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/manifest.json/browser_specific_settings#safari_properties}
*/
if (Object.hasOwn === undefined) {
Object.hasOwn = (obj, prop) =>
Object.prototype.hasOwnProperty.call(obj, prop);
}
// export and define the operation method of settings storage
// they are similar to browser.storage but slightly different
/**
* settings.get
* @param {string|string[]} keys key | array of keys | undefined for all
* @typedef FnGetOptions
* @property {ParamArea=} area
* @property {keyof Platforms=} platform
* @param {FnGetOptions} options
* @returns settings object
*/
export async function get(keys = undefined, options = {}) {
let { area, platform } = options;
if (![undefined, "local", "sync"].includes(area)) {
return console.error("Unexpected storage area:", area);
}
if (![undefined, "macos", "ios", "ipados"].includes(platform)) {
return console.error("Unexpected platform:", platform);
}
// validate setting value and fix surprises to default
/** @param {string} key @param {any} val */
const valueFix = (key, val) => {
if (!key || !Object.hasOwn(settingsDictionary, key)) return;
const def =
settingsDictionary[key].platforms[platform] ??
settingsDictionary[key].default;
// check if value type conforms to settings-dictionary
const type = settingsDictionary[key].type;
if (typeof val != type) {
console.warn(
`Unexpected ${key} value type '${typeof val}' should '${type}', fix to default`,
);
return def;
}
// check if value conforms to settings-dictionary
const values = settingsDictionary[key].values;
if (values.length && !values.includes(val)) {
console.warn(
`Unexpected ${key} value '${val}' should one of '${values}', fix to default`,
);
return def;
}
// verified, pass original value
return val;
};
// [single setting]
if (typeof keys == "string") {
const key = storageKey(keys);
// check if key exist in settings-dictionary
if (!Object.hasOwn(settingsDictionary, key)) {
return console.error("unexpected settings key:", key);
}
// check if only locally stored setting
settingsDictionary[key].local === true && (area = "local");
const storage = await storageRef(area);
const result = await storage.ref.get(key);
if (Object.hasOwn(result, key)) return valueFix(key, result[key]);
return (
settingsDictionary[key].platforms[platform] ??
settingsDictionary[key].default
);
}
const complexGet = async (settingsDefault, areaKeys) => {
const storage = await storageRef(area);
let local = {},
sync = {};
if (storage.area === "sync") {
if (areaKeys.sync.length) {
sync = await storage.ref.get(areaKeys.sync);
}
if (areaKeys.local.length) {
local = await browser.storage.local.get(areaKeys.local);
}
} else {
local = await storage.ref.get(areaKeys.all);
}
const result = Object.assign(settingsDefault, local, sync);
// revert settings object property name
return Object.entries(result).reduce((p, c) => {
p[settingsDictionary[c[0]].name] = valueFix(...c);
return p;
}, {});
};
// [muilt settings]
if (Array.isArray(keys)) {
if (!keys.length) {
return console.error("Settings keys empty:", keys);
}
const settingsDefault = {};
const areaKeys = { local: [], sync: [], all: [] };
for (const k of keys) {
const key = storageKey(k);
// check if key exist in settings-dictionary
if (!Object.hasOwn(settingsDictionary, key)) {
return console.error("unexpected settings key:", key);
}
settingsDefault[key] =
settingsDictionary[key].platforms[platform] ??
settingsDictionary[key].default;
// detach only locally stored settings
settingsDictionary[key].local === true
? areaKeys.local.push(key)
: areaKeys.sync.push(key);
// record all keys in case sync storage is not enabled
areaKeys.all.push(key);
}
return complexGet(settingsDefault, areaKeys);
}
// [all settings]
if (typeof keys == "undefined" || keys === null) {
const settingsDefault = {};
const areaKeys = { local: [], sync: [], all: [] };
for (const key of Object.keys(settingsDictionary)) {
settingsDefault[key] =
settingsDictionary[key].platforms[platform] ??
settingsDictionary[key].default;
// detach only locally stored settings
settingsDictionary[key].local === true
? areaKeys.local.push(key)
: areaKeys.sync.push(key);
// record all keys in case sync storage is not enabled
areaKeys.all.push(key);
}
return complexGet(settingsDefault, areaKeys);
}
return console.error("Unexpected keys type:", keys);
}
/**
* settings.set
* @param {object} keys settings object
* @typedef FnSetOptions
* @property {ParamArea=} area
* @param {FnSetOptions} options
*/
export async function set(keys, options = {}) {
const { area } = options;
if (![undefined, "local", "sync"].includes(area)) {
return console.error("unexpected storage area:", area);
}
if (typeof keys != "object") {
return console.error("Unexpected keys type:", keys);
}
if (!Object.keys(keys).length) {
return console.error("Settings object empty:", keys);
}
const areaKeys = { local: {}, sync: {}, all: {} };
for (const k of Object.keys(keys)) {
const key = storageKey(k);
// check if key exist in settings-dictionary
if (!Object.hasOwn(settingsDictionary, key)) {
return console.error("Unexpected settings keys:", key);
}
// check if value type conforms to settings-dictionary
const type = settingsDictionary[key].type;
if (typeof keys[k] != type) {
if (type === "number" && !Number.isNaN(Number(keys[k]))) {
// compatible with string numbers
keys[k] = Number(keys[k]); // still store it as a number type
} else {
return console.error(
`Unexpected ${k} value type '${typeof keys[k]}' should '${type}'`,
);
}
}
// check if value conforms to settings-dictionary
const values = settingsDictionary[key].values;
if (values.length && !values.includes(keys[k])) {
return console.error(
`Unexpected ${k} value '${keys[k]}' should one of '${values}'`,
);
}
// detach only locally stored settings
settingsDictionary[key].local === true
? (areaKeys.local[key] = keys[k])
: (areaKeys.sync[key] = keys[k]);
// record all keys in case sync storage is not enabled
areaKeys.all[key] = keys[k];
}
const storage = await storageRef(area);
// complexSet
try {
if (storage.area === "sync") {
if (Object.keys(areaKeys.sync).length) {
await storage.ref.set(areaKeys.sync);
}
if (Object.keys(areaKeys.local).length) {
await browser.storage.local.set(areaKeys.local);
}
} else {
await storage.ref.set(areaKeys.all);
}
return true;
} catch (error) {
return console.error(error);
}
}
/**
* settings.reset
* reset to default
* @param {string|string[]} keys key | array of keys | undefined for all
* @typedef FnResetOptions
* @property {ParamArea=} area
* @param {FnResetOptions} options
*/
export async function reset(keys = undefined, options = {}) {
let { area } = options;
if (![undefined, "local", "sync"].includes(area)) {
return console.error("unexpected storage area:", area);
}
// [single setting]
if (typeof keys == "string") {
const key = storageKey(keys);
// check if key exist in settings-dictionary
if (!Object.hasOwn(settingsDictionary, key)) {
return console.error("unexpected settings key:", key, keys);
}
// check if key is protected
if (settingsDictionary[key].protect === true) {
return console.error("protected settings key:", key, keys);
}
settingsDictionary[key].local === true && (area = "local");
const storage = await storageRef(area);
return storage.ref.remove(key);
}
const complexRemove = async (areaKeys) => {
const storage = await storageRef(area);
try {
if (storage.area === "sync") {
if (areaKeys.sync.length) {
await storage.ref.remove(areaKeys.sync);
}
if (areaKeys.local.length) {
await browser.storage.local.remove(areaKeys.local);
}
} else {
await storage.ref.remove(areaKeys.all);
}
return true;
} catch (error) {
return console.error(error);
}
};
// [muilt settings]
if (Array.isArray(keys)) {
if (!keys.length) {
return console.error("Settings keys empty:", keys);
}
const areaKeys = { local: [], sync: [], all: [] };
for (const k of keys) {
const key = storageKey(k);
// check if key exist in settings-dictionary
if (!Object.hasOwn(settingsDictionary, key)) {
return console.error("unexpected settings key:", key, k);
}
// check if key is protected
if (settingsDictionary[key].protect === true) {
return console.error("protected settings key:", key, k);
}
// detach only locally stored settings
settingsDictionary[key].local === true
? areaKeys.local.push(key)
: areaKeys.sync.push(key);
// record all keys in case sync storage is not enabled
areaKeys.all.push(key);
}
return complexRemove(areaKeys);
}
// [all settings]
if (typeof keys == "undefined" || keys === null) {
const areaKeys = { local: [], sync: [], all: [] };
for (const key in settingsDictionary) {
// skip protected keys
if (settingsDictionary[key].protect === true) continue;
// detach only locally stored settings
settingsDictionary[key].local === true
? areaKeys.local.push(key)
: areaKeys.sync.push(key);
// record all keys in case sync storage is not enabled
areaKeys.all.push(key);
// clean up the legacy keys at the same time
const legacyKey = settingsDictionary[key].legacy;
legacyKey && areaKeys.all.push(legacyKey);
}
return complexRemove(areaKeys);
}
return console.error("Unexpected keys type:", keys);
}
/**
* complex onChanged
* this function is convenient for the svelte store to update the state
* @callback onChangedSettingsCallback
* @param {{[key: string]: any}} settings - changed settings
* @param {PossibleAreas} area - storage area
* @returns {void}
* @param {onChangedSettingsCallback} callback
*/
export function onChangedSettings(callback) {
if (typeof callback != "function") {
return console.error("Unexpected callback:", callback);
}
console.info("storage onChanged addListener");
/**
* @see {@link https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/storage/onChanged#listener}
* @param {object} changes
* @param {PossibleAreas} area
*/
const listener = (changes, area) => {
// console.log(`storage.${area}.onChanged`, changes); // DEBUG
const settings = {};
for (const key in changes) {
if (!Object.hasOwn(settingsDictionary, key)) continue;
settings[settingsDictionary[key].name] = changes[key].newValue;
}
try {
callback(settings, area);
} catch (error) {
console.error("onChanged callback:", error);
}
};
browser.storage.onChanged.addListener(listener);
}
// the following functions are used only for compatibility transition periods
// these functions will be removed in the future, perhaps in version 5.0
export async function legacyImport() {
// if legacy data has already been imported, skip this process
const imported = await get("legacy_imported");
if (imported) return console.info("Legacy settings has already imported");
// start the one-time import process
const result = await browser.runtime.sendNativeMessage("app", {
name: "PAGE_LEGACY_IMPORT",
});
if (!result) return console.error("PAGE_LEGACY_IMPORT not response");
if (result.error) return console.error(result.error);
console.info("Import settings data from legacy manifest file");
const settings = {};
for (const key of Object.keys(settingsDictionary)) {
const legacy = settingsDictionary[key].legacy;
if (legacy in result) {
let value = result[legacy];
switch (settingsDictionary[key].type) {
case "boolean":
value = JSON.parse(value);
break;
case "number":
value = Number(value);
break;
}
console.info(`Importing legacy setting: ${legacy}`, value);
settings[settingsDictionary[key].name] = value;
}
}
// import complete tag, to ensure will only be import once
Object.assign(settings, { legacy_imported: Date.now() });
if (await set(settings, { area: "local" })) {
console.info("Import legacy settings complete");
// send a message to the Swift layer to safely clean up legacy data
// browser.runtime.sendNativeMessage({name: "PAGE_LEGACY_IMPORTED"});
return true;
}
return console.error("Import legacy settings abort");
}