Skip to content

Commit a735e17

Browse files
committed
fix: add helper functions for second semester check, semester cols
By delegating the tasks to helper functions, the code becomes marginally easier to browse through. Better function name could be selected, however. getStudentName has better readability than what was previously used, and conveys its function better. s1col and s2col were renamed to sem1_col and sem2_col respectively. This is because "sem" is more understandable than "s". Assigning values to sem1_col and sem2_col was put into a function. Checking whether it was the second semester was also put into a function. Initially both of these tasks were mashed together, but separating it into two functions hopefully provides more clarity and modularity. I added newlines to the import statement from helpers because I initially put the getSemesterCols and isSecondSemester functions in there, but those changes aren't necessary. Signed-off-by: Lucas Sta Maria <[email protected]>
1 parent 9eceaf4 commit a735e17

File tree

2 files changed

+67
-30
lines changed

2 files changed

+67
-30
lines changed

src/js/helpers.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import Assignment from "./models/Assignment";
2828
import Course from './models/Course';
2929
import browser from 'webextension-polyfill';
30+
3031
const getKeyRange = require('get-key-range');
3132

3233
const grade_gpa = {

src/js/saspowerschoolff.js

Lines changed: 66 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,12 @@
2929
import $ from 'jquery';
3030
const browser = require('webextension-polyfill');
3131

32-
import { calculate_gpa, extractFinalPercent, gradeToGPA, analytics_message, saveGradesLocally } from './helpers';
32+
import {
33+
calculate_gpa,
34+
extractFinalPercent,
35+
gradeToGPA,
36+
analytics_message,
37+
saveGradesLocally } from './helpers';
3338

3439
// Vue Components
3540
import Vue from 'vue';
@@ -71,44 +76,21 @@ function main () {
7176
}
7277

7378
function main_page () {
74-
const student_name = document.querySelector('#userName').querySelector('span').innerText;
75-
let second_semester = false;
79+
let student_name = getStudentName();
80+
let { sem1_col, sem2_col } = getSemesterCols();
81+
let second_semester = isSecondSemester();
7682
const courses = [];
7783
const courses_first_semester = [];
7884
const $grade_rows = $('#quickLookup table.grid').find('tr');
79-
let s1col = 0;
80-
let s2col = 0;
8185
let current_term = "";
8286
let attendance_href = "";
83-
if ($grade_rows.eq(1).html().match("S2") != null) {
84-
second_semester = true;
85-
let curr = 0;
86-
$grade_rows.eq(1).find('th').get().forEach(e => {
87-
switch (e.innerText) {
88-
case "S1":
89-
s1col = curr;
90-
break;
91-
case "S2":
92-
s2col = curr;
93-
break;
94-
default:
95-
break;
96-
}
97-
curr += parseInt(e.getAttribute('colspan')) || 1;
98-
});
99-
second_semester = false;
100-
for (let t = 0; t < $grade_rows.length; t++) {
101-
if (gradeToGPA($grade_rows.eq(t).find('td').get(s2col)) !== -1) {
102-
second_semester = true;
103-
}
104-
}
105-
}
87+
10688
for (let i = 0; i < $grade_rows.length; i++) {
10789
let $course;
10890
if (second_semester) {
10991
const $cells = $grade_rows.eq(i).find('td');
110-
$course = $cells.eq(s2col).find('a[href^="scores.html"]');
111-
const $first_grade = $cells.eq(s1col).find('a[href^="scores.html"]');
92+
$course = $cells.eq(sem2_col).find('a[href^="scores.html"]');
93+
const $first_grade = $cells.eq(sem1_col).find('a[href^="scores.html"]');
11294
if ($first_grade.length === 1) {
11395
if (gradeToGPA($first_grade.text()) !== -1) {
11496
new (Vue.extend(ClassGrade))({
@@ -224,3 +206,57 @@ function html2nodelist (html_string) {
224206
temp.innerHTML = html_string;
225207
return temp.content.childNodes;
226208
}
209+
210+
/**
211+
* Returns the student name found in the main page's html
212+
* @returns {string} The student's name
213+
*/
214+
function getStudentName () {
215+
const student_name = document.querySelector('#userName').querySelector('span').innerText;
216+
}
217+
218+
/**
219+
* Return the semester 1 and semester 2 column indexes from the main page HTML.
220+
* @returns { number, number } { sem1_col, sem2_col } The first and second semester column indexes.
221+
*/
222+
function getSemesterCols () {
223+
let sem1_col = 0;
224+
let sem2_col = 0;
225+
const $grade_rows = $('#quickLookup table.grid').find('tr');
226+
if ($grade_rows.eq(1).html().match("S2") != null) {
227+
let curr = 0;
228+
$grade_rows.eq(1).find('th').get().forEach(e => {
229+
switch (e.innerText) {
230+
case "S1":
231+
sem1_col = curr;
232+
break;
233+
case "S2":
234+
sem2_col = curr;
235+
break;
236+
default:
237+
break;
238+
}
239+
curr += parseInt(e.getAttribute('colspan')) || 1;
240+
});
241+
}
242+
return { sem1_col, sem2_col };
243+
}
244+
245+
/**
246+
* Check whether it is currently second semester or not.
247+
* @param {number} sem2_col The column index corresponding to the second semester
248+
* @returns {bool} Whether it is currently second semester
249+
*/
250+
function isSecondSemester (sem2_col) {
251+
const $grade_rows = $('#quickLookup table.grid').find('tr');
252+
if ($grade_rows.eq(1).html().match("S2") != null) {
253+
for (let t = 0; t < $grade_rows.length; t++) {
254+
if (gradeToGPA($grade_rows.eq(t).find('td').get(sem2_col)) !== -1) {
255+
return true;
256+
}
257+
}
258+
}
259+
return false;
260+
}
261+
262+

0 commit comments

Comments
 (0)