-
Notifications
You must be signed in to change notification settings - Fork 3
/
studentwebplus.js
439 lines (358 loc) · 13 KB
/
studentwebplus.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
const lang = document.querySelector("html").getAttribute("lang");
const langMap = {
"nb": {
"title": "Karakterkalkulator",
"spring": "VÅR",
"autumn": "HØST",
"summer": "SOMMER",
"passed": "Bestått",
"study": "Studie",
"average": "Snitt",
"points": "Studiepoeng",
"addBtnTxt": "Legg til",
"start": "Start",
"end" : "Slutt"
},
"en": {
"title": "Grade Calculator",
"spring": "SPRING",
"autumn": "AUTUMN",
"summer": "SUMMER",
"passed": "Passed",
"study": "Study",
"average": "Average",
"points": "ECTS",
"addBtnTxt": "Add",
"start": "Start",
"end" : "End"
},
"nn": {
"title": "Karakterkalkulator",
"spring": "VÅR",
"autumn": "HAUST",
"summer": "SOMMER",
"passed": "Greidd",
"study": "Studie",
"average": "Snitt",
"points": "Studiepoeng,",
"addBtnTxt": "Legg til",
"start": "Start",
"end": "Slutt",
},
}
class Degree {
constructor(name, startYear, endYear) {
this.name = name;
this.startYear = startYear;
this.endYear = endYear;
}
get name() {
return this._name;
}
set name(value) {
this._name = value;
}
get startYear() {
return this._startYear;
}
set startYear(value) {
this._startYear = value;
}
get endYear() {
return this._endYear;
}
set endYear(value) {
this._endYear = value;
}
}
class GradeCalculator {
constructor() {
this.totalEctsForCalculation = 0;
this.totalEcts = 0;
this.sumGrades = 0;
this.nonLetterGrades = ["Bestått","Passed","Greidd"];
this.pattern = /^[A-E]|Bestått|Passed|Greidd$/;
}
get totalEcts() {
return this._totalEcts;
}
set totalEcts(value) {
this._totalEcts = value;
}
get totalEctsForCalculation() {
return this._totalEctsForCalculation;
}
set totalEctsForCalculation(value) {
this._totalEctsForCalculation = value;
}
get nonLetterGrades() {
return this._nonLetterGrades;
}
set nonLetterGrades(value) {
this._nonLetterGrades = value;
}
calculateAverage() {
return this.totalEctsForCalculation === 0 ? 0 : this.sumGrades / this.totalEctsForCalculation;
}
addGrade(grade, ects) {
this.totalEcts += ects;
if (!this.nonLetterGrades.includes(grade)) {
let gradeValue = this.letterToNumber(grade);
this.sumGrades += (gradeValue * ects);
this.totalEctsForCalculation += ects;
}
this.selectedGradeCount++;
}
removeGrade(grade, ects) {
this.totalEcts -= ects;
if (!this.nonLetterGrades.includes(grade)) {
let gradeValue = this.letterToNumber(grade);
this.sumGrades -= (gradeValue * ects);
this.totalEctsForCalculation -= ects;
}
this.selectedGradeCount--;
}
letterToNumber(grade){
return 5 - (grade.charCodeAt(0) - 65);
}
numberToLetter(grade){
return grade < 1.5 ? 'E' : String.fromCharCode(65 + (5- Math.round(grade)));
}
}
let degrees = JSON.parse(localStorage.getItem("degrees")) || [];
function createCustomMenu() {
let customMenu = document.createElement("div");
customMenu.id = "custom-menu";
customMenu.style.display = "none";
customMenu.style.position = "fixed";
customMenu.style.top = "50%";
customMenu.style.left = "50%";
customMenu.style.transform = "translate(-50%, -50%)";
customMenu.style.backgroundColor = "white";
customMenu.style.border = "1px solid black";
customMenu.style.boxShadow = "2px 2px 4px rgba(0,0,0,0.2)";
customMenu.style.width = "600px";
customMenu.style.height = "320px";
let header = document.createElement("div");
header.style.backgroundColor = "black";
header.style.color = "white";
header.style.padding = "10px";
header.style.textAlign = "center";
header.textContent = langMap[lang].title;
let exitButton = document.createElement("button");
exitButton.textContent = "X";
exitButton.style.float = "right";
exitButton.style.backgroundColor = "transparent";
exitButton.style.border = "none";
exitButton.style.color = "white";
exitButton.style.cursor = "pointer";
exitButton.style.marginTop = "-10px";
exitButton.style.marginRight = "-20px";
exitButton.addEventListener("click", function() {
customMenu.style.display = "none";
}
);
header.appendChild(exitButton);
let body = document.createElement("div");
body.style.padding = "10px";
body.style.textAlign = "center";
let left = document.createElement("div");
left.id = "custom-menu-left";
left.style.float = "left";
left.style.width = "40%";
left.style.height = "100%";
left.style.borderRight = "1px solid black";
let right = document.createElement("div");
right.id = "custom-menu-right";
right.style.float = "right";
right.style.width = "60%";
right.style.height = "100%";
let form = document.createElement("form");
form.id = "custom-menu-form";
form.style.textAlign = "left";
let degreeNameLabel = document.createElement("label");
degreeNameLabel.textContent = langMap[lang].study + ": ";
let degreeNameInput = document.createElement("input");
degreeNameInput.type = "text";
degreeNameInput.style.width = "90%";
let startYearLabel = document.createElement("label");
startYearLabel.textContent = langMap[lang].start + ": ";
let startYear = document.createElement("input");
startYear.type = "number";
startYear.min = "1950";
startYear.max = new Date().getFullYear();
startYear.style.width = "90%"
let endYearLabel = document.createElement("label");
endYearLabel.textContent = langMap[lang].end + ": ";
let endyear = document.createElement("input");
endyear.type = "number";
endyear.min = "1950";
endyear.max = new Date().getFullYear();
endyear.style.width = "90%"
let addButton = document.createElement("button");
addButton.textContent = langMap[lang].addBtnTxt;
addButton.className = "ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only small grey";
addButton.addEventListener("click", function(event) {
event.preventDefault();
let degreeName = degreeNameInput.value;
let start = startYear.value;
let end = endyear.value;
if (degreeName.trim() === "") {
alert("Gradsnavn mangler");
return;
}
if (start.trim() === "") {
alert("Start år mangler.");
return;
}
if (end.trim() === "") {
alert("Slutt år mangler.");
return;
}
if (parseInt(start) > parseInt(end)) {
alert("Start år kan ikke være større enn slutt år.");
return;
} else if (parseInt(start) == parseInt(end)) {
alert("Start år og slutt år kan ikke være like")
return;
}
let degree = new Degree(degreeName, start, end);
degrees.push(degree);
localStorage.setItem('degrees', JSON.stringify(degrees));
form.reset();
populateTable();
});
form.appendChild(degreeNameLabel);
form.appendChild(degreeNameInput);
form.appendChild(startYearLabel);
form.appendChild(startYear);
form.appendChild(endYearLabel);
form.appendChild(endyear);
form.appendChild(addButton);
let table = document.createElement("table");
table.style.marginTop = "-6px";
let thead = document.createElement("thead");
let headerRow = document.createElement("tr");
let degreeNameHeader = document.createElement("th");
degreeNameHeader.textContent = langMap[lang].study;
degreeNameHeader.style.minWidth = "100px";
let gradeAverageHeader = document.createElement("th");
gradeAverageHeader.textContent = langMap[lang].average;
gradeAverageHeader.style.minWidth = "50px";
let pointsHeader = document.createElement("th");
pointsHeader.textContent = langMap[lang].points;
pointsHeader.style.minWidth = "50px";
let removeHeader = document.createElement("th");
removeHeader.textContent = "";
removeHeader.style.minWidth = "50px";
headerRow.appendChild(degreeNameHeader);
headerRow.appendChild(gradeAverageHeader);
headerRow.appendChild(pointsHeader);
headerRow.appendChild(removeHeader);
thead.appendChild(headerRow);
table.appendChild(thead);
let tbody = document.createElement("tbody");
tbody.id = "custom-menu-table-body";
table.appendChild(tbody);
right.appendChild(table);
left.appendChild(form);
body.appendChild(left);
body.appendChild(right);
customMenu.appendChild(header);
customMenu.appendChild(body);
document.body.appendChild(customMenu);
return customMenu;
}
function injectMenu() {
let mainMenu = document.getElementById("menuBarRight");
if (mainMenu) {
let div = document.createElement("div");
div.className = "dropdown";
let a = document.createElement("a");
a.className = "dropdown-link";
a.textContent = "Studentweb+";
a.style.cursor = "pointer";
div.appendChild(a);
a.addEventListener("click", function() {
let customMenu = document.getElementById("custom-menu");
customMenu.style.display = customMenu.style.display === "none" ? "block" : "none";
});
mainMenu.appendChild(div);
}
}
function populateTable() {
let tbody = document.getElementById("custom-menu-table-body");
tbody.innerHTML = "";
degrees.forEach((degree, index) => {
let row = document.createElement("tr");
let degreeNameCell = document.createElement("td");
degreeNameCell.textContent = degree._name;
let gradeAverageCell = document.createElement("td");
let results = extractGrades(degree._startYear, degree._endYear);
if (results.avg == null) {
gradeAverageCell.textContent = results.letterGrade;
} else {
gradeAverageCell.textContent = results.avg.toFixed(1) + " (" + results.letterGrade + ")";
}
let pointsCell = document.createElement("td");
pointsCell.textContent = results.points;
let removeCell = document.createElement("td");
let a = document.createElement("a");
a.textContent = "X";
a.style.cursor = "pointer";
a.addEventListener("click", function () {
degrees.splice(index, 1);
localStorage.setItem("degrees", JSON.stringify(degrees));
populateTable();
});
removeCell.appendChild(a);
row.appendChild(degreeNameCell);
row.appendChild(gradeAverageCell);
row.appendChild(pointsCell);
row.appendChild(removeCell);
tbody.appendChild(row);
});
}
function extractGrades(start, end){
const calc = new GradeCalculator();
let table = document.querySelector(".table-standard.reflow.ui-panel-content");
if (table) {
let list = [];
for (const row of table.querySelectorAll("tr.resultatTop, tr.none")) {
let semesterColumn = row.querySelector("td.col1Semester");
let semesterText = semesterColumn.querySelector(".uuHidden").textContent.trim();
let resultColumn = row.querySelector("td.col6Resultat");
let grade = resultColumn.querySelector(".infoLinje span").textContent.trim();
let pointColumn = row.querySelector("td.col7Studiepoeng");
let isValidGrade = calc.pattern.test(grade);
if (isValidGrade && pointColumn.hasChildNodes()) {
let points = pointColumn.querySelector("span").textContent;
let parsedPoints = Number(points.replace(",", "."));
list.push({semesterText, grade, parsedPoints});
}
}
list.reverse();
list.forEach((item) => {
const { semesterText, grade, parsedPoints } = item;
const year = parseInt(semesterText.match(/\d+/)[0]);
if (
(semesterText.includes(start.toString()) && (semesterText.includes(langMap[lang].autumn))) ||
(year > start && year < end) ||
(semesterText.includes(end.toString()) && (semesterText.includes(langMap[lang].spring) || semesterText.includes(langMap[lang].summer)))
) {
calc.addGrade(grade, parsedPoints);
}
});
if (calc.sumGrades == 0 && calc.totalEcts > 0) {
console.log("No letter grades found for this period");
return {avg: null, letterGrade: langMap[lang].passed, points: calc.totalEcts};
}
const avg = calc.calculateAverage();
const letterGrade = calc.numberToLetter(avg)
const points = calc.totalEcts;
return {avg, letterGrade, points};
}
}
createCustomMenu();
injectMenu();
populateTable();