|
1 | | -// // Chrome notification API |
2 | | -const options = { |
3 | | - type: "basic", |
4 | | - title: "Blink Alert! - Its been 20 minutes!", |
5 | | - message: |
6 | | - "Look away from your Computer, Look at something 20 feet away for 20 seconds. (CLICK HERE TO TAKE EXERCISE)", |
7 | | - iconUrl: "images/eyeIcon_128.png", |
8 | | -}; |
9 | | - |
10 | | -let selectedDuration; |
11 | | - |
12 | | -const durationBtn1 = document.getElementById("durationBtn1"); |
13 | | -const durationBtn2 = document.getElementById("durationBtn2"); |
14 | | -const durationBtn3 = document.getElementById("durationBtn3"); |
15 | | - |
16 | 1 | const checkSelectedBtn = (value) => { |
17 | | - if (value === "1200000") { |
| 2 | + if (value === 1200000) { |
18 | 3 | durationBtn1.classList.add("btnSelected"); |
19 | 4 | durationBtn2.classList.remove("btnSelected"); |
20 | 5 | durationBtn3.classList.remove("btnSelected"); |
21 | | - } else if (value === "3600000") { |
| 6 | + } else if (value === 3600000) { |
22 | 7 | durationBtn2.classList.add("btnSelected"); |
23 | 8 | durationBtn1.classList.remove("btnSelected"); |
24 | 9 | durationBtn3.classList.remove("btnSelected"); |
25 | | - } else if (value === "7200000") { |
| 10 | + } else if (value === 7200000) { |
26 | 11 | durationBtn3.classList.add("btnSelected"); |
27 | 12 | durationBtn1.classList.remove("btnSelected"); |
28 | 13 | durationBtn2.classList.remove("btnSelected"); |
29 | 14 | } |
30 | 15 | }; |
31 | 16 |
|
32 | | -chrome.storage.local.get("duration", ({ duration }) => { |
33 | | - selectedDuration = duration || 1200000; |
34 | | - checkSelectedBtn(duration); |
35 | | -}); |
| 17 | +const durationBtn1 = document.getElementById("durationBtn1"); |
| 18 | +const durationBtn2 = document.getElementById("durationBtn2"); |
| 19 | +const durationBtn3 = document.getElementById("durationBtn3"); |
36 | 20 |
|
37 | | -durationBtn1.addEventListener("click", () => { |
38 | | - chrome.storage.local.set({ duration: "1200000" }, () => { |
39 | | - selectedDuration = 1200000; |
40 | | - }); |
41 | | - chrome.storage.local.get("duration", ({ duration }) => { |
42 | | - checkSelectedBtn(duration); |
| 21 | +const setDuration = (value) => { |
| 22 | + chrome.storage.local.set({ duration: value }, () => { |
| 23 | + // Send a message to the background script to update the interval |
| 24 | + chrome.runtime.sendMessage({ updateInterval: true }); |
| 25 | + checkSelectedBtn(value); |
43 | 26 | }); |
44 | | -}); |
| 27 | +}; |
45 | 28 |
|
46 | | -durationBtn2.addEventListener("click", () => { |
47 | | - chrome.storage.local.set({ duration: "3600000" }, () => { |
48 | | - selectedDuration = 3600000; |
49 | | - }); |
| 29 | +document.addEventListener("DOMContentLoaded", () => { |
50 | 30 | chrome.storage.local.get("duration", ({ duration }) => { |
51 | | - checkSelectedBtn(duration); |
| 31 | + setDuration(duration || 1200000); |
52 | 32 | }); |
53 | 33 | }); |
54 | 34 |
|
55 | | -durationBtn3.addEventListener("click", () => { |
56 | | - chrome.storage.local.set({ duration: "7200000" }, () => { |
57 | | - selectedDuration = 7200000; |
58 | | - }); |
59 | | - chrome.storage.local.get("duration", ({ duration }) => { |
60 | | - checkSelectedBtn(duration); |
61 | | - }); |
| 35 | +durationBtn1.addEventListener("click", () => { |
| 36 | + setDuration(1200000); |
62 | 37 | }); |
63 | 38 |
|
64 | | -setInterval(() => { |
65 | | - chrome.notifications.create(options); |
66 | | -}, Number(selectedDuration) || 1200000); |
| 39 | +durationBtn2.addEventListener("click", () => { |
| 40 | + setDuration(3600000); |
| 41 | +}); |
67 | 42 |
|
68 | | -// Create a new tab onclick of notification |
69 | | -chrome.notifications.onClicked.addListener(function () { |
70 | | - chrome.tabs.create({ url: "exercise.html" }); |
| 43 | +durationBtn3.addEventListener("click", () => { |
| 44 | + setDuration(7200000); |
71 | 45 | }); |
0 commit comments