-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopup.js
More file actions
87 lines (78 loc) · 2.91 KB
/
popup.js
File metadata and controls
87 lines (78 loc) · 2.91 KB
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
// html elements
const BUTTON_NAME = "startOrAbortButton";
const REMAINING_TIME = "remainingTime";
const AUTO_START_CHECKBOX = "autoStartCheckbox";
// UI
const QB_ALARM_TITLE = "Quarter Blasted";
const IDLE_TEXT = "Let's blast a quarter!";
const BUTTON_START = "Start";
const BUTTON_ABORT = "Abort";
const QUARTER_LABEL = "quarterLabel"; // used for both the input field ID and the key in browser storage
function pad(value) {
return String(value).padStart(2, "0");
}
async function quarterIsRunning() {
return (await browser.alarms.get(QB_ALARM_TITLE)) || false;
}
document.addEventListener("DOMContentLoaded", async function () {
// TIMER
function updateRemainingTime() {
displayText = IDLE_TEXT;
browser.alarms.get(QB_ALARM_TITLE).then(function (alarm) {
if (alarm) {
const remainingSec = (alarm.scheduledTime - Date.now()) / 1000;
if (remainingSec >= 0) {
const min = Math.floor(remainingSec / 60);
const sec = Math.floor(remainingSec % 60);
displayText = `Remaining time: ${pad(min)}:${pad(sec)}`;
}
}
document.getElementById(REMAINING_TIME).textContent = displayText;
});
}
updateRemainingTime(); // show what's up immediately
setInterval(updateRemainingTime, 1000); // show seconds ticking away
// AUTO START OPTION
const result = await browser.storage.local.get("autoStart");
document.getElementById(AUTO_START_CHECKBOX).checked =
result.autoStart || false;
document
.getElementById(AUTO_START_CHECKBOX)
.addEventListener("change", async function () {
await browser.storage.local.set({
autoStart: this.checked,
});
});
// START-ABORT BUTTON
btn = document.getElementById(BUTTON_NAME);
btn.textContent = (await quarterIsRunning()) ? BUTTON_ABORT : BUTTON_START;
btn.addEventListener("click", async function () {
if (await quarterIsRunning()) {
// abort running quarter
browser.alarms.clear(QB_ALARM_TITLE);
this.textContent = BUTTON_START;
} else {
// start new quarter
browser.alarms.create(QB_ALARM_TITLE, {
delayInMinutes: 15,
});
window.close();
}
});
// QUARTER LABEL
quarterLabel = document.getElementById(QUARTER_LABEL);
quarterLabel.value =
(await browser.storage.local.get(QUARTER_LABEL))[QUARTER_LABEL] || "";
quarterLabel.addEventListener("input", async function () {
await browser.storage.local.set({
[QUARTER_LABEL]: quarterLabel.value,
});
});
// HISTORY
document
.getElementById("historyButton")
.addEventListener("click", function () {
browser.tabs.create({ url: "history.html" });
window.close();
});
});