-
Notifications
You must be signed in to change notification settings - Fork 72
/
background.js
41 lines (39 loc) · 1.1 KB
/
background.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
// Add event listeners
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
console.log('REQUEST', request.method, request)
if (request.method == "getAllLocalStorage") {
sendResponse({data: localStorage});
}
else if (request.method == "getLocalStorage") {
sendResponse({data: localStorage[request.key]});
}
else if (request.method == "setLocalStorage") {
localStorage[request.key] = request.value;
sendResponse({});
}
else if (request.method == "getUserData") {
var data = getUserData(request.usernames);
sendResponse({ data: data });
}
else {
sendResponse({});
}
});
function getUserData(usernames) {
var results = {};
for (var i = 0; i < usernames.length; i++) {
var key = usernames[i],
value = localStorage[key];
results[key] = value;
}
return results;
}
//expire old entries
(function() {
for (i=0; i<localStorage.length; i++) {
var info = JSON.parse(localStorage[localStorage.key(i)]);
var now = new Date().getTime();
if (now > info.expire)
localStorage.removeItem(localStorage.key(i));
}
});