@@ -293,8 +293,8 @@ export class Stagehand {
293
293
llmProvider || new LLMProvider ( this . logger , this . enableCaching ) ;
294
294
this . env = env ;
295
295
this . observations = { } ;
296
- this . apiKey = apiKey || process . env . BROWSERBASE_API_KEY ;
297
- this . projectId = projectId || process . env . BROWSERBASE_PROJECT_ID ;
296
+ this . apiKey = apiKey ?? process . env . BROWSERBASE_API_KEY ;
297
+ this . projectId = projectId ?? process . env . BROWSERBASE_PROJECT_ID ;
298
298
this . verbose = verbose ?? 0 ;
299
299
this . debugDom = debugDom ?? false ;
300
300
this . defaultModelName = "gpt-4o" ;
@@ -339,6 +339,9 @@ export class Stagehand {
339
339
} ) ;
340
340
this . context = context ;
341
341
this . page = context . pages ( ) [ 0 ] ;
342
+ // Redundant but needed for users who are re-connecting to a previously-created session
343
+ await this . page . waitForLoadState ( "domcontentloaded" ) ;
344
+ await this . _waitForSettledDom ( ) ;
342
345
this . defaultModelName = modelName ;
343
346
this . domSettleTimeoutMs = domSettleTimeoutMs ?? this . domSettleTimeoutMs ;
344
347
@@ -358,16 +361,36 @@ export class Stagehand {
358
361
359
362
// This can be greatly improved, but the tldr is we put our built web scripts in dist, which should always
360
363
// be one level above our running directly across evals, example, and as a package
361
- await this . page . addInitScript ( {
364
+ await this . context . addInitScript ( {
365
+ path : path . join ( __dirname , ".." , "dist" , "dom" , "build" , "xpathUtils.js" ) ,
366
+ content : fs . readFileSync (
367
+ path . join ( __dirname , ".." , "dist" , "dom" , "build" , "xpathUtils.js" ) ,
368
+ "utf8" ,
369
+ ) ,
370
+ } ) ;
371
+
372
+ await this . context . addInitScript ( {
362
373
path : path . join ( __dirname , ".." , "dist" , "dom" , "build" , "process.js" ) ,
374
+ content : fs . readFileSync (
375
+ path . join ( __dirname , ".." , "dist" , "dom" , "build" , "process.js" ) ,
376
+ "utf8" ,
377
+ ) ,
363
378
} ) ;
364
379
365
- await this . page . addInitScript ( {
380
+ await this . context . addInitScript ( {
366
381
path : path . join ( __dirname , ".." , "dist" , "dom" , "build" , "utils.js" ) ,
382
+ content : fs . readFileSync (
383
+ path . join ( __dirname , ".." , "dist" , "dom" , "build" , "utils.js" ) ,
384
+ "utf8" ,
385
+ ) ,
367
386
} ) ;
368
387
369
- await this . page . addInitScript ( {
388
+ await this . context . addInitScript ( {
370
389
path : path . join ( __dirname , ".." , "dist" , "dom" , "build" , "debug.js" ) ,
390
+ content : fs . readFileSync (
391
+ path . join ( __dirname , ".." , "dist" , "dom" , "build" , "debug.js" ) ,
392
+ "utf8" ,
393
+ ) ,
371
394
} ) ;
372
395
373
396
return { debugUrl, sessionUrl } ;
@@ -395,20 +418,36 @@ export class Stagehand {
395
418
}
396
419
397
420
// Add initialization scripts
398
- await this . page . addInitScript ( {
421
+ await this . context . addInitScript ( {
399
422
path : path . join ( __dirname , ".." , "dist" , "dom" , "build" , "xpathUtils.js" ) ,
423
+ content : fs . readFileSync (
424
+ path . join ( __dirname , ".." , "dist" , "dom" , "build" , "xpathUtils.js" ) ,
425
+ "utf8" ,
426
+ ) ,
400
427
} ) ;
401
428
402
- await this . page . addInitScript ( {
429
+ await this . context . addInitScript ( {
403
430
path : path . join ( __dirname , ".." , "dist" , "dom" , "build" , "process.js" ) ,
431
+ content : fs . readFileSync (
432
+ path . join ( __dirname , ".." , "dist" , "dom" , "build" , "process.js" ) ,
433
+ "utf8" ,
434
+ ) ,
404
435
} ) ;
405
436
406
- await this . page . addInitScript ( {
437
+ await this . context . addInitScript ( {
407
438
path : path . join ( __dirname , ".." , "dist" , "dom" , "build" , "utils.js" ) ,
439
+ content : fs . readFileSync (
440
+ path . join ( __dirname , ".." , "dist" , "dom" , "build" , "utils.js" ) ,
441
+ "utf8" ,
442
+ ) ,
408
443
} ) ;
409
444
410
- await this . page . addInitScript ( {
445
+ await this . context . addInitScript ( {
411
446
path : path . join ( __dirname , ".." , "dist" , "dom" , "build" , "debug.js" ) ,
447
+ content : fs . readFileSync (
448
+ path . join ( __dirname , ".." , "dist" , "dom" , "build" , "debug.js" ) ,
449
+ "utf8" ,
450
+ ) ,
412
451
} ) ;
413
452
414
453
return { context : this . context } ;
0 commit comments