@@ -208,59 +208,43 @@ chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
208
208
* Function that extract class details and process extracted data
209
209
*/
210
210
function extractClassDetails ( ) {
211
- const rows = document . querySelectorAll ( '.ps_grid-row' ) ; // select all rows in the table
212
- const subjectTitle = document
213
- . getElementById ( 'SSR_CRSE_INFO_V_COURSE_TITLE_LONG' )
214
- . textContent . trim ( ) ;
215
- const subjectCode = document . getElementById ( 'SSR_CRSE_INFO_V_SSS_SUBJ_CATLG' ) . textContent . trim ( ) ;
211
+ const rows = document . querySelectorAll ( '.ps_grid-row' ) ;
212
+ const subjectTitle = document . getElementById ( 'SSR_CRSE_INFO_V_COURSE_TITLE_LONG' ) ?. textContent . trim ( ) || '' ;
213
+ const subjectCode = document . getElementById ( 'SSR_CRSE_INFO_V_SSS_SUBJ_CATLG' ) ?. textContent . trim ( ) || '' ;
216
214
const dataset = { title : subjectTitle , code : subjectCode , class : [ ] } ;
217
215
218
- // tr
219
216
rows . forEach ( ( row ) => {
220
217
const class_ = { } ;
221
- const option = row . querySelector ( '.OPTION_NSFF a' ) . textContent . trim ( ) ;
222
- const status = row . querySelector ( '.ps_box-value' ) . textContent . trim ( ) ;
223
- const psc_disabled = row . classList . contains ( 'psc_disabled' ) ? '1' : '0' ;
224
-
225
- const classElements = row . querySelectorAll ( '.CMPNT_CLASS_NBR a.ps-link' ) ;
226
- const seatsElements = row . querySelectorAll ( '.SEATS span.ps_box-value' ) ;
227
- const dayTimeElements = row . querySelectorAll ( '.DAYS_TIMES .ps_box-longedit' ) ;
228
- const roomElements = row . querySelectorAll ( '.ROOM .ps_box-edit' ) ;
229
- const instructorElements = row . querySelectorAll ( '.INSTRUCTOR .ps_box-longedit' ) || [ ] ;
230
-
231
- // class
232
- const classes = Array . from ( classElements ) . map ( ( el , index ) => {
233
- const classText = el . textContent . trim ( ) ;
234
- const seats = seatsElements [ index ] . innerText
235
- . trim ( )
236
- . split ( ' ' )
237
- . filter ( ( char ) => ! isNaN ( parseInt ( char ) ) )
238
- . join ( ' ' ) ;
239
- const misc = [ ] ;
240
-
241
- // Bundle misc details (day, time, instructor, room)
242
- if ( dayTimeElements [ index ] ) {
243
- const daytimeSpans = dayTimeElements [ index ] . querySelectorAll ( 'span' ) ;
244
- const roomSpans = roomElements [ index ] . querySelectorAll ( 'span' ) ;
245
- const instructorSpans = instructorElements [ index ] ?. querySelectorAll ( 'span' ) || [ ] ;
246
-
247
- daytimeSpans . forEach ( ( span , index_ ) => {
248
- const [ day , time ] = parseDayAndTime ( span . innerHTML . trim ( ) ) ;
249
- const room = roomSpans [ index_ ] . innerText . trim ( ) ;
250
- const instructor = instructorSpans [ index_ ] ?. innerText . trim ( ) || 'no_instructor_displayed' ;
251
- misc . push ( { day, time, room, instructor } ) ;
252
- } ) ;
253
- }
254
-
255
- return { classText, seats, misc } ;
256
- } ) ;
257
-
258
- class_ . option = option ;
259
- class_ . status = status ;
260
- class_ . classes = classes ;
261
-
262
- // For displaying options later on in 'timetable.html'
263
- class_ . psc_disabled = psc_disabled ;
218
+ // Option number
219
+ class_ . option = row . querySelector ( '.OPTION_NSFF a' ) ?. textContent . trim ( ) || '' ;
220
+ // Status
221
+ class_ . status = row . querySelector ( 'td:nth-child(2) .ps_box-value' ) ?. textContent . trim ( ) || '' ;
222
+ // Session
223
+ class_ . session = row . querySelector ( '.SESSION .ps_box-value' ) ?. textContent . trim ( ) || '' ;
224
+ // Disabled
225
+ class_ . psc_disabled = row . classList . contains ( 'psc_disabled' ) ? '1' : '0' ;
226
+
227
+ // All parallel arrays
228
+ const classLinks = row . querySelectorAll ( '.CMPNT_CLASS_NBR a.ps-link' ) ;
229
+ const dateSpans = row . querySelectorAll ( '.DATES .ps_box-value' ) ;
230
+ const dayTimeSpans = row . querySelectorAll ( '.DAYS_TIMES .ps_box-value' ) ;
231
+ const roomSpans = row . querySelectorAll ( '.ROOM .ps_box-value' ) ;
232
+ const instructorSpans = row . querySelectorAll ( '.INSTRUCTOR .ps_box-value' ) ;
233
+ const seatSpans = row . querySelectorAll ( '.SEATS .ps_box-value' ) ;
234
+
235
+ class_ . classes = Array . from ( classLinks ) . map ( ( el , idx ) => ( {
236
+ classText : el . textContent . trim ( ) ,
237
+ dates : dateSpans [ idx ] ?. textContent . trim ( ) || '' ,
238
+ dayTime : dayTimeSpans [ idx ] ?. innerHTML . trim ( ) || '' ,
239
+ room : roomSpans [ idx ] ?. textContent . trim ( ) || '' ,
240
+ instructor : instructorSpans [ idx ] ?. textContent . trim ( ) || 'no_instructor_displayed' ,
241
+ seats : seatSpans [ idx ] ?. textContent . trim ( ) || '' ,
242
+ misc : ( ( ) => {
243
+ // For compatibility with old structure
244
+ const [ day , time ] = parseDayAndTime ( dayTimeSpans [ idx ] ?. innerHTML . trim ( ) || '' ) ;
245
+ return [ { day, time, room : roomSpans [ idx ] ?. textContent . trim ( ) || '' , instructor : instructorSpans [ idx ] ?. textContent . trim ( ) || 'no_instructor_displayed' } ] ;
246
+ } ) ( ) ,
247
+ } ) ) ;
264
248
265
249
dataset . class . push ( class_ ) ;
266
250
} ) ;
0 commit comments