-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathbackground.js
62 lines (54 loc) · 1.81 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
function updateBadge(paused) {
badge_text = paused ? "OFF" : "";
chrome.browserAction.setBadgeText({text: badge_text});
}
function setPaused(paused) {
localStorage.setItem('paused', paused);
updateBadge(paused);
}
chrome.browserAction.onClicked.addListener(function(tab){
localStorage.setItem("lastChangedAt", now());
state = localStorage.getItem('paused') == 'true'
setPaused(!state);
chrome.tabs.update(tab.id, {url: tab.url});
});
chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) {
if (message.name == "isPaused?")
sendResponse({value: localStorage.getItem('paused')});
else if (message.name == "setOptions") {
localStorage.setItem('options', message.options);
checkForRandomSwap();
handleNewOptions(JSON.parse(message.options));
}
});
chrome.runtime.onInstalled.addListener(function() {
setPaused(true);
});
var ONE_DAY = 1000 * 60 * 60 * 24;
//returns the current time in millis since epoch
function now() {
return +new Date();
}
var alreadyQueued = false;
function checkForRandomSwap() {
var options = JSON.parse(localStorage.getItem('options'));
if (!options.checkDaily) return;
var lastChangedAt = parseInt(localStorage.getItem("lastChangedAt"), 10);
//if we've never changed it, or if it's been over a day since it was changed
if (isNaN(lastChangedAt) || lastChangedAt + ONE_DAY < now()) {
lastChangedAt = now();
var paused = Math.random() > 0.5;
setPaused(paused);
localStorage.setItem("lastChangedAt", lastChangedAt);
}
if (!alreadyQueued) {
var time_until_next_check = (lastChangedAt + ONE_DAY) - now();
alreadyQueued = true;
setTimeout(function() {
alreadyQueued = false;
checkForRandomSwap();
}, time_until_next_check);
}
}
updateBadge(localStorage.getItem('paused') == true);
checkForRandomSwap();