-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
209 lines (195 loc) · 8.79 KB
/
index.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
BASE_URL = "https://e3p.nycu.edu.tw";
// handle processing and rednering data
const port = chrome.runtime.connect({
name: "connection"
});
const students_count_regex = /[0-9]+/g
port.onMessage.addListener(function(response) {
if (response["type"] == "main") {
let html = document.createElement("div");
html.setAttribute('style', 'display: none;');
html.innerHTML = response["data"];
let courseLinks = html.querySelectorAll("#layer2_right_current_course_stu .course-link");
if (courseLinks.length == 0) {
console.log("no courses");
let notificationElement = document.getElementById("notification");
notificationElement.innerText = "No courses found, maybe you haven't loged in.";
notificationElement.style.display = "block";
return;
}
for (let i = 0; i < courseLinks.length; i++) {
let id = courseLinks[i].href.split("id=")[1];
port.postMessage({
"type": "course",
"id": id
})
}
} else if (response["type"] == "course") {
// homework in one class
let html = document.createElement("div");
html.setAttribute('style', 'display: none;');
html.innerHTML = response["data"];
let course_name = html.querySelector("#page-header .page-context-header h1").innerText;
course_name = /【[0-9]{3}.】[0-9]+(.*)/.exec(course_name.split(" ")[0])[1];
// in_progress
let inProgress = [];
let inProgressNames = html.querySelectorAll("#news-view-basic-in-progress tbody tr .instancename")
let inProgressStartDates = html.querySelectorAll("#news-view-basic-in-progress tbody tr td:nth-child(2)")
let inProgressDueDates = html.querySelectorAll("#news-view-basic-in-progress tbody tr td:nth-child(3)")
let inProgressStudentsCounts = html.querySelectorAll("#news-view-basic-in-progress tbody tr td:nth-child(4)")
let inProgressLinks = html.querySelectorAll("#news-view-basic-in-progress tbody tr .aalink")
for (let i = 0; i < inProgressNames.length; i++) {
inProgress.push({
"name": inProgressNames[i].innerText,
"course_name": course_name,
"start_date": inProgressStartDates[i].innerText,
"due_date": inProgressDueDates[i].innerText,
"students_count": inProgressStudentsCounts[i].innerText.match(students_count_regex).join("/"),
"link": inProgressLinks[i].href,
});
}
// save in_progress to local storage
saveCache(inProgress, "in-progress");
renderTable("in-progress", inProgress);
// submitted
let submitted = [];
let submittedNames = html.querySelectorAll("#news-view-nofile2-tobegraded-in-progress tbody tr .instancename")
let submittedStartDates = html.querySelectorAll("#news-view-nofile2-tobegraded-in-progress tbody tr td:nth-child(2)")
let submittedDueDates = html.querySelectorAll("#news-view-nofile2-tobegraded-in-progress tbody tr td:nth-child(3)")
let submittedStudentsCounts = html.querySelectorAll("#news-view-nofile2-tobegraded-in-progress tbody tr td:nth-child(4)")
let submittedLinks = html.querySelectorAll("#news-view-nofile2-tobegraded-in-progress tbody tr .aalink")
for (let i = 0; i < submittedNames.length; i++) {
submitted.push({
"name": submittedNames[i].innerText,
"course_name": course_name,
"start_date": submittedStartDates[i].innerText,
"due_date": submittedDueDates[i].innerText,
"students_count": submittedStudentsCounts[i].innerText.match(students_count_regex).join("/"),
"link": submittedLinks[i].href,
});
}
// save submitted to local storage
saveCache(submitted, "submitted");
renderTable("submitted", submitted);
// overdue
let overdue = [];
let overdueNames = html.querySelectorAll("#news-view-nofile2-notsubmitted-in-progress tbody tr .instancename")
let overdueStartDates = html.querySelectorAll("#news-view-nofile2-notsubmitted-in-progress tbody tr td:nth-child(2)")
let overdueDueDates = html.querySelectorAll("#news-view-nofile2-notsubmitted-in-progress tbody tr td:nth-child(3)")
let overdueStudentsCounts = html.querySelectorAll("#news-view-nofile2-notsubmitted-in-progress tbody tr td:nth-child(4)")
let overdueLinks = html.querySelectorAll("#news-view-nofile2-notsubmitted-in-progress tbody tr .aalink")
for (let i = 0; i < overdueNames.length; i++) {
overdue.push({
"name": overdueNames[i].innerText,
"course_name": course_name,
"start_date": overdueStartDates[i].innerText,
"due_date": overdueDueDates[i].innerText,
"students_count": overdueStudentsCounts[i].innerText.match(students_count_regex).join("/"),
"link": overdueLinks[i].href,
});
}
// save overdue to local storage
saveCache(overdue, "overdue");
renderTable("overdue", overdue);
// save update time
chrome.storage.local.set({
"update-time": new Date().getTime()
}, function() {
console.log("update-time saved");
});
}
});
function saveCache(data, key) {
chrome.storage.local.get([key], function(result) {
tmp = result[key] || [];
for (let i = 0; i < data.length; i++) {
tmp.push(data[i]);
}
chrome.storage.local.set({
[key]: tmp
}, function() {
console.log(key + " saved");
});
});
}
function flushCache() {
chrome.storage.local.remove(["in-progress", "submitted", "overdue"], function() {
console.log("cache flushed");
});
}
function flushTable() {
// flush all rows in in-progress-container
let inProgressContainer = document.getElementById("in-progress-container");
while (inProgressContainer.children.length > 1) {
inProgressContainer.removeChild(inProgressContainer.lastChild);
}
// flush all rows in submitted-container
let submittedContainer = document.getElementById("submitted-container");
while (submittedContainer.children.length > 1) {
submittedContainer.removeChild(submittedContainer.lastChild);
}
// flush all rows in overdue-container
let overdueContainer = document.getElementById("overdue-container");
while (overdueContainer.children.length > 1) {
overdueContainer.removeChild(overdueContainer.lastChild);
}
}
function fetchCourseData() {
chrome.cookies.get({
url: BASE_URL,
name: "MoodleSession"
}, function(d) {
// fetch data
port.postMessage({
"type": "main",
"session": d.value
});
})
}
function renderTable(type, data) {
let container = document.getElementById(`${type}-container`);
for (let i = 0; i < data.length; i++) {
let row = `
<td><a href="${data[i]["link"]}" target="_blank" title="${data[i]["name"]}(${data[i]["course_name"]})">${data[i]["name"]}</a></td>
<td>${data[i]["start_date"]}</td>
<td>${data[i]["due_date"]}</td>
<td>${data[i]["students_count"]}</td>
`
let rowElement = document.createElement("tr");
rowElement.innerHTML = row;
container.appendChild(rowElement);
}
}
window.onload = function() {
// get cache data from local storage
chrome.storage.local.get(["in-progress", "submitted", "overdue", "update-time"], function(data) {
const timeDiff = (new Date() - new Date(data["update-time"])) / 1000;
if (data["in-progress"] && data["submitted"] && data["overdue"] && timeDiff < 60 * 60 * 1) {
renderTable("in-progress", data["in-progress"]);
renderTable("submitted", data["submitted"]);
renderTable("overdue", data["overdue"]);
}
else {
flushCache();
flushTable();
fetchCourseData();
}
});
}
document.getElementById("refresh-btn").addEventListener("click", function() {
flushCache();
flushTable();
fetchCourseData();
});
document.getElementById("in-progress-title").addEventListener("click", function() {
const container = document.getElementById("in-progress-container");
container.classList.toggle("hidden");
});
document.getElementById("submitted-title").addEventListener("click", function() {
const container = document.getElementById("submitted-container");
container.classList.toggle("hidden");
});
document.getElementById("overdue-title").addEventListener("click", function() {
const container = document.getElementById("overdue-container");
container.classList.toggle("hidden");
});