-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
76 lines (66 loc) · 2.1 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
var meeting_detail;
var badge_text_static = "Upcoming Meeting: ";
var badge_show_timer = 0.1;
var badge_remove_timer = 5;
var meeting_url = "";
const showBadge = "showBadge";
const clearBadge = "clearBadge";
const autoJoin = "autojoin";
chrome.runtime.onStartup.addListener(
() => {
// TODO: Load meeting detail and set it to `meeting_detail`.
}
);
chrome.runtime.onMessage.addListener(
function(request, sender, senderResponse) {
meeting_detail = JSON.parse(request);
startBadgeTimer(meeting_detail);
}
);
function setBadgeAndTimers(meeting_detail) {
badge_text = "";
badge_text = badge_text_static+" "+meeting_detail.subject;
var meeting_time = new Date(meeting_detail.start.dateTime)
var current_time = new Date();
if (meeting_time < current_time) {
return;
}
badge_show_timer = new Date(meeting_time.getTime() - 5 * 60 * 1000).getTime();
meeting_url = meeting_detail.onlineMeeting.joinUrl;
chrome.alarms.create(autoJoin, {when: meeting_time.getTime()});
}
function cleanAlarms() {
// Delete current alarms.
chrome.alarms.clear(showBadge);
chrome.alarms.clear(clearBadge);
}
function startBadgeTimer(meeting_detail) {
setBadgeAndTimers(meeting_detail);
cleanAlarms();
chrome.alarms.create(showBadge, {when: badge_show_timer});
}
function setBadgeText(badge_text, badge_remove_timer) {
chrome.browserAction.setBadgeText(
{text: badge_text},
() => {
chrome.alarms.create(clearBadge, {delayInMinutes: badge_remove_timer});
}
);
}
function clearBadgeText() {
chrome.browserAction.setBadgeText({text: ""});
}
chrome.alarms.onAlarm.addListener(
(alarm) => {
if (alarm.name == clearBadge) {
clearBadgeText();
} else if (alarm.name == showBadge) {
setBadgeText(badge_text, badge_remove_timer);
} else if (alarm.name == autoJoin) {
chrome.tabs.create({
url: meeting_url
});
clearBadgeText();
}
}
);