Skip to content

Commit 387c01b

Browse files
committed
Add modules
1 parent ec8c759 commit 387c01b

File tree

3 files changed

+206
-195
lines changed

3 files changed

+206
-195
lines changed

js/background.js

+34-30
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,37 @@
1-
function saveEndStateAndClearInterval(intervalId, location, message) {
2-
chrome.storage.local.set({ [location]: message }, function() {
3-
clearInterval(intervalId);
4-
});
5-
}
6-
7-
function startTimer(duration, location) {
8-
var timer = duration;
9-
var intervalId = setInterval(function() {
10-
if (--timer < 0) {
11-
saveEndStateAndClearInterval(intervalId, location, "Timed Out");
12-
} else {
13-
chrome.storage.local.set({ [location]: timer });
14-
}
15-
}, 1000);
16-
}
1+
(function() {
172

18-
chrome.runtime.onMessage.addListener(function(request) {
19-
if (request.newTimer) {
20-
chrome.storage.local.get(null, function(items) {
21-
Object.entries(items).forEach(function([key, value]) {
22-
if (
23-
key !== "timer" &&
24-
key !== "custom" &&
25-
value !== "Finished" &&
26-
value !== "Timed Out"
27-
) {
28-
startTimer(value, key);
29-
}
30-
});
3+
function saveEndStateAndClearInterval(intervalId, location, message) {
4+
chrome.storage.local.set({ [location]: message }, function() {
5+
clearInterval(intervalId);
316
});
327
}
33-
});
8+
9+
function startTimer(duration, location) {
10+
var timer = duration;
11+
var intervalId = setInterval(function() {
12+
if (--timer < 0) {
13+
saveEndStateAndClearInterval(intervalId, location, "Timed Out");
14+
} else {
15+
chrome.storage.local.set({ [location]: timer });
16+
}
17+
}, 1000);
18+
}
19+
20+
chrome.runtime.onMessage.addListener(function(request) {
21+
if (request.newTimer) {
22+
chrome.storage.local.get(null, function(items) {
23+
Object.entries(items).forEach(function([key, value]) {
24+
if (
25+
key !== "timer" &&
26+
key !== "custom" &&
27+
value !== "Finished" &&
28+
value !== "Timed Out"
29+
) {
30+
startTimer(value, key);
31+
}
32+
});
33+
});
34+
}
35+
});
36+
37+
})()

js/content.js

+107-104
Original file line numberDiff line numberDiff line change
@@ -1,125 +1,128 @@
1-
const DEFAULT_TIMER = "five";
1+
(function() {
22

3-
function saveEndStateAndClearInterval(intervalId, location, display, message) {
4-
chrome.storage.local.set({ [location]: message }, function() {
5-
display.textContent = message;
6-
clearInterval(intervalId);
7-
});
8-
}
3+
const DEFAULT_TIMER = "five";
4+
var sidebar = document.getElementById("sidebar");
5+
var timer = document.createElement("div");
6+
timer.classList.add("roundbox");
7+
timer.classList.add("sidebox");
8+
timer.id = "timer";
99

10-
function startTimer(duration, display) {
11-
var timer = duration,
12-
hours,
13-
minutes,
14-
seconds;
15-
var intervalId = setInterval(function() {
16-
if (document.getElementsByClassName("verdict-accepted").length) {
17-
saveEndStateAndClearInterval(
18-
intervalId,
19-
location.href,
20-
display,
21-
"Finished"
22-
);
23-
}
24-
hours = parseInt(timer / 3600, 10);
25-
minutes = parseInt((timer % 3600) / 60, 10);
26-
seconds = parseInt((timer % 3600) % 60, 10);
10+
function saveEndStateAndClearInterval(intervalId, location, display, message) {
11+
chrome.storage.local.set({ [location]: message }, function() {
12+
display.textContent = message;
13+
clearInterval(intervalId);
14+
});
15+
}
2716

28-
hours = hours < 10 ? "0" + hours : hours;
29-
minutes = minutes < 10 ? "0" + minutes : minutes;
30-
seconds = seconds < 10 ? "0" + seconds : seconds;
17+
function startTimer(duration, display) {
18+
var timer = duration,
19+
hours,
20+
minutes,
21+
seconds;
22+
var intervalId = setInterval(function() {
23+
if (document.getElementsByClassName("verdict-accepted").length) {
24+
saveEndStateAndClearInterval(
25+
intervalId,
26+
location.href,
27+
display,
28+
"Finished"
29+
);
30+
}
31+
hours = parseInt(timer / 3600, 10);
32+
minutes = parseInt((timer % 3600) / 60, 10);
33+
seconds = parseInt((timer % 3600) % 60, 10);
3134

32-
if (!hours) {
33-
display.textContent = minutes + ":" + seconds;
34-
} else {
35-
display.textContent = hours + ":" + minutes + ":" + seconds;
36-
}
35+
hours = hours < 10 ? "0" + hours : hours;
36+
minutes = minutes < 10 ? "0" + minutes : minutes;
37+
seconds = seconds < 10 ? "0" + seconds : seconds;
3738

38-
if (--timer < 0) {
39-
saveEndStateAndClearInterval(
40-
intervalId,
41-
location.href,
42-
display,
43-
"Timed Out"
44-
);
45-
} else {
46-
chrome.storage.local.set({ [location.href]: timer });
47-
}
48-
}, 1000);
49-
}
39+
if (!hours) {
40+
display.textContent = minutes + ":" + seconds;
41+
} else {
42+
display.textContent = hours + ":" + minutes + ":" + seconds;
43+
}
44+
45+
if (--timer < 0) {
46+
saveEndStateAndClearInterval(
47+
intervalId,
48+
location.href,
49+
display,
50+
"Timed Out"
51+
);
52+
} else {
53+
chrome.storage.local.set({ [location.href]: timer });
54+
}
55+
}, 1000);
56+
}
5057

51-
var sidebar = document.getElementById("sidebar");
52-
var timer = document.createElement("div");
53-
timer.classList.add("roundbox");
54-
timer.classList.add("sidebox");
55-
timer.id = "timer";
58+
chrome.storage.local.get(location.href, function(obj) {
59+
if (!obj[location.href]) {
60+
const timerIdToTimer = {
61+
five: 05,
62+
ten: 10,
63+
fifteen: 15,
64+
twenty: 20,
65+
"twenty-five": 25,
66+
thirty: 30
67+
};
5668

57-
chrome.storage.local.get(location.href, function(obj) {
58-
if (!obj[location.href]) {
59-
const timerIdToTimer = {
60-
five: 05,
61-
ten: 10,
62-
fifteen: 15,
63-
twenty: 20,
64-
"twenty-five": 25,
65-
thirty: 30
66-
};
69+
chrome.storage.local.get("timer", function(obj) {
70+
var timerId = obj.timer || DEFAULT_TIMER;
71+
if (timerId === "custom") {
72+
chrome.storage.local.get("custom", function(result) {
73+
var hours = result.custom.hours;
74+
var minutes = result.custom.minutes;
75+
var duration = 60 * 60 * hours + 60 * minutes;
6776

68-
chrome.storage.local.get("timer", function(obj) {
69-
var timerId = obj.timer || DEFAULT_TIMER;
70-
if (timerId === "custom") {
71-
chrome.storage.local.get("custom", function(result) {
72-
var hours = result.custom.hours;
73-
var minutes = result.custom.minutes;
74-
var duration = 60 * 60 * hours + 60 * minutes;
77+
hours = hours < 10 ? "0" + hours : hours;
78+
minutes = minutes < 10 ? "0" + minutes : minutes;
7579

76-
hours = hours < 10 ? "0" + hours : hours;
80+
if (!hours) {
81+
timer.innerText = minutes + ":00";
82+
} else {
83+
timer.innerText = hours + ":" + minutes + ":00";
84+
}
85+
sidebar.prepend(timer);
86+
chrome.storage.local.set({ [location.href]: duration });
87+
chrome.runtime.sendMessage({ newTimer: true }, function() {
88+
startTimer(duration, timer);
89+
});
90+
});
91+
} else {
92+
var minutes = timerIdToTimer[timerId];
93+
var duration = 60 * minutes;
7794
minutes = minutes < 10 ? "0" + minutes : minutes;
78-
79-
if (!hours) {
80-
timer.innerText = minutes + ":00";
81-
} else {
82-
timer.innerText = hours + ":" + minutes + ":00";
83-
}
95+
timer.innerText = minutes + ":00";
8496
sidebar.prepend(timer);
8597
chrome.storage.local.set({ [location.href]: duration });
8698
chrome.runtime.sendMessage({ newTimer: true }, function() {
8799
startTimer(duration, timer);
88100
});
89-
});
90-
} else {
91-
var minutes = timerIdToTimer[timerId];
92-
var duration = 60 * minutes;
101+
}
102+
});
103+
} else {
104+
var duration = obj[location.href];
105+
if (duration !== "Finished" && duration !== "Timed Out") {
106+
hours = parseInt(duration / 3600, 10);
107+
minutes = parseInt((duration % 3600) / 60, 10);
108+
seconds = parseInt((duration % 3600) % 60, 10);
109+
hours = hours < 10 ? "0" + hours : hours;
93110
minutes = minutes < 10 ? "0" + minutes : minutes;
94-
timer.innerText = minutes + ":00";
111+
seconds = seconds < 10 ? "0" + seconds : seconds;
112+
if (!hours) {
113+
timer.innerText = minutes + ":" + seconds;
114+
} else {
115+
timer.innerText = hours + ":" + minutes + ":" + seconds;
116+
}
95117
sidebar.prepend(timer);
96-
chrome.storage.local.set({ [location.href]: duration });
97-
chrome.runtime.sendMessage({ newTimer: true }, function() {
98-
startTimer(duration, timer);
99-
});
100-
}
101-
});
102-
} else {
103-
var duration = obj[location.href];
104-
if (duration !== "Finished" && duration !== "Timed Out") {
105-
hours = parseInt(duration / 3600, 10);
106-
minutes = parseInt((duration % 3600) / 60, 10);
107-
seconds = parseInt((duration % 3600) % 60, 10);
108-
hours = hours < 10 ? "0" + hours : hours;
109-
minutes = minutes < 10 ? "0" + minutes : minutes;
110-
seconds = seconds < 10 ? "0" + seconds : seconds;
111-
if (!hours) {
112-
timer.innerText = minutes + ":" + seconds;
118+
startTimer(duration, timer);
113119
} else {
114-
timer.innerText = hours + ":" + minutes + ":" + seconds;
120+
timer.innerText = duration;
121+
sidebar.prepend(timer);
115122
}
116-
sidebar.prepend(timer);
117-
startTimer(duration, timer);
118-
} else {
119-
timer.innerText = duration;
120-
sidebar.prepend(timer);
123+
124+
return;
121125
}
126+
});
122127

123-
return;
124-
}
125-
});
128+
})()

0 commit comments

Comments
 (0)