Repository navigation
Expand file tree
/
Copy pathbackground.js
More file actions
61 lines (57 loc) · 1.76 KB
/
Copy pathbackground.js
File metadata and controls
61 lines (57 loc) · 1.76 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
let timerInterval = null;
let remainingTime = 25 * 60; // 25 minutes default
let isRunning = false;
function startTimer() {
if (isRunning) return;
isRunning = true;
timerInterval = setInterval(() => {
if (remainingTime > 0) {
remainingTime--;
chrome.storage.local.set({ remainingTime });
} else {
clearInterval(timerInterval);
timerInterval = null;
isRunning = false;
remainingTime = 25 * 60;
chrome.storage.local.set({ remainingTime });
// todo: send notif
}
}, 1000);
}
function stopTimer() {
if (timerInterval) {
clearInterval(timerInterval);
timerInterval = null;
}
isRunning = false;
}
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.action === "start") {
startTimer();
sendResponse({ success: true, remainingTime, isRunning });
} else if (request.action === "stop") {
stopTimer();
sendResponse({ success: true, isRunning });
} else if (request.action === "reset") {
stopTimer();
remainingTime = 25 * 60;
chrome.storage.local.set({ remainingTime });
sendResponse({ success: true, remainingTime, isRunning });
} else if (request.action === "getState") {
sendResponse({ remainingTime, isRunning });
} else if (request.action === "setPreset") {
stopTimer();
remainingTime = request.minutes * 60;
chrome.storage.local.set({ remainingTime });
sendResponse({ success: true, remainingTime, isRunning });
}
return true;
});
// on service worker open, load timer
chrome.storage.local.get(["remainingTime"], (result) => {
if (result.remainingTime !== undefined) {
remainingTime = result.remainingTime;
} else {
remainingTime = 25 * 60;
}
});