-
Notifications
You must be signed in to change notification settings - Fork 8
/
ta_database.js
119 lines (97 loc) · 3.36 KB
/
ta_database.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
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
var ta_database = {
VERSION: '9',
data: null,
_resetData: {
version: '9',
setting: {
pageActionLite: true,
popupType: 'tab',
skipConfirmPaste: false,
onlyCacheFocusElement: false,
showContextMenu: true,
intervalToSave: 2000,
shouldAutoClear: false,
autoClear_day: 0,
autoClear_hour: 0,
autoClear_min: 0
},
exceptions: [
"docs.google.com/spreadsheets",
"slack.com",
"messenger.com"
]
},
_loadFromStorage: () => local.get().then( db_data => {
ta_database.data = db_data;
}),
init: () => ta_database._loadFromStorage().then( async () => {
var me = ta_database;
var add_on_version = me.VERSION;
var current_version = me.data && me.data.version;
if (!current_version) return me.reset();
if (add_on_version == current_version) return Promise.resolve();
await me.updateDatabaseVersion();
// return me.set('version', add_on_version);
}),
updateDatabaseVersion: async () => {
var me = ta_database;
for (var key in me.data) {
if (["version", "setting", "exceptions"].includes(key)) continue;
let { last_modified } = me.data[key];
if (typeof last_modified == 'string') {
// do nothing
}
else if (typeof last_modified == 'object' && last_modified.getTime) {
me.data[key].last_modified = String(last_modified.getTime())
}
else {
me.data[key].last_modified = me.data[key].time;
}
await me.set(key, me.data[key]);
}
if (me.data.version != me.VERSION) {
await me.set('version', me.VERSION);
}
if (me.data.setting == undefined) me.data.setting = me._resetData.setting;
for (var key in me._resetData.setting) {
if (!(key in me.data.setting)) {
me.data.setting[key] = me._resetData.setting[key];
}
}
for (var key in me.data.setting) {
if (!(key in me._resetData.setting)) {
delete me.data.setting[key];
}
}
await me.set('setting', me.data.setting);
if (me.data.exceptions == undefined) {
await me.set('exceptions', me._resetData.exceptions);
}
},
reset: () => local.clear().then( () => {
// reserve setting, clean caches
var { data, _resetData } = ta_database;
var keep_config = Object.assign({}, _resetData);
if (data.setting) keep_config.setting = data.setting;
if (data.exceptions) keep_config.exceptions = data.exceptions;
return local.set(keep_config).then( () => {
ta_database.data = keep_config;
});
}),
remove: key => local.remove(key).then( () => {
delete ta_database.data[key];
}),
set: (name, obj) => {
ta_database.data[name] = obj
var tmp = {};
tmp[name] = obj;
return local.set(tmp);
},
setOptions: config => {
var { setting } = ta_database.data;
var { key, val } = config;
setting[key] = val;
return ta_database.set('setting', setting);
},
print: () => console.log(ta_database.data)
};