Skip to content

Add an option to use the viewport size of the host operating system window #874

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

Open
wants to merge 2 commits 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
4 changes: 3 additions & 1 deletion docs/reference/initialization_config.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,9 @@ This constructor is used to create an instance of Stagehand.
</ResponseField>
</Expandable>
</ResponseField>
<ResponseField name="viewport" type="object">
<ResponseField name="viewport" type="object | null">
The size of the viewport. If set to `null`, the viewport will be set to the size of the operating system window. Defaults to `{ width: 1024, height: 768 }`.

<Expandable title="properties">
<ResponseField name="width" type="number">
Width of the viewport.
Expand Down
16 changes: 11 additions & 5 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,13 +280,19 @@ async function getBrowser(
const context = await chromium.launchPersistentContext(userDataDir, {
acceptDownloads: localBrowserLaunchOptions?.acceptDownloads ?? true,
headless: localBrowserLaunchOptions?.headless ?? headless,
viewport: {
width: localBrowserLaunchOptions?.viewport?.width ?? 1024,
height: localBrowserLaunchOptions?.viewport?.height ?? 768,
},
viewport:
localBrowserLaunchOptions?.viewport === null
? null
: {
width: localBrowserLaunchOptions?.viewport?.width ?? 1024,
height: localBrowserLaunchOptions?.viewport?.height ?? 768,
},
locale: localBrowserLaunchOptions?.locale ?? "en-US",
timezoneId: localBrowserLaunchOptions?.timezoneId ?? "America/New_York",
deviceScaleFactor: localBrowserLaunchOptions?.deviceScaleFactor ?? 1,
deviceScaleFactor:
localBrowserLaunchOptions?.viewport === null
? undefined
: (localBrowserLaunchOptions?.deviceScaleFactor ?? 1),
args: localBrowserLaunchOptions?.args ?? [
"--disable-blink-features=AutomationControlled",
],
Expand Down
2 changes: 1 addition & 1 deletion types/stagehand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ export interface LocalBrowserLaunchOptions {
urlFilter?: string | RegExp;
};
recordVideo?: { dir: string; size?: { width: number; height: number } };
viewport?: { width: number; height: number };
viewport?: { width: number; height: number } | null;
deviceScaleFactor?: number;
timezoneId?: string;
bypassCSP?: boolean;
Expand Down