-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
58 lines (48 loc) · 1.41 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
function countdown() {
const s = parseInt(localStorage.getItem('sec'),10)
const m = parseInt(localStorage.getItem('min'),10)
const h = parseInt(localStorage.getItem('hrs'),10)
if (s <= 0) {
localStorage.setItem('sec', 0)
} else {
localStorage.setItem('sec', s-1)
}
if(m > 0 && s == 0){
localStorage.setItem('sec', 59)
localStorage.setItem('min', m-1)
return;
}
if (h > 0 && m == 0 && s == 0){
localStorage.setItem('sec', 59)
localStorage.setItem('min', 59)
localStorage.setItem('hrs', h-1)
return;
}
}
function getTimeLeftMs() {
const s = parseInt(localStorage.getItem('sec'),10)
const m = parseInt(localStorage.getItem('min'),10)
const h = parseInt(localStorage.getItem('hrs'),10)
return (s + (m* 60) + (h* 60)) * 1000
}
function onMessage (message) {
// timer not finished
if (!message.done) {
chrome.alarms.create("onesecond", {when: Date.now() + 1000})
}
}
function checkDone (alarm) {
chrome.alarms.clear("onesecond")
countdown()
const timeLeft = getTimeLeftMs()
if (timeLeft === 0){
chrome.runtime.sendMessage({done: true})
localStorage.setItem('cancel', false)
chrome.tabs.create({url: 'home.html'});
return;
}
chrome.runtime.sendMessage({countdown: true})
chrome.alarms.create("onesecond", {when: Date.now() + 1000})
}
chrome.runtime.onMessage.addListener(onMessage)
chrome.alarms.onAlarm.addListener(checkDone)