refactor(browser): prune browser automation surface

This commit is contained in:
Peter Steinberger
2025-12-20 02:53:22 +00:00
parent 849446ae17
commit 6fc30962d6
19 changed files with 85 additions and 802 deletions

View File

@@ -2,7 +2,6 @@ import type { Command } from "commander";
import {
browserDom,
browserEval,
browserQuery,
browserScreenshot,
browserSnapshot,
@@ -13,31 +12,6 @@ import { danger } from "../globals.js";
import { defaultRuntime } from "../runtime.js";
import type { BrowserParentOpts } from "./browser-cli-shared.js";
async function readStdin(): Promise<string> {
const chunks: string[] = [];
return await new Promise((resolve, reject) => {
process.stdin.setEncoding("utf8");
process.stdin.on("data", (chunk) => chunks.push(String(chunk)));
process.stdin.on("end", () => resolve(chunks.join("")));
process.stdin.on("error", reject);
});
}
async function readTextFromSource(opts: {
js?: string;
jsFile?: string;
jsStdin?: boolean;
}): Promise<string> {
if (opts.jsFile) {
const fs = await import("node:fs/promises");
return await fs.readFile(opts.jsFile, "utf8");
}
if (opts.jsStdin) {
return await readStdin();
}
return opts.js ?? "";
}
export function registerBrowserInspectCommands(
browser: Command,
parentOpts: (cmd: Command) => BrowserParentOpts,
@@ -80,44 +54,6 @@ export function registerBrowserInspectCommands(
}
});
browser
.command("eval")
.description("Run JavaScript in the active tab")
.argument("[js]", "JavaScript expression")
.option("--js-file <path>", "Read JavaScript from a file")
.option("--js-stdin", "Read JavaScript from stdin", false)
.option("--target-id <id>", "CDP target id (or unique prefix)")
.option("--await", "Await promise result", false)
.action(async (js: string | undefined, opts, cmd) => {
const parent = parentOpts(cmd);
const baseUrl = resolveBrowserControlUrl(parent?.url);
try {
const source = await readTextFromSource({
js,
jsFile: opts.jsFile,
jsStdin: Boolean(opts.jsStdin),
});
if (!source.trim()) {
defaultRuntime.error(danger("Missing JavaScript input."));
defaultRuntime.exit(1);
return;
}
const result = await browserEval(baseUrl, {
js: source,
targetId: opts.targetId?.trim() || undefined,
awaitPromise: Boolean(opts.await),
});
if (parent?.json) {
defaultRuntime.log(JSON.stringify(result, null, 2));
return;
}
defaultRuntime.log(JSON.stringify(result.result, null, 2));
} catch (err) {
defaultRuntime.error(danger(String(err)));
defaultRuntime.exit(1);
}
});
browser
.command("query")
.description("Query selector matches")