-
Notifications
You must be signed in to change notification settings - Fork 28
/
background.js
40 lines (39 loc) · 1.47 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
chrome.browserAction.onClicked.addListener(function (tab) {
var showingRealNames = localStorage['showingRealNames'];
showingRealNames = showingRealNames && JSON.parse(showingRealNames);
if (showingRealNames === undefined) {
showingRealNames = true;
}
showingRealNames = !showingRealNames;
localStorage['showingRealNames'] = JSON.stringify(showingRealNames);
chrome.tabs.sendMessage(tab.id, {action: 'toggle', showingRealNames: showingRealNames });
});
var ONE_WEEK = 7 * 24 * 60 * 60 * 1000;
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
if (request.action == "get-showingRealNames") {
var showingRealNames = localStorage['showingRealNames'];
showingRealNames = showingRealNames && JSON.parse(showingRealNames);
if (showingRealNames === undefined) {
showingRealNames = true;
}
sendResponse({showingRealNames: showingRealNames});
}
if (request.action === 'get-real-name') {
var cached = localStorage['user:' + request.username];
if (cached) {
cached = JSON.parse(cached);
if (cached.timestamp > Date.now() - ONE_WEEK) {
sendResponse({cached: cached.realName});
} else {
sendResponse({cached: null});
}
}
}
if (request.action === 'set-real-name') {
localStorage['user:' + request.username] = JSON.stringify({
realName: request.realName,
timestamp: Date.now()
});
sendResponse({});
}
});