-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent.js
128 lines (116 loc) · 3.9 KB
/
content.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
(function() {
const DEFAULT_TIMER = "five";
var sidebar = document.getElementById("sidebar");
var timer = document.createElement("div");
timer.classList.add("roundbox");
timer.classList.add("sidebox");
timer.id = "timer";
function saveEndStateAndClearInterval(intervalId, location, display, message) {
chrome.storage.local.set({ [location]: message }, function() {
display.textContent = message;
clearInterval(intervalId);
});
}
function startTimer(duration, display) {
var timer = duration,
hours,
minutes,
seconds;
var intervalId = setInterval(function() {
if (document.getElementsByClassName("verdict-accepted").length) {
saveEndStateAndClearInterval(
intervalId,
location.href,
display,
"Finished"
);
}
hours = parseInt(timer / 3600, 10);
minutes = parseInt((timer % 3600) / 60, 10);
seconds = parseInt((timer % 3600) % 60, 10);
hours = hours < 10 ? "0" + hours : hours;
minutes = minutes < 10 ? "0" + minutes : minutes;
seconds = seconds < 10 ? "0" + seconds : seconds;
if (!hours) {
display.textContent = minutes + ":" + seconds;
} else {
display.textContent = hours + ":" + minutes + ":" + seconds;
}
if (--timer < 0) {
saveEndStateAndClearInterval(
intervalId,
location.href,
display,
"Timed Out"
);
} else {
chrome.storage.local.set({ [location.href]: timer });
}
}, 1000);
}
chrome.storage.local.get(location.href, function(obj) {
if (!obj[location.href]) {
const timerIdToTimer = {
five: 05,
ten: 10,
fifteen: 15,
twenty: 20,
"twenty-five": 25,
thirty: 30
};
chrome.storage.local.get("timer", function(obj) {
var timerId = obj.timer || DEFAULT_TIMER;
if (timerId === "custom") {
chrome.storage.local.get("custom", function(result) {
var hours = result.custom.hours;
var minutes = result.custom.minutes;
var duration = 60 * 60 * hours + 60 * minutes;
hours = hours < 10 ? "0" + hours : hours;
minutes = minutes < 10 ? "0" + minutes : minutes;
if (!hours) {
timer.innerText = minutes + ":00";
} else {
timer.innerText = hours + ":" + minutes + ":00";
}
sidebar.prepend(timer);
chrome.storage.local.set({ [location.href]: duration });
chrome.runtime.sendMessage({ newTimer: true }, function() {
startTimer(duration, timer);
});
});
} else {
var minutes = timerIdToTimer[timerId];
var duration = 60 * minutes;
minutes = minutes < 10 ? "0" + minutes : minutes;
timer.innerText = minutes + ":00";
sidebar.prepend(timer);
chrome.storage.local.set({ [location.href]: duration });
chrome.runtime.sendMessage({ newTimer: true }, function() {
startTimer(duration, timer);
});
}
});
} else {
var duration = obj[location.href];
if (duration !== "Finished" && duration !== "Timed Out") {
hours = parseInt(duration / 3600, 10);
minutes = parseInt((duration % 3600) / 60, 10);
seconds = parseInt((duration % 3600) % 60, 10);
hours = hours < 10 ? "0" + hours : hours;
minutes = minutes < 10 ? "0" + minutes : minutes;
seconds = seconds < 10 ? "0" + seconds : seconds;
if (!hours) {
timer.innerText = minutes + ":" + seconds;
} else {
timer.innerText = hours + ":" + minutes + ":" + seconds;
}
sidebar.prepend(timer);
startTimer(duration, timer);
} else {
timer.innerText = duration;
sidebar.prepend(timer);
}
return;
}
});
})()