-
Notifications
You must be signed in to change notification settings - Fork 2
/
storage.js
100 lines (93 loc) · 2.44 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
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
var mystorage = {};
(function(){
if (typeof chrome !== 'undefined')
{
mystorage.all = function(callback) {chrome.storage.sync.get(null, callback);};
mystorage.get = function(key, callback) {
chrome.storage.sync.get(key, function(item) {
if (key in item)
callback(item[key]);
else
callback();
});
};
mystorage.set = function(data, callback) {chrome.storage.sync.set(data, callback);};
mystorage.remove = function(data, callback) {chrome.storage.sync.remove(data, callback);};
mystorage.clear = function(callback) {chrome.storage.sync.clear(callback);};
}
else
{
//var self = self || addon;
mystorage.lastID = 0;
mystorage.replycallbacks = {};
function onreceive(reply){
mystorage.replycallbacks[reply.id](reply.data);
delete mystorage.replycallbacks[reply.id];
}
function send(message){
if (self.port)
self.port.emit('storage', message);
//else if (self.sendMessage)
// self.sendMessage(message);
else
console.error("Cannot send messages");
}
if (self.port)
self.port.on('storage-reply', onreceive);
//else if (self.onmessage)
// self.onmessage(onreceive);
//else if (self.on)
// self.on(onreceive);
else
console.error("Cannot receive messages");
//mystorage.ss = require("sdk/simple-storage");
mystorage.all = function(callback) {
var id = mystorage.lastID++;
mystorage.replycallbacks[id] = callback;
send({get:null, id:id});
//callback(mystorage.ss.storage);
};
mystorage.get = function(key, callback) {
var id = mystorage.lastID++;
mystorage.replycallbacks[id] = function(reply){
callback(reply[key]);
};
send({get:key, id:id});
/*
mystorage.replycallback
if (key in mystorage.ss.storage)
callback(mystorage.ss.storage[key]);
else
callback();
*/
};
mystorage.set = function (data, callback) {
var id = mystorage.lastID++;
mystorage.replycallbacks[id] = callback;
send({set:data, id:id});
/*
for (var k in data)
mystorage.ss.storage[k] = data[k];
callback();
*/
};
mystorage.remove = function (key, callback) {
var id = mystorage.lastID++;
mystorage.replycallbacks[id] = callback;
send({remove:key, id:id});
/*
if (key instanceof Array)
{
for (var k in key)
delete mystorage.ss.storage[k];
}
else
delete mystorage.ss.storage[key];
callback();
*/
};
mystorage.clear = function(callback) {
console.error("Clearing storage not implemented");
};
}
})();