Skip to content

Commit 4e52dd8

Browse files
authored
Bump Version to 1.1.2 (#178)
* Add init scripts to the context, not just the page * 1.1.2
1 parent d7d4f59 commit 4e52dd8

File tree

3 files changed

+51
-12
lines changed

3 files changed

+51
-12
lines changed

lib/index.ts

Lines changed: 48 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -293,8 +293,8 @@ export class Stagehand {
293293
llmProvider || new LLMProvider(this.logger, this.enableCaching);
294294
this.env = env;
295295
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;
298298
this.verbose = verbose ?? 0;
299299
this.debugDom = debugDom ?? false;
300300
this.defaultModelName = "gpt-4o";
@@ -339,6 +339,9 @@ export class Stagehand {
339339
});
340340
this.context = context;
341341
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();
342345
this.defaultModelName = modelName;
343346
this.domSettleTimeoutMs = domSettleTimeoutMs ?? this.domSettleTimeoutMs;
344347

@@ -358,16 +361,36 @@ export class Stagehand {
358361

359362
// This can be greatly improved, but the tldr is we put our built web scripts in dist, which should always
360363
// 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({
362373
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+
),
363378
});
364379

365-
await this.page.addInitScript({
380+
await this.context.addInitScript({
366381
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+
),
367386
});
368387

369-
await this.page.addInitScript({
388+
await this.context.addInitScript({
370389
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+
),
371394
});
372395

373396
return { debugUrl, sessionUrl };
@@ -395,20 +418,36 @@ export class Stagehand {
395418
}
396419

397420
// Add initialization scripts
398-
await this.page.addInitScript({
421+
await this.context.addInitScript({
399422
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+
),
400427
});
401428

402-
await this.page.addInitScript({
429+
await this.context.addInitScript({
403430
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+
),
404435
});
405436

406-
await this.page.addInitScript({
437+
await this.context.addInitScript({
407438
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+
),
408443
});
409444

410-
await this.page.addInitScript({
445+
await this.context.addInitScript({
411446
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+
),
412451
});
413452

414453
return { context: this.context };

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@browserbasehq/stagehand",
3-
"version": "1.1.1",
3+
"version": "1.1.2",
44
"description": "An AI web browsing framework focused on simplicity and extensibility.",
55
"main": "./dist/index.js",
66
"module": "./dist/index.js",

0 commit comments

Comments
 (0)