-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
159 lines (130 loc) · 4.95 KB
/
utils.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
'use strict';
let term = 1202;
async function setTerm() {
if (term) return;
const url = "https://enroll.wisc.edu/api/search/v1/terms";
term = await fetch(url).then(res => res.json())
.then(e => Math.max(...e.map(e => e.termCode))).catch(handleError);
}
async function searchCourse(query) {
const data = {
"queryString": query,
"selectedTerm": term,
"sortOrder": "SCORE",
"page": 1,
"pageSize": 10
};
const options = {
method: "POST",
headers: { "Content-Type": "application/json;charset=UTF-8" },
body: JSON.stringify(data)
};
const url = "https://enroll.wisc.edu/api/search/v1";
return await fetch(url, options).then(res => res.json()).catch(handleError)
}
async function getCourseList() {
const result = await new Promise(f => chrome.storage.local.get(["courseList"], f));
return result.courseList ? result.courseList : [];
}
async function setCourseList(courseList) {
return await new Promise(f => chrome.storage.local.set({ courseList }, f))
}
async function removeFromCourseList(courseID) {
const courseList = await getCourseList();
await setCourseList(courseList.filter(e => e.id !== courseID));
}
async function addToCourseList(courseID) {
const courseMeta = { alarm: false, id: courseID };
const courseList = await getCourseList();
if (courseList.filter(e => e.id === courseID).length !== 0) return;
await setCourseList(courseList.concat(courseMeta));
return await loadCourse(courseMeta);
}
async function modifyCourseListByCourseID(courseID, f) {
const courseList = await getCourseList();
const elem = courseList.find(e => e.id === courseID);
f(elem);
await setCourseList(courseList);
return elem;
}
async function loadCourse(courseMeta) {
const url = `https://enroll.wisc.edu/api/search/v1/enrollmentPackages/${term}/${courseMeta.id.replace("-", "/")}`;
return await fetch(url).then(res => res.json())
.then(list => ({ list, ...courseMeta })).catch(handleError)
}
async function getSavedCourseData() {
return (await new Promise(f => chrome.storage.local.get(["courseData"], f))).courseData;
}
async function loadAllCourses() {
let courseData;
try {
await setTerm();
const courseList = await getCourseList();
courseData = await Promise.all(courseList.map(loadCourse));
await checkDifference(courseData);
await new Promise(f => chrome.storage.local.set({ courseData }, f));
} catch (e) {
courseData = await getSavedCourseData();
}
return courseData;
}
async function checkDifference(newData) {
const oldData = await getSavedCourseData();
if (!oldData || !newData) return;
newData.filter(e => e.alarm).forEach(newCourse => {
const oldCourse = oldData.find(e => e.id === newCourse.id);
checkCourseDifference(newCourse, oldCourse);
console.log({ oldCourse, newCourse });
})
}
function checkCourseDifference(newCourse, oldCourse) {
if (!oldCourse || !newCourse) return;
const newSectionList = newCourse.list;
const oldSectionList = oldCourse.list;
const courseName = getCourseName(newSectionList);
newSectionList.forEach(newSection => {
const sectionName = getSectionName(newSection);
const oldSection = oldSectionList.find(e => e.docId === newSection.docId);
if (!oldSection) {
sendNotification(courseName, `New Section added for ${courseName}`);
return;
}
const oldStatus = oldSection.packageEnrollmentStatus.status.toLowerCase();
const newStatus = newSection.packageEnrollmentStatus.status.toLowerCase();
if (oldStatus !== newStatus) {
sendNotification(courseName, `Section ${sectionName} is ${newStatus} now`);
}
});
}
async function getMadGradesURL(courseName) {
const options = { headers: { "Authorization": "Token token=16d5319478d74e9ab353cc56e74cc6ea" } };
const url = `https://api.madgrades.com/v1/courses?query=${courseName}`;
return await fetch(url, options).then(res => res.json())
.then(res => res.results[0].uuid)
.then(uuid => `https://madgrades.com/courses/${uuid}`)
.catch(() => null)
}
function timeToString(time) {
return new Date(time).toLocaleTimeString('en-US', { hour: 'numeric', minute: 'numeric' });
}
function sendNotification(title, message) {
chrome.notifications.create({
type: "basic",
iconUrl: "icons/icon128.png",
title,
message
});
}
function handleError(err) {
if (err.message === "Failed to fetch") {
sendNotification("Login required", "Please login to your wisc account");
window.open("https://enroll.wisc.edu/");
}
throw err;
}
function getCourseName(course) {
return course[0].sections[0].subject.shortDescription + " " + course[0].catalogNumber;
}
function getSectionName(section) {
return section.sections.map(e => `${e.type} ${e.sectionNumber}`).join("; ");
}