Skip to content

Commit 1abcf73

Browse files
[chore]: pull stringified JS into TS file (#1622)
# why - stringified JS is hard to debug & not good practice # what changed - pulled stringified JS from `screenshotUtils.ts` into its own `resolveMaskRect.ts` file - added a generator to compile screenshot scripts into function strings: `packages/core/lib/v3/dom/genScreenshotScripts.ts` # test plan - existing tests should cover this <!-- This is an auto-generated description by cubic. --> --- ## Summary by cubic Replace stringified screenshot DOM code with typed TS functions and generate function strings at build time. This makes screenshot scripts easier to maintain and debug. - **Refactors** - Extracted resolveMaskRect into lib/v3/dom/screenshotScripts/resolveMaskRect.ts with types. - Added genScreenshotScripts.ts to bundle TS scripts and emit build/screenshotScripts.generated.ts. - Updated screenshotUtils to use screenshotScriptSources.resolveMaskRect; included generator in build-dom-scripts. <sup>Written for commit 8ce9a91. Summary will update on new commits. <a href="https://cubic.dev/pr/browserbase/stagehand/pull/1622">Review in cubic</a></sup> <!-- End of auto-generated description by cubic. -->
1 parent fd744ad commit 1abcf73

File tree

5 files changed

+82
-16
lines changed

5 files changed

+82
-16
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import fs from "node:fs";
2+
import path from "node:path";
3+
import { pathToFileURL } from "node:url";
4+
import esbuild from "esbuild";
5+
6+
const here = __dirname;
7+
const srcDir = path.join(here, "./screenshotScripts");
8+
const outDir = path.join(here, "./build");
9+
const entry = path.join(srcDir, "index.ts");
10+
const moduleOut = path.join(outDir, "screenshotScripts.mjs");
11+
12+
async function main(): Promise<void> {
13+
fs.mkdirSync(outDir, { recursive: true });
14+
15+
esbuild.buildSync({
16+
entryPoints: [entry],
17+
bundle: true,
18+
format: "esm",
19+
platform: "browser",
20+
target: "es2020",
21+
minify: true,
22+
outfile: moduleOut,
23+
});
24+
25+
const compiledModule = (await import(
26+
pathToFileURL(moduleOut).href
27+
)) as Record<string, unknown>;
28+
29+
const entries = Object.entries(compiledModule).filter(
30+
([, value]) => typeof value === "function",
31+
);
32+
const sorted = entries.sort(([a], [b]) => a.localeCompare(b));
33+
34+
const scriptMap: Record<string, string> = Object.fromEntries(
35+
sorted.map(([name, fn]) => {
36+
const callable = fn as (...args: unknown[]) => unknown;
37+
return [name, callable.toString()];
38+
}),
39+
);
40+
41+
const banner = `/*\n * AUTO-GENERATED FILE. DO NOT EDIT.\n * Update sources in lib/v3/dom/screenshotScripts and run genScreenshotScripts.ts.\n */`;
42+
43+
const content = `${banner}
44+
export const screenshotScriptSources = ${JSON.stringify(scriptMap, null, 2)} as const;
45+
export type ScreenshotScriptName = keyof typeof screenshotScriptSources;
46+
`;
47+
48+
fs.writeFileSync(
49+
path.join(outDir, "screenshotScripts.generated.ts"),
50+
content,
51+
);
52+
53+
await fs.promises.unlink(moduleOut).catch(() => {});
54+
}
55+
56+
void main();
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { resolveMaskRect } from "./resolveMaskRect";
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
export type MaskRect = {
2+
x: number;
3+
y: number;
4+
width: number;
5+
height: number;
6+
};
7+
8+
export function resolveMaskRect(this: Element | null): MaskRect | null {
9+
if (!this || typeof this.getBoundingClientRect !== "function") return null;
10+
const rect = this.getBoundingClientRect();
11+
if (!rect) return null;
12+
const style = window.getComputedStyle(this);
13+
if (!style) return null;
14+
if (style.visibility === "hidden" || style.display === "none") return null;
15+
if (rect.width <= 0 || rect.height <= 0) return null;
16+
return {
17+
x: rect.left + window.scrollX,
18+
y: rect.top + window.scrollY,
19+
width: rect.width,
20+
height: rect.height,
21+
};
22+
}

packages/core/lib/v3/understudy/screenshotUtils.ts

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import type {
88
ScreenshotScaleOption,
99
} from "../types/public/screenshotTypes";
1010
import { StagehandInvalidArgumentError } from "../types/public/sdkErrors";
11+
import { screenshotScriptSources } from "../dom/build/screenshotScripts.generated";
1112

1213
export type ScreenshotCleanup = () => Promise<void> | void;
1314

@@ -325,21 +326,7 @@ async function resolveMaskRectForObject(
325326
"Runtime.callFunctionOn",
326327
{
327328
objectId,
328-
functionDeclaration: `function() {
329-
if (!this || typeof this.getBoundingClientRect !== 'function') return null;
330-
const rect = this.getBoundingClientRect();
331-
if (!rect) return null;
332-
const style = window.getComputedStyle(this);
333-
if (!style) return null;
334-
if (style.visibility === 'hidden' || style.display === 'none') return null;
335-
if (rect.width <= 0 || rect.height <= 0) return null;
336-
return {
337-
x: rect.left + window.scrollX,
338-
y: rect.top + window.scrollY,
339-
width: rect.width,
340-
height: rect.height,
341-
};
342-
}`,
329+
functionDeclaration: screenshotScriptSources.resolveMaskRect,
343330
returnByValue: true,
344331
},
345332
);

packages/core/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"types": "./dist/index.d.ts",
88
"scripts": {
99
"gen-version": "tsx scripts/gen-version.ts",
10-
"build-dom-scripts": "tsx lib/v3/dom/genDomScripts.ts && tsx lib/v3/dom/genLocatorScripts.ts && tsx lib/v3/dom/genA11yScripts.ts",
10+
"build-dom-scripts": "tsx lib/v3/dom/genDomScripts.ts && tsx lib/v3/dom/genLocatorScripts.ts && tsx lib/v3/dom/genScreenshotScripts.ts && tsx lib/v3/dom/genA11yScripts.ts",
1111
"build-js": "tsup --entry.index lib/v3/index.ts --dts",
1212
"typecheck": "tsc --noEmit",
1313
"build": "pnpm run gen-version && pnpm run build-dom-scripts && pnpm run build-js && pnpm run typecheck",

0 commit comments

Comments
 (0)