@@ -77,7 +77,8 @@ function main_page () {
7777 const $grade_rows = $ ( '#quickLookup table.grid' ) . find ( 'tr' ) ;
7878 let s1col = 0 ;
7979 let s2col = 0 ;
80-
80+ let current_term = "" ;
81+ let attendance_href = "" ;
8182 if ( $grade_rows . eq ( 1 ) . html ( ) . match ( "S2" ) != null ) {
8283 second_semester = true ;
8384 let curr = 0 ;
@@ -126,6 +127,9 @@ function main_page () {
126127 }
127128 }
128129 }
130+ 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.
131+ current_term = new URL ( attendance_href ) . searchParams . get ( "term" ) ;
132+ }
129133 $ ( "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>` ) ;
130134
131135 if ( second_semester ) {
@@ -142,19 +146,25 @@ function main_page () {
142146 if ( element_list . length > 2 ) {
143147 for ( let i = 2 ; i < element_list . length ; i ++ ) {
144148 const $prev_course = element_list [ i ] ;
145- courses_first_semester . push ( new Course ( $prev_course . getElementsByTagName ( "td" ) [ 0 ] . textContent . trim ( ) ,
146- $prev_course . getElementsByTagName ( "td" ) [ 2 ] . getElementsByTagName ( "a" ) [ 0 ] . href ,
147- $prev_course . getElementsByTagName ( "td" ) [ 1 ] . textContent . trim ( ) ,
148- ) ) ;
149+ if ( $prev_course ?. innerText ?. trim ( ) === "S2" ) {
150+ break ;
151+ }
152+ if ( $prev_course ?. getElementsByTagName ( "td" ) . length > 1 ) {
153+ courses_first_semester . push ( new Course ( $prev_course . getElementsByTagName ( "td" ) [ 0 ] . textContent . trim ( ) ,
154+ $prev_course . getElementsByTagName ( "td" ) [ 2 ] . getElementsByTagName ( "a" ) [ 0 ] . href ,
155+ $prev_course . getElementsByTagName ( "td" ) [ 1 ] . textContent . trim ( ) ,
156+ ) ) ;
157+ }
149158 }
150159 $ ( "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>` ) ;
151160 }
152161 } ) ;
153162 }
154163 $ ( "table[border='0'][cellpadding='3'][cellspacing='1'][width='100%']" ) . prepend ( `<td style="background-color: white;" align="center"><button id="calculateCumulative">Calculate Cumulative GPA</button></td>` ) ;
155164 // passing courses in to possibly include current semester GPA if term has not finished yet.
165+
156166 $ ( "#calculateCumulative" ) . click ( function ( ) {
157- show_cumulative_gpa ( courses ) ;
167+ show_cumulative_gpa ( courses , current_term , second_semester ) ;
158168 } ) ;
159169 saveGradesLocally ( student_name , courses ) ;
160170 // Hypo Grade Calculator
@@ -168,9 +178,9 @@ function main_page () {
168178 } ,
169179 } ) . $mount ( ".hypo-grade-div-fixed" ) ;
170180}
171- function show_cumulative_gpa ( courses ) {
181+ function show_cumulative_gpa ( courses , current_term , current_semester ) {
172182 $ ( "#calculateCumulative" ) . hide ( ) ;
173- calculate_cumulative_gpa ( courses ) . then ( cumulative_gpa => {
183+ calculate_cumulative_gpa ( courses , current_term , current_semester ) . then ( cumulative_gpa => {
174184 $ ( "table[border='0'][cellpadding='3'][cellspacing='1'][width='100%']" ) . prepend ( `<tr><td align="center">Cumulative GPA(9-12): ${ cumulative_gpa . toFixed ( 2 ) } </td></tr>` ) ;
175185 } ) ;
176186}
@@ -200,20 +210,22 @@ function login_page () {
200210 } ) ;
201211}
202212
203- function calculate_cumulative_gpa ( current_courses ) {
213+ function calculate_cumulative_gpa ( current_courses , current_term , current_semester ) {
204214 const list_of_gpas = [ ] ;
205215 const all_courses = [ ] ;
206216 const credit_hour_list = [ ] ;
207217 let element_list = [ ] ;
218+ const current_term_grades = [ ] ;
208219 const fetches = [ ] ;
209220 // Fetches grade history page
210221 const gpas = fetch ( "https://powerschool.sas.edu.sg/guardian/termgrades.html" )
211222 . then ( response => response . text ( ) )
212223 . then ( data => {
213224 const el = document . createElement ( "html" ) ;
214225 el . innerHTML = data ;
226+ const current_term_history = el . getElementsByClassName ( "selected" ) [ 0 ] . textContent . split ( " - " ) [ 0 ] ;
215227 const tabs = el . getElementsByClassName ( "tabs" ) [ 0 ] . getElementsByTagName ( "li" ) ;
216- // Iterate until the end of tabs or until no longer at a high school semester.
228+ // Iterate until the end of tabs or until no longer at a high school semester
217229 for ( let i = 0 ; i < tabs . length && / H S $ / . test ( tabs [ i ] . innerText ) ; i ++ ) {
218230 fetches . push (
219231 fetch ( tabs [ i ] . getElementsByTagName ( "a" ) [ 0 ] . href )
@@ -227,35 +239,50 @@ function calculate_cumulative_gpa (current_courses) {
227239 for ( let t = 2 ; t < element_list . length ; t ++ ) {
228240 if ( element_list [ t ] . innerText . trim ( ) === ( "S2" ) ) {
229241 all_courses . push ( courses ) ;
242+ if ( i === 0 ) {
243+ current_term_grades . push ( courses ) ;
244+ }
230245 courses = [ ] ;
231246 }
232247 if ( element_list [ t ] . getElementsByTagName ( "th" ) . length > 0 ) {
233248 continue ;
234249 } else {
235250 const $prev_course = element_list [ t ] ;
251+ if ( $prev_course ?. getElementsByTagName ( "td" ) . length > 1 ) {
252+ const course = new Course ( $prev_course . getElementsByTagName ( "td" ) [ 0 ] . textContent . trim ( ) , "" ,
253+ $prev_course . getElementsByTagName ( "td" ) [ 1 ] . textContent . trim ( ) ) ;
254+ courses . push ( course ) ;
255+ }
236256 // Creates course object with each course from grade history page
237- const course = new Course ( $prev_course . getElementsByTagName ( "td" ) [ 0 ] . textContent . trim ( ) , "" ,
238- $prev_course . getElementsByTagName ( "td" ) [ 1 ] . textContent . trim ( ) , 0 , "" ) ;
239-
240- courses . push ( course ) ;
241257 }
242258 }
259+ if ( i === 0 ) {
260+ current_term_grades . push ( courses ) ;
261+ }
243262 all_courses . push ( courses ) ;
244263 } ) ) ;
245264 }
246265 // Calculates cumulative GPA based on credit hours per semester and gpa for each semester.
247- let include_current_semester = false ;
248- if ( current_courses . length !== 0 ) {
249- for ( let i = 0 ; i < current_courses . length ; i ++ ) {
250- if ( current_courses [ i ] . link . includes ( "begdate" ) ) {
251- include_current_semester = true ;
266+
267+ const cumulative_gpa = Promise . all ( fetches ) . then ( function ( ) {
268+ let include_current_semester = false ;
269+ if ( current_courses . length !== 0 ) {
270+ for ( let i = 0 ; i < current_courses . length ; i ++ ) {
271+ if ( new URL ( current_courses [ i ] . link ) . searchParams . get ( "begdate" ) ) {
272+ include_current_semester = true ;
273+ }
252274 }
253275 }
254- }
255- if ( include_current_semester ) {
256- all_courses . push ( current_courses ) ;
257- }
258- const cumulative_gpa = Promise . all ( fetches ) . then ( function ( ) {
276+ // Handles edge case where grade history page is updated before semester end
277+ if ( current_term_history === current_term && include_current_semester && current_term_grades . length === 2 && current_semester ) {
278+ include_current_semester = false ;
279+ } else if ( current_term_history === current_term && include_current_semester && current_term_grades . length === 1 && current_semester === false ) {
280+ include_current_semester = false ;
281+ }
282+
283+ if ( include_current_semester ) {
284+ all_courses . push ( current_courses ) ;
285+ }
259286 let total_count = 0 ;
260287 let total_gpa = 0 ;
261288 for ( let i = 0 ; i < all_courses . length ; i ++ ) {
0 commit comments