Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove user data dir from chrome startup (WIP) #140

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 22 additions & 41 deletions lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { type Page, type BrowserContext, chromium } from "@playwright/test";
import crypto from "crypto";
import { z } from "zod";
import fs from "fs";
import { act, extract, observe, verifyActCompletion } from "./inference";
import { AvailableModel, LLMProvider } from "./llm/LLMProvider";
import path from "path";
Expand Down Expand Up @@ -71,54 +70,36 @@ async function getBrowser(
level: 0,
});

const tmpDir = fs.mkdtempSync(`/tmp/pwtest`);
fs.mkdirSync(`${tmpDir}/userdir/Default`, { recursive: true });

const defaultPreferences = {
plugins: {
always_open_pdf_externally: true,
},
};

fs.writeFileSync(
`${tmpDir}/userdir/Default/Preferences`,
JSON.stringify(defaultPreferences),
);

const downloadsPath = `${process.cwd()}/downloads`;
fs.mkdirSync(downloadsPath, { recursive: true });

const context = await chromium.launchPersistentContext(
`${tmpDir}/userdir`,
{
acceptDownloads: true,
headless: headless,
viewport: {
width: 1250,
height: 800,
},
locale: "en-US",
timezoneId: "America/New_York",
deviceScaleFactor: 1,
args: [
"--enable-webgl",
"--use-gl=swiftshader",
"--enable-accelerated-2d-canvas",
"--disable-blink-features=AutomationControlled",
"--disable-web-security",
],
bypassCSP: true,
userDataDir: "./user_data",
},
);
const browser = await chromium.launch({
headless: headless,
args: [
"--enable-webgl",
"--use-gl=swiftshader",
"--enable-accelerated-2d-canvas",
"--disable-blink-features=AutomationControlled",
"--disable-web-security",
],
});

logger({
category: "Init",
message: "Local browser started successfully.",
});

const context = await browser.newContext({
viewport: {
width: 1250,
height: 800,
},
locale: "en-US",
deviceScaleFactor: 1,
bypassCSP: true,
});

await applyStealthScripts(context);

const page = await context.newPage();

return { context };
}
}
Expand Down
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
{
"name": "stagehand",
"name": "@browserbase/stagehand",
"version": "1.0.0",
"description": "",
"main": "./dist/index.js",
"module": "./dist/index.js",
"scripts": {
"example": "npm run build-dom-scripts && tsx examples/example.ts",
"example": "npm run build-dom-scripts && tsx examples/2048.ts",
"debug-url": "npm run build-dom-scripts && tsx examples/debugUrl.ts",
"2048": "npm run build-dom-scripts && tsx examples/2048.ts",
"format": "prettier --write .",
"cache:clear": "rm -rf .cache",
"evals": "npm run build-dom-scripts && npx braintrust eval evals/index.eval.ts",
Expand Down
Loading