mirror of
https://github.com/farcasclaudiu/openclaw.git
synced 2026-06-28 17:01:53 +03:00
refactor: lint cleanups and helpers
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import type { BrowserFormField } from "./client-actions-core.js";
|
||||
import {
|
||||
type BrowserConsoleMessage,
|
||||
ensurePageState,
|
||||
@@ -168,18 +169,29 @@ export async function typeViaPlaywright(opts: {
|
||||
export async function fillFormViaPlaywright(opts: {
|
||||
cdpPort: number;
|
||||
targetId?: string;
|
||||
fields: Array<Record<string, unknown>>;
|
||||
fields: BrowserFormField[];
|
||||
}): Promise<void> {
|
||||
const page = await getPageForTargetId(opts);
|
||||
ensurePageState(page);
|
||||
for (const field of opts.fields) {
|
||||
const ref = String(field.ref ?? "").trim();
|
||||
const type = String(field.type ?? "").trim();
|
||||
const value = String(field.value ?? "");
|
||||
const ref = field.ref.trim();
|
||||
const type = field.type.trim();
|
||||
const rawValue = field.value;
|
||||
const value =
|
||||
typeof rawValue === "string"
|
||||
? rawValue
|
||||
: typeof rawValue === "number" || typeof rawValue === "boolean"
|
||||
? String(rawValue)
|
||||
: "";
|
||||
if (!ref || !type) continue;
|
||||
const locator = refLocator(page, ref);
|
||||
if (type === "checkbox" || type === "radio") {
|
||||
await locator.setChecked(value === "true");
|
||||
const checked =
|
||||
rawValue === true ||
|
||||
rawValue === 1 ||
|
||||
rawValue === "1" ||
|
||||
rawValue === "true";
|
||||
await locator.setChecked(checked);
|
||||
continue;
|
||||
}
|
||||
await locator.fill(value);
|
||||
@@ -199,18 +211,47 @@ export async function evaluateViaPlaywright(opts: {
|
||||
if (opts.ref) {
|
||||
const locator = refLocator(page, opts.ref);
|
||||
return await locator.evaluate((el, fnBody) => {
|
||||
const runner = new Function(
|
||||
"element",
|
||||
`"use strict"; const fn = ${fnBody}; return fn(element);`,
|
||||
) as (element: Element) => unknown;
|
||||
return runner(el as Element);
|
||||
const compileRunner = (body: string) => {
|
||||
const inner = `"use strict"; const candidate = ${body}; return typeof candidate === "function" ? candidate(element) : candidate;`;
|
||||
// This intentionally evaluates user-supplied code in the browser context.
|
||||
// oxlint-disable-next-line typescript-eslint/no-implied-eval
|
||||
return new Function("element", inner) as (element: Element) => unknown;
|
||||
};
|
||||
let compiled: unknown;
|
||||
try {
|
||||
compiled = compileRunner(fnBody);
|
||||
} catch (err) {
|
||||
const message =
|
||||
err instanceof Error
|
||||
? err.message
|
||||
: typeof err === "string"
|
||||
? err
|
||||
: "invalid expression";
|
||||
throw new Error(`Invalid evaluate function: ${message}`);
|
||||
}
|
||||
return (compiled as (element: Element) => unknown)(el as Element);
|
||||
}, fnText);
|
||||
}
|
||||
return await page.evaluate((fnBody) => {
|
||||
const runner = new Function(
|
||||
`"use strict"; const fn = ${fnBody}; return fn();`,
|
||||
) as () => unknown;
|
||||
return runner();
|
||||
const compileRunner = (body: string) => {
|
||||
const inner = `"use strict"; const candidate = ${body}; return typeof candidate === "function" ? candidate() : candidate;`;
|
||||
// This intentionally evaluates user-supplied code in the browser context.
|
||||
// oxlint-disable-next-line typescript-eslint/no-implied-eval
|
||||
return new Function(inner) as () => unknown;
|
||||
};
|
||||
let compiled: unknown;
|
||||
try {
|
||||
compiled = compileRunner(fnBody);
|
||||
} catch (err) {
|
||||
const message =
|
||||
err instanceof Error
|
||||
? err.message
|
||||
: typeof err === "string"
|
||||
? err
|
||||
: "invalid expression";
|
||||
throw new Error(`Invalid evaluate function: ${message}`);
|
||||
}
|
||||
return (compiled as () => unknown)();
|
||||
}, fnText);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user