-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsourceVariable.js
57 lines (56 loc) · 1.52 KB
/
sourceVariable.js
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
/**
* 现有两个object 都要保存到sourceVariable
* obj1 = {token:["token0", "token1"]}
* obj2 = {token:["token1", "token2"]}
*
* 使用sourceVariable.set("obj*", obj*) 结果是这样
* {
"obj1": "{"token":["token0","token1"]}",
" "obj2": "{"token":["token1", "token2"]}"
* }
*
* 使用sourceVariable.add(obj*) 自动去重数组
* {"token": ["token0", "token1", "token2"]}
*/
/**
* 书源变量扩展 支持键值对保存数据
* set(key, value)
* add(value)
*/
var sourceVariable = {
map: new Map(),
inited: false,
set: function(key, value) {
this.init();
this.map.set(key, value);
this.save()
},
add: function(value) {
if (!value) return;
this.init();
Object.entries(value).forEach(entry => {
let key = entry[0], value = entry[1];
if (value instanceof Array) {
let array = this.map.get(key) || new Array();
let set = new Set(array.concat(value));
this.map.set(key, Array.from(set));
} else {
this.map.set(key, value);
}
});
this.save();
},
init: function() {
if (this.inited) return;
this.inited = true;
let string = source.getVariable();
if (!!string) {
try {
this.map = new Map(Object.entries(JSON.parse(string)))
} catch {}
}
},
save: function() {
source.setVariable(JSON.stringify(Object.fromEntries(this.map)))
}
}