This repository has been archived by the owner on Apr 19, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 250
/
ConfigStorage.js
51 lines (50 loc) · 1.67 KB
/
ConfigStorage.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
'use strict';
// idea here is to abstract around the use of chrome.storage.local as it functions differently from "localStorage" and IndexedDB
// localStorage deals with strings, not objects, so the objects have been serialized.
var ConfigStorage = {
// key can be one string, or array of strings
get: function(key, callback) {
if (GUI.isChromeApp()) {
chrome.storage.local.get(key,callback);
} else {
//console.log('Abstraction.get',key);
if (Array.isArray(key)) {
var obj = {};
key.forEach(function (element) {
try {
obj = {...obj, ...JSON.parse(window.localStorage.getItem(element))};
} catch (e) {
// is okay
}
});
callback(obj);
} else {
var keyValue = window.localStorage.getItem(key);
if (keyValue) {
var obj = {};
try {
obj = JSON.parse(keyValue);
} catch (e) {
// It's fine if we fail that parse
}
callback(obj);
} else {
callback({});
}
}
}
},
// set takes an object like {'userLanguageSelect':'DEFAULT'}
set: function(input) {
if (GUI.isChromeApp()) {
chrome.storage.local.set(input);
} else {
//console.log('Abstraction.set',input);
Object.keys(input).forEach(function (element) {
var tmpObj = {};
tmpObj[element] = input[element];
window.localStorage.setItem(element, JSON.stringify(tmpObj));
});
}
}
}