This repository has been archived by the owner on May 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
background.js
92 lines (80 loc) · 2.58 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
// globals for easy access by interval'ed callback
var LastIcon = 'default',
notifer,
icon = 'default',
within = '',
group = 'group_1';
function swapIcon() {
var schedule = JSON.parse(localStorage.loshinCache || '[0]')[0],
newDate = new Date(),
today = 'sunday monday tuesday wednesday thursday friday saturday'.split(' ')[newDate.getDay()],
alertThreshold = localStorage.loshinAlertBefore * 1 || 15,
minutes = [],
blackOut, minute = newDate.getHours() * 60 + newDate.getMinutes(),
group = localStorage.loshinMyGroup || 'group_1';
if (!schedule) {
chrome.browserAction.setIcon({
path: 'icons/default.png'
});
chrome.browserAction.setTitle({
title: 'loshin: Could not fetch data, Click to reload'
});
return;
}
blackOut = schedule[group][today];
['morning', 'evening'].forEach(function(shift) {
blackOut[shift].split('-').forEach(function(tym) {
var tyms = tym.split(':');
minutes.push((tyms[0] < 1 ? 24 : tyms[0]) * 60 + tyms[1] * 1);
});
});
// quick fix:
if (minutes[0] > minutes[1]) minutes[1] += 1440;
if (minutes[2] > minutes[3]) minutes[3] += 1440;
if (((diff1 = minutes[0] - minute) >= 0 && diff1 < alertThreshold) ||
((diff1 = minutes[2] - minute) >= 0 && diff1 < alertThreshold)
) icon = 'going';
else if (((diff2 = minutes[1] - minute) >= 0 && diff2 < alertThreshold) ||
((diff2 = minutes[3] - minute) >= 0 && diff2 < alertThreshold)
) icon = 'coming';
else if (minute < minutes[0] || minute > minutes[3] || (minute > minutes[1] && minute < minutes[2]))
icon = 'on';
else icon = 'off';
if (icon == 'going') within = ' within ' + diff1 + ' minute(s)';
else if (icon == 'coming') within = ' within ' + diff2 + ' minute(s)';
else within = '';
chrome.browserAction.setIcon({
path: 'icons/' + icon + '.png'
});
chrome.browserAction.setTitle({
title: 'loshin: Light is ' + icon + within
});
if (icon !== LastIcon) {
clearInterval(notifer);
notifer = null;
notify();
}
function notify() {
LastIcon = icon;
// revoke as per https://developer.mozilla.org/en/docs/Web/API/notification
var notif = new Notification(
new Date().toLocaleTimeString().replace(/:\d+ /, ' '), {
icon: 'icons/' + icon + '.png',
body: 'loshin: Light is ' + icon + within
}
);
}
if (!notifer) {
var inter = localStorage.loshinNoticeInterval;
notifer = setInterval(
function() {
notify();
}, (inter || 30) * 60000
);
}
}
function ping() {
var tymr = setInterval(swapIcon, 10000);
swapIcon();
}
ping();