Skip to content

Commit 728790f

Browse files
committed
fix: add function for showing GPA
When reading the code in the main_page function, a headache can be caused from the amount of jQuery code that has to be traversed. In continuation of solving this problem, I have moved some of the code into more descriptive functions. The showCurrentGPA function is more descriptive than random jQuery. The showfirstSemGPA function is also more descriptive than a fetch for the HTML and a lot of HTML parsing and then finally displaying the GPA. The getFirstSemCourses function is *hopefully* more descriptive than fetch("https://powerschool.sas.edu.sg/guardian/termgrades.html"). There's probably a better function name for this. By moving some of the code into the main_page function, it hopefully creates better readability of the main_page function. Signed-off-by: Lucas Sta Maria <[email protected]>
1 parent a735e17 commit 728790f

File tree

1 file changed

+54
-29
lines changed

1 file changed

+54
-29
lines changed

src/js/saspowerschoolff.js

Lines changed: 54 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,10 @@ function main_page () {
8080
let { sem1_col, sem2_col } = getSemesterCols();
8181
let second_semester = isSecondSemester();
8282
const courses = [];
83-
const courses_first_semester = [];
8483
const $grade_rows = $('#quickLookup table.grid').find('tr');
8584
let current_term = "";
8685
let attendance_href = "";
87-
86+
8887
for (let i = 0; i < $grade_rows.length; i++) {
8988
let $course;
9089
if (second_semester) {
@@ -119,36 +118,12 @@ function main_page () {
119118
if ((attendance_href = $grade_rows.eq($grade_rows.length - 1)?.find('a[href*="attendancedates"]')?.[0]?.href)) { // Check that attendance_href exists and if it does, run the next line.
120119
current_term = new URL(attendance_href).searchParams.get("term");
121120
}
122-
$("table[border='0'][cellpadding='3'][cellspacing='1'][width='100%']").prepend(`<tr><td align="center">Current Semester GPA (${second_semester ? 'S2' : 'S1'}): ${calculate_gpa(courses)}</td></tr>`);
121+
showCurrentGPA(second_semester, courses);
123122

124123
if (second_semester) {
125-
fetch("https://powerschool.sas.edu.sg/guardian/termgrades.html")
126-
.then((response) => {
127-
return response.text();
128-
})
129-
.then((data) => {
130-
const el = document.createElement("html");
131-
let element_list = [];
132-
el.innerHTML = data;
133-
element_list = el.getElementsByClassName("box-round")[0].getElementsByTagName("table")[0];
134-
element_list = element_list.getElementsByTagName("tbody")[0].getElementsByTagName("tr");
135-
if (element_list.length > 2) {
136-
for (let i = 2; i < element_list.length; i++) {
137-
const $prev_course = element_list[i];
138-
if ($prev_course?.innerText?.trim() === "S2") {
139-
break;
140-
}
141-
if ($prev_course?.getElementsByTagName("td").length > 1) {
142-
courses_first_semester.push(new Course($prev_course.getElementsByTagName("td")[0].textContent.trim(),
143-
$prev_course.getElementsByTagName("td")[2].getElementsByTagName("a")[0].href,
144-
$prev_course.getElementsByTagName("td")[1].textContent.trim(),
145-
));
146-
}
147-
}
148-
$("table[border='0'][cellpadding='3'][cellspacing='1'][width='100%']").prepend(`<tr><td align="center">Last Semester GPA (S1): ${calculate_gpa(courses_first_semester)}</td></tr>`);
149-
}
150-
});
124+
showFirstSemGPA();
151125
}
126+
152127
$("table[border='0'][cellpadding='3'][cellspacing='1'][width='100%']").prepend(`<td id="cumulative-gpa"></td>`);
153128
// passing courses in to possibly include current semester GPA if term has not finished yet.
154129
new (Vue.extend(CumulativeGPA))({
@@ -259,4 +234,54 @@ function isSecondSemester (sem2_col) {
259234
return false;
260235
}
261236

237+
/**
238+
* Show the current semester's GPA.
239+
* @param second_semester If the current semester is the second semester
240+
* @param courses an array of Courses that the student is taking
241+
*/
242+
function showCurrentGPA (second_semester, courses) {
243+
$("table[border='0'][cellpadding='3'][cellspacing='1'][width='100%']").prepend(`<tr><td align="center">Current Semester GPA (${second_semester ? 'S2' : 'S1'}): ${calculate_gpa(courses)}</td></tr>`);
244+
}
262245

246+
/**
247+
* Show the first semester GPA.
248+
*/
249+
function showFirstSemGPA () {
250+
const courses_first_semester = [];
251+
getFirstSemCourses()
252+
.then((data) => {
253+
const el = document.createElement("html");
254+
let element_list = [];
255+
el.innerHTML = data;
256+
element_list = el.getElementsByClassName("box-round")[0].getElementsByTagName("table")[0];
257+
element_list = element_list.getElementsByTagName("tbody")[0].getElementsByTagName("tr");
258+
if (element_list.length > 2) {
259+
for (let i = 2; i < element_list.length; i++) {
260+
const $prev_course = element_list[i];
261+
if ($prev_course?.innerText?.trim() === "S2") {
262+
break;
263+
}
264+
if ($prev_course?.getElementsByTagName("td").length > 1) {
265+
courses_first_semester.push(new Course($prev_course.getElementsByTagName("td")[0].textContent.trim(),
266+
$prev_course.getElementsByTagName("td")[2].getElementsByTagName("a")[0].href,
267+
$prev_course.getElementsByTagName("td")[1].textContent.trim(),
268+
));
269+
}
270+
}
271+
$("table[border='0'][cellpadding='3'][cellspacing='1'][width='100%']").prepend(`<tr><td align="center">Last Semester GPA (S1): ${calculate_gpa(courses_first_semester)}</td></tr>`);
272+
}
273+
});
274+
}
275+
276+
/**
277+
* Get the first semester courses and grades HTML.
278+
* @returns {Promise} the html from the first semester course and grades page
279+
*/
280+
function getFirstSemCourses () {
281+
return new Promise((resolve, reject) => {
282+
fetch("https://powerschool.sas.edu.sg/guardian/termgrades.html")
283+
.then(response => {
284+
resolve(response);
285+
});
286+
});
287+
}

0 commit comments

Comments
 (0)