fix: tolerate trailing semicolons in browser evaluate

This commit is contained in:
Peter Steinberger
2026-01-03 21:37:06 +00:00
parent 609aa4a7c5
commit 52edef225b
3 changed files with 42 additions and 2 deletions

View File

@@ -18,6 +18,7 @@
- Discord: emit system events for reaction add/remove with per-guild reaction notifications (off|own|all|allowlist) (#140) — thanks @thewilloftheshadow.
### Fixes
- Browser tools: tolerate trailing semicolons in evaluate expressions (#153) — thanks @azade-c.
- Auto-reply: drop final payloads when block streaming to avoid duplicate Discord sends.
- Bash tool: default auto-background delay to 10s.
- Telegram: chunk block-stream replies to avoid “message is too long” errors (#124) — thanks @mukhtharcm.

View File

@@ -232,4 +232,41 @@ describe("pw-tools-core", () => {
expect(dismiss).toHaveBeenCalled();
expect(accept).not.toHaveBeenCalled();
});
it("evaluates expressions with trailing semicolons", async () => {
const evaluate = vi.fn(
async (fn: (body: string) => unknown, body: string) => fn(body),
);
currentPage = { evaluate };
const mod = await importModule();
const result = await mod.evaluateViaPlaywright({
cdpUrl: "http://127.0.0.1:18792",
fn: "() => 42;",
});
expect(result).toBe(42);
expect(evaluate).toHaveBeenCalled();
});
it("evaluates element expressions with trailing semicolons", async () => {
const evaluate = vi.fn(
async (
fn: (el: { tagName: string }, body: string) => unknown,
body: string,
) => fn({ tagName: "DIV" }, body),
);
currentRefLocator = { evaluate };
currentPage = {};
const mod = await importModule();
const result = await mod.evaluateViaPlaywright({
cdpUrl: "http://127.0.0.1:18792",
fn: "(el) => el.tagName;",
ref: "1",
});
expect(result).toBe("DIV");
expect(sessionMocks.refLocator).toHaveBeenCalledWith(currentPage, "1");
});
});

View File

@@ -207,6 +207,8 @@ export async function evaluateViaPlaywright(opts: {
}): Promise<unknown> {
const fnText = String(opts.fn ?? "").trim();
if (!fnText) throw new Error("function is required");
const fnBody = fnText.replace(/[;\s]+$/g, "");
if (!fnBody) throw new Error("function is required");
const page = await getPageForTargetId(opts);
ensurePageState(page);
if (opts.ref) {
@@ -226,7 +228,7 @@ export async function evaluateViaPlaywright(opts: {
}
`,
) as (el: Element, fnBody: string) => unknown;
return await locator.evaluate(elementEvaluator, fnText);
return await locator.evaluate(elementEvaluator, fnBody);
}
// Use Function constructor at runtime to avoid esbuild adding __name helper
// which doesn't exist in the browser context
@@ -242,7 +244,7 @@ export async function evaluateViaPlaywright(opts: {
}
`,
) as (fnBody: string) => unknown;
return await page.evaluate(browserEvaluator, fnText);
return await page.evaluate(browserEvaluator, fnBody);
}
export async function armFileUploadViaPlaywright(opts: {