@@ -77,6 +77,8 @@ function main_page () {
7777 const $grade_rows = $ ( '#quickLookup table.grid' ) . find ( 'tr' ) ;
7878 let s1col = 0 ;
7979 let s2col = 0 ;
80+ let current_semester = 1 ;
81+ let current_term = "" ;
8082
8183 if ( $grade_rows . eq ( 1 ) . html ( ) . match ( "S2" ) != null ) {
8284 second_semester = true ;
@@ -126,6 +128,9 @@ function main_page () {
126128 }
127129 }
128130 }
131+ if ( $grade_rows . eq ( $grade_rows . length - 1 ) . find ( 'a[href*="attendancedates"]' ) [ 0 ] . href && $grade_rows . eq ( $grade_rows . length - 1 ) . find ( 'a[href*="attendancedates"]' ) [ 0 ] . href . includes ( "term" ) ) {
132+ current_term = $grade_rows . eq ( $grade_rows . length - 1 ) . find ( 'a[href*="attendancedates"]' ) [ 0 ] . href . split ( "term=" ) [ 1 ] . split ( "&" ) [ 0 ] ;
133+ }
129134 $ ( "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>` ) ;
130135
131136 if ( second_semester ) {
@@ -142,6 +147,9 @@ function main_page () {
142147 if ( element_list . length > 2 ) {
143148 for ( let i = 2 ; i < element_list . length ; i ++ ) {
144149 const $prev_course = element_list [ i ] ;
150+ if ( $prev_course . innerText . trim ( ) === ( "S2" ) ) {
151+ break ;
152+ }
145153 if ( $prev_course && $prev_course . getElementsByTagName ( "td" ) . length > 1 ) {
146154 courses_first_semester . push ( new Course ( $prev_course . getElementsByTagName ( "td" ) [ 0 ] . textContent . trim ( ) ,
147155 $prev_course . getElementsByTagName ( "td" ) [ 2 ] . getElementsByTagName ( "a" ) [ 0 ] . href ,
@@ -155,8 +163,11 @@ function main_page () {
155163 }
156164 $ ( "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>` ) ;
157165 // passing courses in to possibly include current semester GPA if term has not finished yet.
166+ if ( second_semester ) {
167+ current_semester = 2 ;
168+ }
158169 $ ( "#calculateCumulative" ) . click ( function ( ) {
159- show_cumulative_gpa ( courses ) ;
170+ show_cumulative_gpa ( courses , current_term , current_semester ) ;
160171 } ) ;
161172 saveGradesLocally ( student_name , courses ) ;
162173 // Hypo Grade Calculator
@@ -170,9 +181,9 @@ function main_page () {
170181 } ,
171182 } ) . $mount ( ".hypo-grade-div-fixed" ) ;
172183}
173- function show_cumulative_gpa ( courses ) {
184+ function show_cumulative_gpa ( courses , current_term , current_semester ) {
174185 $ ( "#calculateCumulative" ) . hide ( ) ;
175- calculate_cumulative_gpa ( courses ) . then ( cumulative_gpa => {
186+ calculate_cumulative_gpa ( courses , current_term , current_semester ) . then ( cumulative_gpa => {
176187 $ ( "table[border='0'][cellpadding='3'][cellspacing='1'][width='100%']" ) . prepend ( `<tr><td align="center">Cumulative GPA(9-12): ${ cumulative_gpa . toFixed ( 2 ) } </td></tr>` ) ;
177188 } ) ;
178189}
@@ -202,20 +213,22 @@ function login_page () {
202213 } ) ;
203214}
204215
205- function calculate_cumulative_gpa ( current_courses ) {
216+ function calculate_cumulative_gpa ( current_courses , current_term , current_semester ) {
206217 const list_of_gpas = [ ] ;
207218 const all_courses = [ ] ;
208219 const credit_hour_list = [ ] ;
209220 let element_list = [ ] ;
221+ const current_term_grades = [ ] ;
210222 const fetches = [ ] ;
211223 // Fetches grade history page
212224 const gpas = fetch ( "https://powerschool.sas.edu.sg/guardian/termgrades.html" )
213225 . then ( response => response . text ( ) )
214226 . then ( data => {
215227 const el = document . createElement ( "html" ) ;
216228 el . innerHTML = data ;
229+ const current_term_history = el . getElementsByClassName ( "selected" ) [ 0 ] . textContent . split ( " - " ) [ 0 ] ;
217230 const tabs = el . getElementsByClassName ( "tabs" ) [ 0 ] . getElementsByTagName ( "li" ) ;
218- // Iterate until the end of tabs or until no longer at a high school semester.
231+ // Iterate until the end of tabs or until no longer at a high school semester
219232 for ( let i = 0 ; i < tabs . length && / H S $ / . test ( tabs [ i ] . innerText ) ; i ++ ) {
220233 fetches . push (
221234 fetch ( tabs [ i ] . getElementsByTagName ( "a" ) [ 0 ] . href )
@@ -229,6 +242,9 @@ function calculate_cumulative_gpa (current_courses) {
229242 for ( let t = 2 ; t < element_list . length ; t ++ ) {
230243 if ( element_list [ t ] . innerText . trim ( ) === ( "S2" ) ) {
231244 all_courses . push ( courses ) ;
245+ if ( i === 0 ) {
246+ current_term_grades . push ( courses ) ;
247+ }
232248 courses = [ ] ;
233249 }
234250 if ( element_list [ t ] . getElementsByTagName ( "th" ) . length > 0 ) {
@@ -243,22 +259,33 @@ function calculate_cumulative_gpa (current_courses) {
243259 // Creates course object with each course from grade history page
244260 }
245261 }
262+ if ( i === 0 ) {
263+ current_term_grades . push ( courses ) ;
264+ }
246265 all_courses . push ( courses ) ;
247266 } ) ) ;
248267 }
249268 // Calculates cumulative GPA based on credit hours per semester and gpa for each semester.
250- let include_current_semester = false ;
251- if ( current_courses . length !== 0 ) {
252- for ( let i = 0 ; i < current_courses . length ; i ++ ) {
253- if ( current_courses [ i ] . link . includes ( "begdate" ) ) {
254- include_current_semester = true ;
269+
270+ const cumulative_gpa = Promise . all ( fetches ) . then ( function ( ) {
271+ let include_current_semester = false ;
272+ if ( current_courses . length !== 0 ) {
273+ for ( let i = 0 ; i < current_courses . length ; i ++ ) {
274+ if ( current_courses [ i ] . link . includes ( "begdate" ) ) {
275+ include_current_semester = true ;
276+ }
255277 }
256278 }
257- }
258- if ( include_current_semester ) {
259- all_courses . push ( current_courses ) ;
260- }
261- const cumulative_gpa = Promise . all ( fetches ) . then ( function ( ) {
279+ // Handles edge case where grade history page is updated before semester end
280+ if ( current_term_history === current_term && include_current_semester && current_term_grades . length === 2 && current_semester === 2 ) {
281+ include_current_semester = false ;
282+ } else if ( current_term_history === current_term && include_current_semester && current_term_grades . length === 1 && current_semester === 1 ) {
283+ include_current_semester = false ;
284+ }
285+
286+ if ( include_current_semester ) {
287+ all_courses . push ( current_courses ) ;
288+ }
262289 let total_count = 0 ;
263290 let total_gpa = 0 ;
264291 for ( let i = 0 ; i < all_courses . length ; i ++ ) {
0 commit comments