forked from yxliang01/Unimelb-WAM-Checker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate.js
136 lines (107 loc) · 3.45 KB
/
update.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
"use strict";
var alarm_callback = {
"CheckWAM": CheckWAM
};
var targetTab;
var lastResult;
chrome.alarms.onAlarm.addListener(onAlarm);
chrome.notifications.onButtonClicked.addListener(function onNotificationButtonClicked(notificationId, buttonIndex) {
switch (notificationId) {
case "WAM Changed":
if (buttonIndex === 0) {
activateTab(targetTab);
}
break;
}
});
function activateTab(tab) {
chrome.tabs.update(tab.id, { "active": true });
}
function onAlarm(alarm) {
alarm_callback[alarm.name]();
}
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
chrome.tabs.query({ "url": ["https://prod.ss.unimelb.edu.au/student/SM/ResultsDtls*"] }, function findTarget(tabs) {
if (tabs.length === 0) {
sendResponse(false);
return;
}
tabs.some(function(tab, idx) {
chrome.tabs.sendMessage(tab.id, "Find Result", undefined, function(response) {
if (targetTab === undefined) {
if (response !== null) {
setTargetTab(tab);
sendResponse(true);
return true;
} else if (idx === tabs.length - 1)
sendResponse(false);
return true;
}
});
});
});
return true;
}
);
function setTargetTab(tab) {
targetTab = tab;
lastResult = undefined;
if (tab !== undefined) {
chrome.alarms.create('CheckWAM', { periodInMinutes: 1 });
CheckWAM();
} else
stopMonitoring();
}
function CheckWAM() {
if (targetTab === undefined)
return;
if (targetTab.status !== "complete")
return;
if (targetTab.url.indexOf("https://prod.ss.unimelb.edu.au/student/SM/ResultsDtls") === -1) {
stopMonitoring();
return;
}
chrome.tabs.sendMessage(targetTab.id, "Find Result", undefined, function(response) {
if (response === null) {
setTargetTab(undefined);
return;
}
if (lastResult === undefined) {
setCurrentMark(response);
} else if (lastResult !== response) {
notifyUser();
setCurrentMark(response);
}
});
chrome.tabs.reload(targetTab.id);
}
function setCurrentMark(mark) {
lastResult = mark;
}
function stopMonitoring() {
chrome.alarms.clear('CheckWAM');
chrome.notifications.create("Stop Monitoring", {
"type": "basic",
"iconUrl": "images/icon16.png",
"title": "The WAM monitoring had been stopped",
"message": "The tab is closed or doesn't show your WAM",
"priority": 2,
"eventTime": Date.now(),
"isClickable": false,
"requireInteraction": true
});
}
function notifyUser() {
chrome.notifications.create("WAM Changed", {
"type": "basic",
"iconUrl": "images/icon16.png",
"title": "You WAM had been changed!",
"message": "You WAM had been changed!",
"priority": 2,
"eventTime": Date.now(),
"buttons": [{ "title": "Check it now!" }],
"isClickable": true,
"requireInteraction": true
});
}