-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
53 lines (50 loc) · 1.84 KB
/
background.js
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
var status = 0;
console.log("20/20/20 background process started..")
chrome.alarms.create({ periodInMinutes: 20 });
chrome.action.setBadgeText({ text: "ON" });
chrome.action.setBadgeBackgroundColor({ color: "blue" })
chrome.action.onClicked.addListener(function (tab) {
console.log("20/20/20 button clicked..")
status += 1;
console.log("status: ", status)
badge_text = status % 2 != 0 ? "OFF" : "ON"
badge_color = status % 2 != 0 ? "yellow" : "blue"
chrome.action.setBadgeText({ text: badge_text });
chrome.action.setBadgeBackgroundColor({ color: badge_color })
if (status % 2 != 0) {
console.log("clearing alarms and notifications..");
chrome.alarms.clearAll();
chrome.notifications.getAll((items) => {
if (items) {
for (let key in items) {
chrome.notifications.clear(key);
}
}
});
} else {
chrome.alarms.create({ periodInMinutes: 20 });
}
});
chrome.alarms.onAlarm.addListener(() => {
chrome.notifications.create({
type: 'basic',
title: '20/20/20',
iconUrl: 'images/202020.jpg',
message: " Take a 20-second break and focus your eyes on something at least 20 feet away.",
priority: 0
},
function () {
console.log("notification running, playing sound..");
chrome.offscreen.hasDocument().then(function (has) {
if (!has) {
try {
chrome.offscreen.createDocument({
url: chrome.runtime.getURL('notification.html'),
reasons: ['AUDIO_PLAYBACK'],
justification: 'Play 202020 notification sound',
});
} catch (e) { }
}
});
});
});