Skip to content

Commit be1c378

Browse files
committed
fix: add function to get users courses
getCourses is a bit misleading considering it also adds a new class grade, however, conveys the point better than it collects the user's courses. I additionally made the variables at the start of main_page constant because there is no reason they should change after the function call. Making these changes improves readability of the code, better conveying what portions of it are doing. Signed-off-by: Lucas Sta Maria <[email protected]>
1 parent d4d9f92 commit be1c378

File tree

1 file changed

+49
-37
lines changed

1 file changed

+49
-37
lines changed

src/js/saspowerschoolff.js

Lines changed: 49 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -76,44 +76,12 @@ function main () {
7676
}
7777

7878
function main_page () {
79-
let student_name = getStudentName();
80-
let { sem1_col, sem2_col } = getSemesterCols();
81-
let second_semester = isSecondSemester();
82-
const courses = [];
83-
const $grade_rows = $('#quickLookup table.grid').find('tr');
79+
const student_name = getStudentName();
80+
const { sem1_col, sem2_col } = getSemesterCols();
81+
const second_semester = isSecondSemester();
82+
const current_term = getCurrentTerm();
83+
const courses = getCourses(second_semester);
8484

85-
for (let i = 0; i < $grade_rows.length; i++) {
86-
let $course;
87-
if (second_semester) {
88-
const $cells = $grade_rows.eq(i).find('td');
89-
$course = $cells.eq(sem2_col).find('a[href^="scores.html"]');
90-
const $first_grade = $cells.eq(sem1_col).find('a[href^="scores.html"]');
91-
if ($first_grade.length === 1) {
92-
if (gradeToGPA($first_grade.text()) !== -1) {
93-
new (Vue.extend(ClassGrade))({
94-
propsData: {
95-
course: new Course("", `https://powerschool.sas.edu.sg/guardian/${$first_grade.attr('href')}`, $first_grade.text()),
96-
showMissing: false,
97-
},
98-
}).$mount($first_grade.get(0));
99-
}
100-
}
101-
} else {
102-
$course = $grade_rows.eq(i).find('td a[href^="scores.html"]').eq(0);
103-
}
104-
if ($course.length === 1) {
105-
const temp = $course.parents().eq(1).children("td[align=left]").text().match(".*(?=Details)")[0];
106-
courses.push(new Course(temp.trim(), `https://powerschool.sas.edu.sg/guardian/${$course.attr('href')}`, $course.text()));
107-
if (gradeToGPA($course.text()) !== -1) {
108-
new (Vue.extend(ClassGrade))({
109-
propsData: {
110-
course: courses[courses.length - 1],
111-
},
112-
}).$mount($course.get(0));
113-
}
114-
}
115-
}
116-
let current_term = getCurrentTerm();
11785
showCurrentGPA(second_semester, courses);
11886

11987
if (second_semester) {
@@ -254,6 +222,50 @@ function getCurrentTerm () {
254222
return current_term;
255223
}
256224

225+
/**
226+
* Returns an array of the current courses of the user and creates Vue objects for the courses.
227+
* @param second_semester If the current semester is the second semester
228+
* @returns {Course[]} array of Course objects representing the Courses of the user
229+
*/
230+
function getCourses(second_semester) {
231+
const $grade_rows = $('#quickLookup table.grid').find('tr');
232+
const courses = [];
233+
234+
for (let i = 0; i < $grade_rows.length; i++) {
235+
let $course;
236+
if (second_semester) {
237+
const $cells = $grade_rows.eq(i).find('td');
238+
$course = $cells.eq(sem2_col).find('a[href^="scores.html"]');
239+
const $first_grade = $cells.eq(sem1_col).find('a[href^="scores.html"]');
240+
if ($first_grade.length === 1) {
241+
if (gradeToGPA($first_grade.text()) !== -1) {
242+
new (Vue.extend(ClassGrade))({
243+
propsData: {
244+
course: new Course("", `https://powerschool.sas.edu.sg/guardian/${$first_grade.attr('href')}`, $first_grade.text()),
245+
showMissing: false,
246+
},
247+
}).$mount($first_grade.get(0));
248+
}
249+
}
250+
} else {
251+
$course = $grade_rows.eq(i).find('td a[href^="scores.html"]').eq(0);
252+
}
253+
if ($course.length === 1) {
254+
const temp = $course.parents().eq(1).children("td[align=left]").text().match(".*(?=Details)")[0];
255+
courses.push(new Course(temp.trim(), `https://powerschool.sas.edu.sg/guardian/${$course.attr('href')}`, $course.text()));
256+
if (gradeToGPA($course.text()) !== -1) {
257+
new (Vue.extend(ClassGrade))({
258+
propsData: {
259+
course: courses[courses.length - 1],
260+
},
261+
}).$mount($course.get(0));
262+
}
263+
}
264+
}
265+
266+
return courses;
267+
}
268+
257269
/**
258270
* Show the first semester GPA.
259271
*/

0 commit comments

Comments
 (0)