forked from magicoflolis/Userscript-Plus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.js
More file actions
121 lines (110 loc) 路 2.82 KB
/
Copy pathconfig.js
File metadata and controls
121 lines (110 loc) 路 2.82 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
'use strict';
const brws = (typeof browser=="undefined"?chrome:browser),
Config = {
configLocalListeners: [],
configSyncListeners: [],
syncDefaults: {
debug: true,
theme: "dark",
sleazyfork: true,
},
cachedSyncConfig: null,
cachedLocalStorage: null,
resetToDefault,
};
function configProxy() {
brws.storage.onChanged.addListener((changes = brws.storage.StorageChange, areaName) => {
if (areaName === "sync") {
for (const key in changes) {
Config.cachedSyncConfig[key] = changes[key].newValue;
}
for (const callback of Config.configSyncListeners) {
callback(changes);
}
} else if (areaName === "local") {
for (const key in changes) {
Config.cachedLocalStorage[key] = changes[key].newValue;
}
for (const callback of Config.configLocalListeners) {
callback(changes);
}
}
});
const syncHandler= {
set(prop,value) {
Config.cachedSyncConfig[prop] = value;
brws.storage.sync.set({
[prop]: value
});
return true;
},
get(obj, prop) {
const data = Config.cachedSyncConfig[prop];
return obj[prop] || data;
},
deleteProperty(prop) {
brws.storage.sync.remove(prop);
return true;
}
};
const localHandler = {
set(prop, value) {
Config.cachedLocalStorage[prop] = value;
brws.storage.local.set({
[prop]: value
});
return true;
},
get(obj, prop) {
const data = Config.cachedLocalStorage[prop];
return obj[prop] || data;
},
deleteProperty(prop) {
brws.storage.local.remove(prop);
return true;
}
};
return {
sync: new Proxy({ handler: syncHandler }, syncHandler),
local: new Proxy({ handler: localHandler }, localHandler)
};
}
async function fetchConfig() {
await Promise.all([new Promise((resolve) => {
brws.storage.sync.get(null, function(items) {
Config.cachedSyncConfig = items;
resolve();
});
}), new Promise((resolve) => {
brws.storage.local.get(null, function(items) {
Config.cachedLocalStorage = items;
resolve();
});
})]);
}
async function setupConfig() {
await fetchConfig();
addDefaults();
const config = configProxy();
Config.config = config.sync;
Config.local = config.local;
}
function addDefaults() {
for (const key in Config.syncDefaults) {
if(!Object.prototype.hasOwnProperty.call(Config.cachedSyncConfig, key)) {
Config.cachedSyncConfig[key] = Config.syncDefaults[key];
}
}
for (const key in Config.localDefaults) {
if(!Object.prototype.hasOwnProperty.call(Config.cachedLocalStorage, key)) {
Config.cachedLocalStorage[key] = Config.localDefaults[key];
}
}
}
function resetToDefault() {
brws.storage.sync.set({
...Config.syncDefaults,
});
}
setupConfig();
export default Config;