-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstorage.js
48 lines (40 loc) · 1.05 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
47
48
var storage;
function noop() {}
function getStorage() {
try {
return window.localStorage;
} catch(e) {
return {
getItem: noop,
setItem: noop,
removeItem: noop,
getAllKeys: noop
};
}
}
if (typeof chrome !== 'undefined' && chrome.storage && chrome.storage.local) {
storage = { isChromeStorage: true };
storage.getItem = function (key, callback) {
chrome.storage.local.get(key, function (obj) {
if (obj[key]) callback(null, obj[key]);
else callback(chrome.runtime.lastError, null);
});
};
storage.setItem = function (key, value, callback) {
var obj = {};
obj[key] = value;
chrome.storage.local.set(obj, function () {
if (callback && chrome.runtime.lastError) callback(key);
});
};
storage.removeItem = function(key) {
chrome.storage.local.remove(key);
};
storage.getAllKeys = function (callback) {
chrome.storage.local.get(null, function (obj) {
callback(null, Object.keys(obj));
});
};
}
else storage = getStorage();
module.exports = storage;