-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathopen_course.js
55 lines (55 loc) · 2.15 KB
/
open_course.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
const urlParams = new URLSearchParams(window.location.search);
const fromCMSExtension = urlParams.get('fromCMS');
const searchStr = urlParams.get("q")
console.log("Asdasd");
if (fromCMSExtension) {
console.log("Let's go!");
// Click on the first search result
const results = document.querySelector(".course-search-result");
const subjectCode = urlParams.get("q");
if (!results) {
// No results found
chrome.storage.local.get("subjects", (result) => {
const newSubj = result.subjects.map(s => {
if (s.subject == subjectCode) {
return { ...s, error: "Course not found." };
} else return s;
})
chrome.storage.local.set({ subjects: newSubj }, (e) => { });
chrome.runtime.sendMessage({ closeThis: true });
})
}
// If it is not an "L" section and there are multiple search results, log the error
else if (results.children.length > 1 && !/\bL\b/ig.test(subjectCode)) {
chrome.storage.local.get("subjects", (result) => {
const newSubj = result.subjects.map(s => {
if (s.subject == subjectCode) {
return { ...s, error: "Multiple search results found." };
} else return s;
})
chrome.storage.local.set({ subjects: newSubj }, (e) => { });
chrome.runtime.sendMessage({ closeThis: true });
})
} else {
const subNameInSearch = results.children[0].getElementsByTagName("a")[0].innerText;
let [whole, subjectCode, code] = [...searchStr.matchAll(/(\w+)\s*(F\d{3})/ig)][0];
if ((new RegExp(`\\b${subjectCode}\\b`)).test(subNameInSearch)) {
const link = results.children[0].getElementsByTagName("a")[0].getAttribute("href");
window.location.href = link + "&fromCMS=1";
} else {
chrome.storage.local.get("subjects", res => {
const newSubjects = res.subjects;
for (let i = 0; i < newSubjects.length; i++) {
const s = newSubjects[i];
if (s.code == `${subjectCode} ${code}`) {
s.error = "Course not found";
break;
}
}
chrome.storage.local.set({ subjects: newSubjects }, e => {
chrome.runtime.sendMessage({ closeThis: true });
});
})
}
}
}