@@ -20,6 +20,7 @@ import (
2020
2121 "github.com/chromedp/cdproto/cdp"
2222 "github.com/chromedp/cdproto/network"
23+ "github.com/chromedp/cdproto/runtime"
2324 "github.com/chromedp/chromedp"
2425)
2526
@@ -38,7 +39,14 @@ func GetEnv(name string) (string, error) {
3839func InitChromeDp () (chromedpCtx context.Context , cancelFnc context.CancelFunc ) {
3940 log .Printf ("Initializing chromedp..." )
4041 if Headless {
41- chromedpCtx , cancelFnc = chromedp .NewContext (context .Background ())
42+ opts := append (chromedp .DefaultExecAllocatorOptions [:],
43+ chromedp .Flag ("headless" , true ),
44+ chromedp .Flag ("no-sandbox" , true ),
45+ chromedp .Flag ("disable-dev-shm-usage" , true ),
46+ chromedp .Flag ("disable-gpu" , true ),
47+ )
48+ allocCtx , _ := chromedp .NewExecAllocator (context .Background (), opts ... )
49+ chromedpCtx , cancelFnc = chromedp .NewContext (allocCtx )
4250 } else {
4351 allocCtx , _ := chromedp .NewExecAllocator (context .Background ())
4452 chromedpCtx , cancelFnc = chromedp .NewContext (allocCtx )
@@ -93,6 +101,9 @@ func RefreshToken(chromedpCtx context.Context) map[string][]string {
93101 chromedp .Navigate (`https://coursebook.utdallas.edu/` ),
94102 chromedp .ActionFunc (func (ctx context.Context ) error {
95103 cookies , err := network .GetCookies ().Do (ctx )
104+ if err != nil {
105+ return err
106+ }
96107 cookieStrs = make ([]string , len (cookies ))
97108 gotToken := false
98109 for i , cookie := range cookies {
@@ -105,7 +116,7 @@ func RefreshToken(chromedpCtx context.Context) map[string][]string {
105116 if ! gotToken {
106117 return errors .New ("failed to get a new token" )
107118 }
108- return err
119+ return nil
109120 }),
110121 )
111122 if r != nil && r .Status != 200 {
@@ -164,10 +175,12 @@ func RefreshAstraToken(chromedpCtx context.Context) map[string][]string {
164175
165176 // Save all cookies to string
166177 cookieStr := ""
167- _ , err = chromedp .RunResponse (chromedpCtx ,
168- chromedp .WaitVisible (`body` , chromedp .ByQuery ),
178+ err = chromedp .Run (chromedpCtx ,
169179 chromedp .ActionFunc (func (ctx context.Context ) error {
170180 cookies , err := network .GetCookies ().Do (ctx )
181+ if err != nil {
182+ return err
183+ }
171184 gotToken := false
172185 for _ , cookie := range cookies {
173186 cookieStr = fmt .Sprintf ("%s%s=%s; " , cookieStr , cookie .Name , cookie .Value )
@@ -179,7 +192,7 @@ func RefreshAstraToken(chromedpCtx context.Context) map[string][]string {
179192 if ! gotToken {
180193 return errors .New ("failed to get a new token" )
181194 }
182- return err
195+ return nil
183196 }),
184197 )
185198 if err != nil {
@@ -278,42 +291,33 @@ func Retry(action func() error, maxRetries int, retryCallback func(numRetries in
278291
279292// Get all the available course prefixes
280293func GetCoursePrefixes (chromedpCtx context.Context ) []string {
281- // Refresh the token
294+ // Might need to refresh the token every time we get new course prefixes in the future
282295 // refreshToken(chromedpCtx)
283296
284- log .Printf ("Finding course prefix nodes..." )
285-
286297 var coursePrefixes []string
287- var coursePrefixNodes [] * cdp. Node
298+ log . Println ( "Finding course prefixes..." )
288299
289300 // Get option elements for course prefix dropdown
290- err := chromedp .Run (chromedpCtx ,
301+ _ , err := chromedp .RunResponse (chromedpCtx ,
291302 chromedp .Navigate ("https://coursebook.utdallas.edu" ),
292- chromedp .Nodes ("select#combobox_cp option" , & coursePrefixNodes , chromedp .ByQueryAll ),
303+ chromedp .QueryAfter ("select#combobox_cp option" ,
304+ func (ctx context.Context , _ runtime.ExecutionContextID , nodes ... * cdp.Node ) error {
305+ for _ , node := range nodes [1 :] {
306+ coursePrefixes = append (coursePrefixes , node .AttributeValue ("value" ))
307+ }
308+ return nil
309+ },
310+ ),
293311 )
294-
295312 if err != nil {
296313 log .Panic (err )
297314 }
298-
299- log .Println ("Found the course prefix nodes!" )
300-
301- log .Println ("Finding course prefixes..." )
302-
303- // Remove the first option due to it being empty
304- coursePrefixNodes = coursePrefixNodes [1 :]
305-
306- // Get the value of each option and append to coursePrefixes
307- for _ , node := range coursePrefixNodes {
308- coursePrefixes = append (coursePrefixes , node .AttributeValue ("value" ))
309- }
310-
311- log .Println ("Found the course prefixes!" )
312-
315+ log .Printf ("Found the %d course prefixes!" , len (coursePrefixes ))
313316 return coursePrefixes
314317}
315318
316- func ConvertFromInterface [T string | float64 ](value interface {}) * T {
319+ // Convert the value of any type to either string or float64
320+ func ConvertFromInterface [T string | float64 ](value any ) * T {
317321 if parsed , ok := value .(T ); ok {
318322 return & parsed
319323 }
0 commit comments