|
1 |
| -const DEFAULT_TIMER = "five"; |
| 1 | +(function() { |
2 | 2 |
|
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"; |
9 | 9 |
|
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 | + } |
27 | 16 |
|
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); |
31 | 34 |
|
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; |
37 | 38 |
|
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 | + } |
50 | 57 |
|
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 | + }; |
56 | 68 |
|
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; |
67 | 76 |
|
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; |
75 | 79 |
|
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; |
77 | 94 | 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"; |
84 | 96 | sidebar.prepend(timer);
|
85 | 97 | chrome.storage.local.set({ [location.href]: duration });
|
86 | 98 | chrome.runtime.sendMessage({ newTimer: true }, function() {
|
87 | 99 | startTimer(duration, timer);
|
88 | 100 | });
|
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; |
93 | 110 | 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 | + } |
95 | 117 | 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); |
113 | 119 | } else {
|
114 |
| - timer.innerText = hours + ":" + minutes + ":" + seconds; |
| 120 | + timer.innerText = duration; |
| 121 | + sidebar.prepend(timer); |
115 | 122 | }
|
116 |
| - sidebar.prepend(timer); |
117 |
| - startTimer(duration, timer); |
118 |
| - } else { |
119 |
| - timer.innerText = duration; |
120 |
| - sidebar.prepend(timer); |
| 123 | + |
| 124 | + return; |
121 | 125 | }
|
| 126 | + }); |
122 | 127 |
|
123 |
| - return; |
124 |
| - } |
125 |
| -}); |
| 128 | +})() |
0 commit comments