forked from icer6683/extension
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
77 lines (73 loc) · 1.9 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
let timerTime;
let timerID;
var timeRemaining;
let seconds;
let restTime;
let smallRoundsLeft;
let roundsLeft;
let study = true;
let pause = false;
chrome.runtime.onConnect.addListener(function (port1) {
port1.onMessage.addListener(function (msg1) {
if (msg1.start === "initiate") {
seconds = msg1.time;
restTime = msg1.restTime;
smallRoundsLeft = (2 * msg1.rounds) - 1;
roundsLeft = msg1.rounds;
}
if (msg1.cmd === "done or not") {
roundChanger();
if (smallRoundsLeft == 0) {
port1.postMessage({ msg: "done" });
} else {
port1.postMessage({ msg: "not yet" });
}
} else if (msg1.cmd === "give time") {
timerMaker(seconds);
} else if (msg1.cmd === "get the time") {
port1.postMessage({ timeLeft: timerTime, checkPause: pause, roundsLeft: roundsLeft, study: study });
} else if (msg1.cmd === "pause") {
pause = true;
if (timerID) {
clearTimeout(timerID);
}
timeRemaining = msg1.timeNow;
} else if (msg1.cmd === "start again") {
if (pause === true) {
timerTime = new Date(new Date().getTime() + timeRemaining * 1000);
}
pause = false;
} else if (msg1.cmd === "cancel") {
if (timerID) {
clearTimeout(timerID);
}
pause = false;
timerTime = null;
smallRoundsLeft = 0;
roundsLeft = 0;
}
})
})
function roundChanger() {
if (study == true) {
study = false;
} else if (study == false) {
study = true;
roundsLeft--;
}
if (timerID) {
clearTimeout(timerID);
}
timerTime = null;
smallRoundsLeft--;
if (smallRoundsLeft > 0) {
timerMaker(study ? seconds : restTime);
}
}
function timerMaker(time) {
timerTime = new Date(new Date().getTime() + (time + 1) * 1000);
if (!timerID) {
timerID = setTimeout(() => { alert("Round Done!"); }, timerTime.getTime() - Date.now());
}
pause = false;
}