-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
147 lines (123 loc) · 3.21 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
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
var dict = {
1: "drink_water",
2: "screen_time_check",
3: "pomodoro_method",
4: "sleep-reminder",
5: "drink_water",
6: "screen_time_check",
7: "pomodoro_method",
8: "sleep-reminder",
9: "clearAll"
};
var activityList = Array(6).fill("inactive");
chrome.action.setBadgeText({text: 'OFF'});
chrome.alarms.onAlarm.addListener(
function(alarm){
//determine which alarm is set off
//log to keep track which alarm is set off
var notifTitle;
var notifMsg;
switch(alarm.name){
case "drink_water":
notifTitle = "Stay Hydrated!"
notifMsg = "Remeber to drink your water :)"
break;
case "screen_time_check":
notifTitle = "Screen time reminder"
notifMsg = "You have been using your computer for a while!"
break;
case "pomodoro_method":
notifTitle = "Pomodoro Method: take your 5 minute break now!"
notifMsg = "You have worked for 25 minutes continously, now take a 5 minute break and get your mind back in focus!"
break;
case "sleep-reminder":
notifTitle = "Time to sleep!"
notifMsg = "Right now it is the best for your health to go to sleep"
break;
}
chrome.notifications.create(
{
type :"basic",
iconUrl: "/images/balance128.png",
title: notifTitle,
message: notifMsg,
silent: false
},
() => {}
)
console.log("current alarm: " + alarm.name);
}
)
chrome.runtime.onMessage.addListener(
function (request, sender, sendResponse) {
if (request.time <5){
createAlarm(request.time, request.repeat);
}
else if (request.time >= 5){
stopAlarm(request.time);
}
}
);
function createAlarm(type, num){
chrome.action.setBadgeText({text: 'ON'});
//pomodoro
if (type == 3){
chrome.alarms.create(
dict[type],
{
delayInMinutes: 25,
periodInMinutes: 30
}
)
}
else if (type == 4){
//sleep time reminder
console.log(Date());
//this does the timezone offset
const d = new Date();
let diff = d.getTimezoneOffset()*1000*60;
//how many miliseconds passed in a day
let msPassed = (Date.now())%(1000*3600*24) - diff;
console.log(diff);
console.log(msPassed);
//Use the sleep time to subtract that milisecond
let addedTime = 1000*3600*num - msPassed;
console.log(addedTime);
//if the time already passed
if(addedTime <= 0){
addedTime += 1000*3600*24;
}
console.log(Date.now() + addedTime);
chrome.alarms.create(
"sleep-reminder",
{
when: Date.now() + addedTime,
//a full day: 24 hours => 24 x 60 min = 1440 min
periodInMinutes: 1440
}
)
}
else {
chrome.alarms.create(
dict[type],
{
delayInMinutes: num,
periodInMinutes: num
}
)
}
activityList[type-1] = "active";
}
function stopAlarm(type){
if (dict[type] == "clearAll"){
chrome.alarms.clearAll();
activityList = Array(6).fill("inactive");
}
else {
chrome.alarms.clear(dict[type]);
activityList[(type-5)] = "inactive";
}
if (activityList.findIndex((value)=>value === "active") == -1){
chrome.action.setBadgeText({text: 'OFF'});
}
}