-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstorage.js
46 lines (37 loc) · 1.11 KB
/
storage.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
let syncWithChrome = localStorage.getItem('syncWithChrome') === true;
if (syncWithChrome) {
console.log('Chrome Sync On');
}
export function getPreference(preference) {
return localStorage.getItem(preference);
}
export function getPreferences(preferences) {
let result = {};
preferences.forEach((preference) => {
result[preference] = getPreference(preference);
});
if (result === 'true' || result === 'false') {
result = result === 'true';
}
return result;
}
export function setPreference(preference, value) {
localStorage.setItem(preference, value);
if (syncWithChrome) {
chrome.storage.sync.set({ [preference]: value });
}
}
export function setPreferences(preferences) {
preferences.forEach((preference) => {
setPreference(preference, preferences[preference]);
});
}
export function toggleSyncWithChrome() {
syncWithChrome = !syncWithChrome;
localStorage.setItem('syncWithChrome', syncWithChrome);
if (syncWithChrome) {
chrome.storage.sync.get(null, (items) => {
setPreferences(items);
});
}
}