Skip to content

Commit 0c4b1e7

Browse files
miguelg719kamath
andauthored
Override screenshot logic (#589)
* added screenshot proxy override with CDP * changeset * Update .changeset/free-pots-move.md Co-authored-by: Anirudh Kamath <[email protected]> --------- Co-authored-by: Anirudh Kamath <[email protected]>
1 parent dfd24e6 commit 0c4b1e7

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
lines changed

Diff for: .changeset/free-pots-move.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@browserbasehq/stagehand": patch
3+
---
4+
5+
Added CDP support for screenshots, find more about the benefits here: https://docs.browserbase.com/features/screenshots#why-use-cdp-for-screenshots%3F

Diff for: lib/StagehandPage.ts

+35
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,41 @@ export class StagehandPage {
251251
};
252252
}
253253

254+
// Handle screenshots with CDP
255+
if (prop === "screenshot") {
256+
return async (
257+
options: {
258+
type?: "png" | "jpeg";
259+
quality?: number;
260+
fullPage?: boolean;
261+
clip?: { x: number; y: number; width: number; height: number };
262+
omitBackground?: boolean;
263+
} = {},
264+
) => {
265+
const cdpOptions: Record<string, unknown> = {
266+
format: options.type === "jpeg" ? "jpeg" : "png",
267+
quality: options.quality,
268+
clip: options.clip,
269+
omitBackground: options.omitBackground,
270+
fromSurface: true,
271+
};
272+
273+
if (options.fullPage) {
274+
cdpOptions.captureBeyondViewport = true;
275+
}
276+
277+
const data = await this.sendCDP<{ data: string }>(
278+
"Page.captureScreenshot",
279+
cdpOptions,
280+
);
281+
282+
// Convert base64 to buffer
283+
const buffer = Buffer.from(data.data, "base64");
284+
285+
return buffer;
286+
};
287+
}
288+
254289
// Handle goto specially
255290
if (prop === "goto") {
256291
return async (url: string, options: GotoOptions) => {

Diff for: stagehand.config.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const StagehandConfig: ConstructorParams = {
1010
: "LOCAL",
1111
apiKey: process.env.BROWSERBASE_API_KEY /* API key for authentication */,
1212
projectId: process.env.BROWSERBASE_PROJECT_ID /* Project identifier */,
13-
debugDom: true /* Enable DOM debugging features */,
13+
debugDom: false /* Enable DOM debugging features */,
1414
headless: false /* Run browser in headless mode */,
1515
logger: (message: LogLine) =>
1616
console.log(logLineToString(message)) /* Custom logging function */,

0 commit comments

Comments
 (0)