-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathoptions.js
143 lines (141 loc) · 5.27 KB
/
options.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
window.onload = () => {
tableFromStorage();
chrome.storage.local.get("openedOnce", ({ openedOnce }) => {
if (!openedOnce) {
document.getElementById("about").scrollIntoView();
chrome.storage.local.set({ openedOnce: true });
}
})
}
function enrolInCourses() {
trimInputs();
const [subjects, allCodesAreValid] = getSubjectsFromTextbox();
const subjectCodes = document.getElementById("codes").value.toUpperCase().split("\n");
createTable(subjects);
if (!allCodesAreValid) UIkit.notification('Course code number is required', "danger");
else {
console.log({ subjects });
chrome.storage.local.set({ subjects }, (e) => {
chrome.storage.local.remove("unenrol");
subjectCodes.forEach(subject => {
chrome.tabs.create({
active: false,
url: `https://cms.bits-hyderabad.ac.in/course/search.php?areaids=core_course-course&q=${subject}&fromCMS=1`
})
});
})
}
}
document.getElementById("enrol").addEventListener("click", enrolInCourses);
document.getElementById("unenrol-all").addEventListener("click", (e) => {
UIkit.modal.confirm("Do you want to unenrol from all courses? This action cannot be interrupted or reverted.").then(function () {
chrome.storage.local.set({ subjects: null, unenrol: true, unenrol_all: true });
chrome.tabs.create({ url: "https://cms.bits-hyderabad.ac.in/my/" });
})
})
function getSubjectsFromTextbox() {
const subjectCodes = document.getElementById("codes").value.toUpperCase().split("\n");
//take first 3 elements only
if (subjectCodes.length > 3) {
const remaining = subjectCodes.splice(3);
subjectCodes.length = 3;
//replace "codes" textbox with remaining elements
document.getElementById("codes").value = remaining.join("\n");
// document.getElementById("codes").value = subjectCodes.join("\n");
// subjectCodes.length = 3;
UIkit.notification('Only 3 courses can be worked with at once. Click "Enrol" once these are done.', "danger");
}
const regex = /\w+ F\d{3}/g;
const sectionRegex = /([TLP]\d+)|\bL\b/g;
let allCodesAreValid = true;
const subjects = subjectCodes.map(c => {
const matches1 = c.match(regex);
if (matches1 && matches1.length == 1) {
const matches2 = c.match(sectionRegex);
if (matches2 && matches2.length == 1) {
return { subject: c, code: matches1[0], section: matches2[0], enrolled: false, isValid: true };
} else {
allCodesAreValid = false;
return { subject: c, code: matches1[0], isValid: false };
}
} else {
allCodesAreValid = false;
return { subject: c, code: c, enrolled: false, isValid: false };
}
});
return [subjects, allCodesAreValid];
}
document.getElementById("unenrol").addEventListener("click", (e) => {
trimInputs();
UIkit.modal.confirm('Do you want to unenrol from these courses?').then(function () {
// Yes
const [subjects, allCodesAreValid] = getSubjectsFromTextbox();
const subjectCodes = document.getElementById("codes").value.toUpperCase().split("\n");
createTable(subjects);
if (!allCodesAreValid) UIkit.notification('Course code number is required', "danger");
else {
console.log({ subjects });
chrome.storage.local.set({ subjects }, (e) => {
chrome.storage.local.set({ unenrol: true }, (e) => {
chrome.tabs.create({ active: false, url: "https://cms.bits-hyderabad.ac.in/my/" })
})
})
}
}, function () {
// console.log('Rejected.')
});
})
function trimInputs() {
document.getElementById("codes").value = document.getElementById("codes").value.
replace(/(^\s*)|(\s*$)/gi, ""). // removes leading and trailing spaces
replace(/[ ]{2,}/gi, " "). // replaces multiple spaces with one space
replace(/\n +/, "\n")
.replace(/(^\n+)|\n+$/, ""); // Removes spaces after newlines
return;
}
function createTable(subjects) {
const table = document.getElementById("tbody");
let html = "";
subjects.forEach(s => {
html += s.isValid ? `<tr id="${s.code}"><td>${s.code}</td><td>${s.section}</td><td></td></tr>` :
`<tr class="red"><td>${s.subject}</td><td>--</td><td>Invalid course code</tr>`;
})
table.innerHTML = html;
}
function tableFromStorage() {
chrome.storage.local.get('subjects', (res) => {
showTableFromSubjects(res.subjects);
})
}
chrome.storage.onChanged.addListener((a, b) => {
tableFromStorage();
const subjectCodes = document.getElementById("codes").value;
// if (subjectCodes.trim() !== "") {
// showTableFromSubjects(a.subjects.newValue, true);
// enrolInCourses();
// } else
// showTableFromSubjects(a.subjects.newValue);
})
function processRemainingSubjects() {
const subjectCodes = document.getElementById("codes").value;
if (subjectCodes.trim() !== "") {
enrolInCourses();
return true;
}
return false;
}
function showTableFromSubjects(subjects, append = false) {
let html = "";
subjects.forEach(s => {
html += `
<tr class="${s.enrolled ? 'green' : s.error ? 'red' : s.unenrolled ? 'black' : ''}">
<td>${s.code}</td>
<td>${s.section}</td>
<td>${s.enrolled ? 'Enrolled' : (s.error ? s.error : s.unenrolled ? 'Unenrolled' : 'Loading')}</td>
</tr>`;
})
if (append) {
document.getElementById("tbody").innerHTML += html;
} else
document.getElementById("tbody").innerHTML = html;
}