pi-browser-harness 0.12.0 → 0.13.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +36 -0
- package/README.md +86 -0
- package/package.json +12 -6
- package/skills/pi-browser-harness/SKILL.md +57 -2
- package/src/cdp/commands.ts +200 -14
- package/src/cdp/console-buffer.ts +37 -23
- package/src/cdp/discovery.ts +57 -25
- package/src/cdp/events.ts +8 -3
- package/src/cdp/network-buffer.ts +38 -32
- package/src/cdp/ownership.ts +22 -4
- package/src/cdp/record-store.ts +2 -2
- package/src/cdp/session.ts +320 -217
- package/src/cdp/target-factory.ts +34 -15
- package/src/cdp/types.ts +17 -1
- package/src/client.ts +76 -43
- package/src/daemon/bridge.ts +110 -349
- package/src/daemon/connection.ts +323 -0
- package/src/daemon/index.ts +77 -48
- package/src/daemon/lines.ts +40 -0
- package/src/daemon/main.ts +12 -0
- package/src/daemon/protocol.ts +55 -25
- package/src/daemon/server.ts +100 -63
- package/src/daemon/socket.ts +10 -0
- package/src/daemon/spawn.ts +21 -22
- package/src/daemon/transport.ts +80 -49
- package/src/domains/ax-tree.ts +8 -8
- package/src/domains/cdp-call.ts +14 -7
- package/src/domains/click.ts +2 -2
- package/src/domains/concurrency.ts +50 -0
- package/src/domains/console.ts +20 -64
- package/src/domains/drag.ts +26 -28
- package/src/domains/element-call.ts +102 -68
- package/src/domains/files.ts +13 -9
- package/src/domains/form-batch.ts +13 -10
- package/src/domains/form.ts +78 -107
- package/src/domains/headers.ts +2 -2
- package/src/domains/history.ts +4 -4
- package/src/domains/io.ts +35 -0
- package/src/domains/isolated-tab.ts +6 -12
- package/src/domains/js.ts +65 -44
- package/src/domains/keyboard.ts +14 -11
- package/src/domains/navigate.ts +102 -66
- package/src/domains/network.ts +55 -95
- package/src/domains/page/fill.page.ts +139 -0
- package/src/domains/page/form.page.ts +61 -0
- package/src/domains/page/inject.ts +37 -0
- package/src/domains/page/keyboard.page.ts +16 -0
- package/src/domains/page/live-value.page.ts +10 -0
- package/src/domains/page/read-page.page.ts +51 -0
- package/src/domains/page/serp.page.ts +39 -0
- package/src/domains/page.ts +18 -51
- package/src/domains/poll.ts +42 -0
- package/src/domains/readpage/read-page.ts +48 -53
- package/src/domains/ref-resolve.ts +43 -29
- package/src/domains/ref-signature.ts +11 -0
- package/src/domains/render.ts +12 -23
- package/src/domains/screenshot-capture.ts +4 -2
- package/src/domains/screenshot.ts +13 -10
- package/src/domains/scroll.ts +4 -4
- package/src/domains/search/google-serp.ts +1 -1
- package/src/domains/search/web-search.ts +38 -33
- package/src/domains/snapshot.ts +71 -73
- package/src/domains/tab-resolve.ts +36 -0
- package/src/domains/tabs.ts +18 -69
- package/src/domains/target.ts +1 -1
- package/src/index.ts +90 -33
- package/src/profile/bind.ts +45 -14
- package/src/profile/browser-process.ts +50 -12
- package/src/profile/command.ts +20 -12
- package/src/profile/launch.ts +13 -9
- package/src/profile/list.ts +9 -13
- package/src/profile/store.ts +2 -1
- package/src/prompt.ts +32 -7
- package/src/registry.ts +65 -4
- package/src/schemas/parse.ts +2 -2
- package/src/setup.ts +28 -19
- package/src/state.ts +2 -1
- package/src/util/debug.ts +7 -1
- package/src/util/guards.ts +3 -0
- package/src/util/mutex.ts +1 -1
- package/src/util/paths.ts +3 -2
- package/src/util/result.ts +8 -0
- package/src/util/sharp-shim.ts +18 -11
- package/src/util/sleep.ts +18 -0
- package/src/util/tool.ts +163 -114
- package/src/util/truncate.ts +1 -1
- package/tsconfig.json +2 -1
- package/src/domains/fill-engine.ts +0 -80
package/src/domains/network.ts
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
import { Type } from "typebox";
|
|
2
|
-
import { Markdown
|
|
2
|
+
import { Markdown } from "@mariozechner/pi-tui";
|
|
3
3
|
import { getMarkdownTheme, keyHint } from "@mariozechner/pi-coding-agent";
|
|
4
4
|
import { type Result, err, ok } from "../util/result";
|
|
5
5
|
import { formatBytes } from "../util/truncate";
|
|
6
|
-
import { defineBrowserTool, type ToolErr, type ToolOk } from "../util/tool";
|
|
6
|
+
import { type DetailsOf, defineBrowserTool, type ToolErr, type ToolOk } from "../util/tool";
|
|
7
7
|
import { cdpCall } from "./cdp-call";
|
|
8
|
+
import { deadlineSignal, runLimited } from "./concurrency";
|
|
8
9
|
import { applyTruncation } from "../util/truncate";
|
|
9
|
-
import {
|
|
10
|
-
import type {
|
|
10
|
+
import { type NetworkRecord, NetworkRecordSchema } from "../cdp/network-buffer";
|
|
11
|
+
import type { BrowserClient } from "../client";
|
|
11
12
|
|
|
12
13
|
const HttpGetArgs = Type.Object({
|
|
13
14
|
url: Type.String({ description: "URL to GET" }),
|
|
@@ -31,7 +32,7 @@ export const httpGetTool = defineBrowserTool({
|
|
|
31
32
|
ensureAlive: false,
|
|
32
33
|
async handler(args): Promise<Result<ToolOk, ToolErr>> {
|
|
33
34
|
const ac = new AbortController();
|
|
34
|
-
const timer = setTimeout(() => ac.abort(), (args.timeout ?? 20) * 1000);
|
|
35
|
+
const timer = setTimeout(() => { ac.abort(); }, (args.timeout ?? 20) * 1000);
|
|
35
36
|
try {
|
|
36
37
|
const fetchInit: RequestInit = { signal: ac.signal };
|
|
37
38
|
if (args.headers !== undefined) fetchInit.headers = args.headers;
|
|
@@ -86,6 +87,7 @@ const NetworkRequestsArgs = Type.Object({
|
|
|
86
87
|
});
|
|
87
88
|
|
|
88
89
|
const BODY_BUDGET_MS = 5_000;
|
|
90
|
+
const BODY_CONCURRENCY = 6;
|
|
89
91
|
const PER_BODY_CAP = 50_000;
|
|
90
92
|
|
|
91
93
|
const renderNetworkMarkdown = (
|
|
@@ -97,7 +99,7 @@ const renderNetworkMarkdown = (
|
|
|
97
99
|
return "_No matching requests captured yet._";
|
|
98
100
|
}
|
|
99
101
|
|
|
100
|
-
const failed = records.filter((r) => r.failed || (r.status !== undefined && r.status >= 500)).length;
|
|
102
|
+
const failed = records.filter((r) => r.failed === true || (r.status !== undefined && r.status >= 500)).length;
|
|
101
103
|
const totalBytes = records.reduce((s, r) => s + (r.responseBodySize ?? 0), 0);
|
|
102
104
|
const showWindow = (() => {
|
|
103
105
|
const finished = records.filter((r) => r.durationMs !== undefined);
|
|
@@ -113,8 +115,8 @@ const renderNetworkMarkdown = (
|
|
|
113
115
|
if (opts.total > records.length) headerLines.push(`(${records.length} of ${opts.total} matches shown — increase \`limit\` for more)`);
|
|
114
116
|
|
|
115
117
|
const compactRows = (records.length > 5 && !opts.expanded ? records.slice(0, 5) : records).map((r) => {
|
|
116
|
-
const status = r.failed ? "ERR" : r.status !== undefined ? String(r.status) : "—";
|
|
117
|
-
const warn = r.failed || (r.status !== undefined && r.status >= 500) ? " ⚠" : "";
|
|
118
|
+
const status = r.failed === true ? "ERR" : r.status !== undefined ? String(r.status) : "—";
|
|
119
|
+
const warn = r.failed === true || (r.status !== undefined && r.status >= 500) ? " ⚠" : "";
|
|
118
120
|
const ms = r.durationMs !== undefined ? `${r.durationMs} ms` : "—";
|
|
119
121
|
const url = r.url.length > 60 ? r.url.slice(0, 57) + "…" : r.url;
|
|
120
122
|
return `\`${r.method.padEnd(4)} ${status.padStart(3)}\` ${url} _${ms}_${warn}`;
|
|
@@ -154,65 +156,38 @@ const renderNetworkMarkdown = (
|
|
|
154
156
|
return [headerLines.join("\n"), "", tableHeader, tableRows, bodySections].filter((s) => s !== "").join("\n");
|
|
155
157
|
};
|
|
156
158
|
|
|
157
|
-
|
|
158
|
-
total:
|
|
159
|
-
returned:
|
|
160
|
-
bufferOverflowed:
|
|
161
|
-
truncated:
|
|
162
|
-
fullOutputPath
|
|
163
|
-
requests:
|
|
164
|
-
includeBodies:
|
|
165
|
-
};
|
|
159
|
+
const NetworkDetailsSchema = Type.Object({
|
|
160
|
+
total: Type.Number(),
|
|
161
|
+
returned: Type.Number(),
|
|
162
|
+
bufferOverflowed: Type.Boolean(),
|
|
163
|
+
truncated: Type.Boolean(),
|
|
164
|
+
fullOutputPath: Type.Optional(Type.String()),
|
|
165
|
+
requests: Type.Array(NetworkRecordSchema),
|
|
166
|
+
includeBodies: Type.Boolean(),
|
|
167
|
+
});
|
|
166
168
|
|
|
167
|
-
|
|
168
|
-
const o = asRecord(v);
|
|
169
|
-
if (o === undefined) return undefined;
|
|
170
|
-
const method = asString(o["method"]);
|
|
171
|
-
const url = asString(o["url"]);
|
|
172
|
-
const type = asString(o["type"]);
|
|
173
|
-
if (method === undefined || url === undefined || type === undefined) return undefined;
|
|
174
|
-
const status = asNumber(o["status"]);
|
|
175
|
-
const statusText = asString(o["statusText"]);
|
|
176
|
-
const mimeType = asString(o["mimeType"]);
|
|
177
|
-
const durationMs = asNumber(o["durationMs"]);
|
|
178
|
-
const responseBodySize = asNumber(o["responseBodySize"]);
|
|
179
|
-
const failed = asBoolean(o["failed"]);
|
|
180
|
-
const errorText = asString(o["errorText"]);
|
|
181
|
-
const bodyRaw = o["body"];
|
|
182
|
-
const body = typeof bodyRaw === "string" || bodyRaw === null ? bodyRaw : undefined;
|
|
183
|
-
return {
|
|
184
|
-
requestId: asString(o["requestId"]) ?? "",
|
|
185
|
-
method,
|
|
186
|
-
url,
|
|
187
|
-
type,
|
|
188
|
-
requestStartedMs: asNumber(o["requestStartedMs"]) ?? 0,
|
|
189
|
-
requestBodySize: asNumber(o["requestBodySize"]) ?? 0,
|
|
190
|
-
...(status !== undefined ? { status } : {}),
|
|
191
|
-
...(statusText !== undefined ? { statusText } : {}),
|
|
192
|
-
...(mimeType !== undefined ? { mimeType } : {}),
|
|
193
|
-
...(durationMs !== undefined ? { durationMs } : {}),
|
|
194
|
-
...(responseBodySize !== undefined ? { responseBodySize } : {}),
|
|
195
|
-
...(failed !== undefined ? { failed } : {}),
|
|
196
|
-
...(errorText !== undefined ? { errorText } : {}),
|
|
197
|
-
...(body !== undefined ? { body } : {}),
|
|
198
|
-
};
|
|
199
|
-
};
|
|
169
|
+
export type NetworkDetails = DetailsOf<typeof NetworkDetailsSchema>;
|
|
200
170
|
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
};
|
|
171
|
+
// Copies each drained record with its response body attached (null when Chrome no longer has it). Bounded by
|
|
172
|
+
// BODY_BUDGET_MS in total; records whose fetch did not finish in time are returned without a body.
|
|
173
|
+
const withBodies = async (
|
|
174
|
+
client: BrowserClient,
|
|
175
|
+
records: ReadonlyArray<NetworkRecord>,
|
|
176
|
+
signal: AbortSignal | undefined,
|
|
177
|
+
): Promise<ReadonlyArray<NetworkRecord>> => {
|
|
178
|
+
const deadline = deadlineSignal(BODY_BUDGET_MS, signal);
|
|
179
|
+
const bodies = await runLimited(records, { limit: BODY_CONCURRENCY, signal: deadline.signal }, async (r): Promise<string | null> => {
|
|
180
|
+
const bodyRes = await cdpCall(client, "Network.getResponseBody", { requestId: r.requestId });
|
|
181
|
+
if (!bodyRes.success) return null;
|
|
182
|
+
const b = bodyRes.data;
|
|
183
|
+
const body = b.base64Encoded ? Buffer.from(b.body, "base64").toString("utf8") : b.body;
|
|
184
|
+
return body.length > PER_BODY_CAP ? body.slice(0, PER_BODY_CAP) + "…" : body;
|
|
185
|
+
});
|
|
186
|
+
deadline.clear();
|
|
187
|
+
return records.map((r, i) => {
|
|
188
|
+
const body = bodies[i];
|
|
189
|
+
return body === undefined ? { ...r } : { ...r, body };
|
|
190
|
+
});
|
|
216
191
|
};
|
|
217
192
|
|
|
218
193
|
export const networkRequestsTool = defineBrowserTool({
|
|
@@ -227,9 +202,10 @@ export const networkRequestsTool = defineBrowserTool({
|
|
|
227
202
|
"If bufferOverflowed:true in the result, older events were dropped (capacity 500 records/tab).",
|
|
228
203
|
],
|
|
229
204
|
parameters: NetworkRequestsArgs,
|
|
205
|
+
details: NetworkDetailsSchema,
|
|
230
206
|
concurrency: "parallel",
|
|
231
207
|
|
|
232
|
-
async handler(args, { client }): Promise<Result<ToolOk
|
|
208
|
+
async handler(args, { client, signal }): Promise<Result<ToolOk<NetworkDetails>, ToolErr>> {
|
|
233
209
|
const session = client.session();
|
|
234
210
|
const drained = session.drainNetworkBuffer({
|
|
235
211
|
...(args.urlPattern !== undefined ? { urlPattern: args.urlPattern } : {}),
|
|
@@ -240,27 +216,13 @@ export const networkRequestsTool = defineBrowserTool({
|
|
|
240
216
|
limit: args.limit ?? 50,
|
|
241
217
|
});
|
|
242
218
|
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
if (ac.signal.aborted) return;
|
|
248
|
-
const bodyRes = await cdpCall(client, "Network.getResponseBody", { requestId: r.requestId });
|
|
249
|
-
if (!bodyRes.success) {
|
|
250
|
-
r.body = null;
|
|
251
|
-
return;
|
|
252
|
-
}
|
|
253
|
-
const b = bodyRes.data;
|
|
254
|
-
let body = b.base64Encoded ? Buffer.from(b.body, "base64").toString("utf8") : b.body;
|
|
255
|
-
if (body.length > PER_BODY_CAP) body = body.slice(0, PER_BODY_CAP) + "…";
|
|
256
|
-
r.body = body;
|
|
257
|
-
});
|
|
258
|
-
await Promise.allSettled(fills);
|
|
259
|
-
clearTimeout(timer);
|
|
260
|
-
}
|
|
219
|
+
const includeBodies = args.includeResponseBodies ?? false;
|
|
220
|
+
const records: ReadonlyArray<NetworkRecord> = includeBodies
|
|
221
|
+
? await withBodies(client, drained.records, signal)
|
|
222
|
+
: drained.records;
|
|
261
223
|
|
|
262
|
-
const text = renderNetworkMarkdown(
|
|
263
|
-
includeBodies
|
|
224
|
+
const text = renderNetworkMarkdown(records, {
|
|
225
|
+
includeBodies,
|
|
264
226
|
total: drained.total,
|
|
265
227
|
bufferOverflowed: drained.bufferOverflowed,
|
|
266
228
|
expanded: true,
|
|
@@ -269,21 +231,19 @@ export const networkRequestsTool = defineBrowserTool({
|
|
|
269
231
|
|
|
270
232
|
const details: NetworkDetails = {
|
|
271
233
|
total: drained.total,
|
|
272
|
-
returned:
|
|
234
|
+
returned: records.length,
|
|
273
235
|
bufferOverflowed: drained.bufferOverflowed,
|
|
274
236
|
truncated: trunc.wasTruncated,
|
|
275
|
-
requests:
|
|
276
|
-
includeBodies
|
|
237
|
+
requests: records,
|
|
238
|
+
includeBodies,
|
|
239
|
+
...(trunc.fullOutputPath !== undefined ? { fullOutputPath: trunc.fullOutputPath } : {}),
|
|
277
240
|
};
|
|
278
|
-
if (trunc.fullOutputPath !== undefined) details.fullOutputPath = trunc.fullOutputPath;
|
|
279
241
|
|
|
280
242
|
return ok({ text: trunc.text, details });
|
|
281
243
|
},
|
|
282
244
|
|
|
283
|
-
renderResult(result, expanded
|
|
284
|
-
const details =
|
|
285
|
-
if (!details) return new Text(theme.fg("error", "network: no details"), 0, 0);
|
|
286
|
-
|
|
245
|
+
renderResult(result, expanded) {
|
|
246
|
+
const details = result.details;
|
|
287
247
|
const md = renderNetworkMarkdown(details.requests, {
|
|
288
248
|
includeBodies: details.includeBodies && expanded,
|
|
289
249
|
total: details.total,
|
|
@@ -291,7 +251,7 @@ export const networkRequestsTool = defineBrowserTool({
|
|
|
291
251
|
expanded,
|
|
292
252
|
});
|
|
293
253
|
const tail = expanded
|
|
294
|
-
? `\n\n${keyHint("app.tools.expand", "to collapse")}${details.fullOutputPath ? ` · full payload at \`${details.fullOutputPath}\`` : ""}`
|
|
254
|
+
? `\n\n${keyHint("app.tools.expand", "to collapse")}${details.fullOutputPath !== undefined ? ` · full payload at \`${details.fullOutputPath}\`` : ""}`
|
|
295
255
|
: `\n\n${keyHint("app.tools.expand", "to expand")}`;
|
|
296
256
|
|
|
297
257
|
return new Markdown(md + tail, 0, 0, getMarkdownTheme());
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
// Page-side code: the exported function's SOURCE TEXT is injected into Chrome (see ./inject.ts).
|
|
2
|
+
// Rules for every `*.page.ts` module (enforced by test/domains/page-functions-test.ts):
|
|
3
|
+
// - export plain `function` declarations only (no arrows — tsx strips a single parameter's parentheses)
|
|
4
|
+
// - a function must be self-contained: no references to module scope, imports or other exports
|
|
5
|
+
// - no NAMED inner functions (`function f() {}`, `const f = () => {}`): tsx/esbuild wraps those in a module-level
|
|
6
|
+
// `__name` helper that does not travel with the source; anonymous callbacks such as `.find((o) => …)` are fine
|
|
7
|
+
// - inputs arrive as arguments, never by string interpolation
|
|
8
|
+
// - only `import type` is allowed, so the module has no runtime dependencies
|
|
9
|
+
|
|
10
|
+
export type PageOption = { readonly value: string; readonly text: string };
|
|
11
|
+
|
|
12
|
+
export type FillOptions = {
|
|
13
|
+
// browser_fill refuses a <select> (its documented error points at browser_select_option); browser_fill_form fills it.
|
|
14
|
+
readonly rejectSelect: boolean;
|
|
15
|
+
readonly focusFirst: boolean;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
// What the fill engine (fillField) and the checkbox function (form.page.ts setChecked) return from the page.
|
|
19
|
+
export type PageResult =
|
|
20
|
+
| { readonly ok: true; readonly kind: "select"; readonly value: string; readonly text: string; readonly tag: string }
|
|
21
|
+
| { readonly ok: true; readonly kind: "checkbox" | "radio"; readonly checked: boolean; readonly changed: boolean; readonly tag: string }
|
|
22
|
+
| { readonly ok: true; readonly kind: "input" | "textarea" | "contenteditable"; readonly value: string; readonly tag: string }
|
|
23
|
+
| {
|
|
24
|
+
readonly ok: false;
|
|
25
|
+
readonly reason: string;
|
|
26
|
+
readonly tag?: string;
|
|
27
|
+
// "select_rejected": the engine was told not to touch a <select>; "no_match": a <select> had no such option.
|
|
28
|
+
readonly code?: "select_rejected" | "no_match";
|
|
29
|
+
readonly options?: ReadonlyArray<PageOption>;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
// The element a form function receives, typed the way the functions treat it: an HTML element that MAY
|
|
33
|
+
// carry the form-control properties, discovered at runtime by tag rather than by instanceof, so the functions behave
|
|
34
|
+
// identically on every element the page hands over.
|
|
35
|
+
export type FormControl = HTMLElement & {
|
|
36
|
+
type?: string;
|
|
37
|
+
value?: string;
|
|
38
|
+
checked?: boolean;
|
|
39
|
+
disabled?: boolean;
|
|
40
|
+
options?: HTMLOptionsCollection;
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
// Writes `value` into `el` the way a user would: through the native value setter (so React/Vue controlled inputs
|
|
44
|
+
// notice), via click for checkboxes, execCommand for rich-text editors. Element functions take the element as their
|
|
45
|
+
// first parameter; inject.ts's elementFunctionSource forwards Runtime.callFunctionOn's `this` into it.
|
|
46
|
+
export function fillField(el: FormControl | null, value: unknown, opts: FillOptions): PageResult {
|
|
47
|
+
if (el?.nodeType !== 1) return { ok: false, reason: "ref does not point to an element" };
|
|
48
|
+
const tagName = el.tagName;
|
|
49
|
+
const tag = tagName.toLowerCase();
|
|
50
|
+
const type = (el.type ?? "").toLowerCase();
|
|
51
|
+
|
|
52
|
+
if (opts.rejectSelect && tag === "select") {
|
|
53
|
+
return { ok: false, reason: "element is a <select>", code: "select_rejected", tag: tagName };
|
|
54
|
+
}
|
|
55
|
+
if (el.disabled) return { ok: false, reason: "element is disabled", tag: tagName };
|
|
56
|
+
|
|
57
|
+
if (opts.focusFirst) {
|
|
58
|
+
try {
|
|
59
|
+
el.focus();
|
|
60
|
+
} catch {
|
|
61
|
+
/* focus throws on some detached or shadow nodes; a missed focus is not a failed fill */
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
if (tag === "select" && el.options !== undefined) {
|
|
66
|
+
const want = String(value);
|
|
67
|
+
let matched: HTMLOptionElement | null = null;
|
|
68
|
+
for (const o of el.options) {
|
|
69
|
+
if (o.value === want || o.label === want || o.text === want) {
|
|
70
|
+
matched = o;
|
|
71
|
+
break;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
if (!matched) {
|
|
75
|
+
const options: PageOption[] = [];
|
|
76
|
+
for (const o of el.options) options.push({ value: o.value, text: o.text });
|
|
77
|
+
return { ok: false, reason: "no matching option", code: "no_match", options, tag: tagName };
|
|
78
|
+
}
|
|
79
|
+
el.value = matched.value;
|
|
80
|
+
for (const t of ["input", "change"]) el.dispatchEvent(new Event(t, { bubbles: true }));
|
|
81
|
+
return { ok: true, kind: "select", value: el.value ?? "", text: matched.text, tag: tagName };
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
if (tag === "input" && (type === "checkbox" || type === "radio")) {
|
|
85
|
+
const want = value === true || value === "true" || value === "on" || value === 1;
|
|
86
|
+
let changed = false;
|
|
87
|
+
if (el.checked !== want) {
|
|
88
|
+
try {
|
|
89
|
+
el.click();
|
|
90
|
+
} catch {
|
|
91
|
+
/* a click handler may throw; the direct write below still applies the state */
|
|
92
|
+
}
|
|
93
|
+
if (el.checked !== want) {
|
|
94
|
+
el.checked = want;
|
|
95
|
+
for (const t of ["input", "change"]) el.dispatchEvent(new Event(t, { bubbles: true }));
|
|
96
|
+
}
|
|
97
|
+
changed = true;
|
|
98
|
+
}
|
|
99
|
+
return { ok: true, kind: type, checked: el.checked, changed, tag: tagName };
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
if (el.isContentEditable) {
|
|
103
|
+
try {
|
|
104
|
+
el.focus();
|
|
105
|
+
} catch {
|
|
106
|
+
/* see above */
|
|
107
|
+
}
|
|
108
|
+
let done = false;
|
|
109
|
+
try {
|
|
110
|
+
const sel = window.getSelection();
|
|
111
|
+
if (sel) {
|
|
112
|
+
sel.removeAllRanges();
|
|
113
|
+
const rng = document.createRange();
|
|
114
|
+
rng.selectNodeContents(el);
|
|
115
|
+
sel.addRange(rng);
|
|
116
|
+
}
|
|
117
|
+
// eslint-disable-next-line @typescript-eslint/no-deprecated -- insertText is still the one way to drive ProseMirror/Quill-style editors through their own input path
|
|
118
|
+
done = document.execCommand("insertText", false, String(value));
|
|
119
|
+
} catch {
|
|
120
|
+
/* execCommand unavailable or refused; fall through to the textContent write */
|
|
121
|
+
}
|
|
122
|
+
if (!done) {
|
|
123
|
+
el.textContent = String(value);
|
|
124
|
+
el.dispatchEvent(new InputEvent("input", { bubbles: true }));
|
|
125
|
+
}
|
|
126
|
+
return { ok: true, kind: "contenteditable", value: el.textContent, tag: tagName };
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
if (tag === "input" || tag === "textarea") {
|
|
130
|
+
const proto = tag === "textarea" ? window.HTMLTextAreaElement.prototype : window.HTMLInputElement.prototype;
|
|
131
|
+
const desc = Object.getOwnPropertyDescriptor(proto, "value");
|
|
132
|
+
if (desc?.set) desc.set.call(el, String(value));
|
|
133
|
+
else el.value = String(value);
|
|
134
|
+
for (const t of ["input", "change"]) el.dispatchEvent(new Event(t, { bubbles: true }));
|
|
135
|
+
return { ok: true, kind: tag, value: el.value ?? "", tag: tagName };
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
return { ok: false, reason: "element is not a fillable field (tag=" + tag + ")", tag: tagName };
|
|
139
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
// Page-side code for browser_focus, browser_select_option and browser_set_checked. See fill.page.ts for the rules.
|
|
2
|
+
import type { FormControl, PageResult } from "./fill.page";
|
|
3
|
+
|
|
4
|
+
export type FocusResult = { readonly status: "ok"; readonly tag: string } | { readonly status: "not_focusable"; readonly tag: string };
|
|
5
|
+
|
|
6
|
+
export function focusElement(el: Element & { focus?: () => void }): FocusResult {
|
|
7
|
+
if (typeof el.focus !== "function") return { status: "not_focusable", tag: el.tagName };
|
|
8
|
+
el.focus();
|
|
9
|
+
return document.activeElement === el ? { status: "ok", tag: el.tagName } : { status: "not_focusable", tag: el.tagName };
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export type SelectOption = { readonly value: string; readonly label: string };
|
|
13
|
+
|
|
14
|
+
export type SelectResult =
|
|
15
|
+
| { readonly status: "ok"; readonly value: string; readonly label: string }
|
|
16
|
+
| { readonly status: "not_select"; readonly tag: string }
|
|
17
|
+
| { readonly status: "no_match"; readonly options: ReadonlyArray<SelectOption> };
|
|
18
|
+
|
|
19
|
+
// null sentinels distinguish "not provided" from a real empty-string match.
|
|
20
|
+
export type SelectWanted = {
|
|
21
|
+
readonly wv: string | null;
|
|
22
|
+
readonly wl: string | null;
|
|
23
|
+
readonly wi: number | null;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export function selectOption(el: FormControl, arg: SelectWanted): SelectResult {
|
|
27
|
+
if (el.tagName !== "SELECT" || el.options === undefined) return { status: "not_select", tag: el.tagName };
|
|
28
|
+
const opts = Array.from(el.options);
|
|
29
|
+
const wv = arg.wv, wl = arg.wl, wi = arg.wi;
|
|
30
|
+
let opt: HTMLOptionElement | null = null;
|
|
31
|
+
if (wi !== null) opt = opts[wi] ?? null;
|
|
32
|
+
else if (wv !== null) opt = opts.find((o) => o.value === wv) ?? null;
|
|
33
|
+
else if (wl !== null) opt = opts.find((o) => o.text === wl) ?? null;
|
|
34
|
+
if (!opt) return { status: "no_match", options: opts.map((o) => ({ value: o.value, label: o.text })) };
|
|
35
|
+
el.value = opt.value;
|
|
36
|
+
el.dispatchEvent(new Event("input", { bubbles: true }));
|
|
37
|
+
el.dispatchEvent(new Event("change", { bubbles: true }));
|
|
38
|
+
return { status: "ok", value: opt.value, label: opt.text };
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export function setChecked(el: FormControl, checked: boolean): PageResult {
|
|
42
|
+
const tag = el.tagName.toLowerCase();
|
|
43
|
+
const type = (el.type ?? "").toLowerCase();
|
|
44
|
+
if (tag !== "input" || (type !== "checkbox" && type !== "radio")) return { ok: false, reason: "ref is not a checkbox or radio" };
|
|
45
|
+
if (el.disabled) return { ok: false, reason: "element is disabled" };
|
|
46
|
+
const want = checked;
|
|
47
|
+
let changed = false;
|
|
48
|
+
if (el.checked !== want) {
|
|
49
|
+
try {
|
|
50
|
+
el.click();
|
|
51
|
+
} catch {
|
|
52
|
+
/* a click handler may throw; the direct write below still applies the state */
|
|
53
|
+
}
|
|
54
|
+
if (el.checked !== want) {
|
|
55
|
+
el.checked = want;
|
|
56
|
+
el.dispatchEvent(new Event("change", { bubbles: true }));
|
|
57
|
+
}
|
|
58
|
+
changed = true;
|
|
59
|
+
}
|
|
60
|
+
return { ok: true, kind: type, checked: el.checked, changed, tag: el.tagName };
|
|
61
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
// The node-side half of the page-function scheme.
|
|
2
|
+
//
|
|
3
|
+
// Page code lives in `*.page.ts` modules next to this file, type-checked by tsconfig.page.json against the DOM lib
|
|
4
|
+
// only. At runtime those modules are plain TypeScript functions that pi's loader (jiti) or tsx strips the types from,
|
|
5
|
+
// so `fn.toString()` is the exact JavaScript to ship into Chrome. Two consequences shape this file:
|
|
6
|
+
// - a page function must be a `function` declaration, not an arrow: tsx emits `x=>...` for single-parameter arrows,
|
|
7
|
+
// which is not a valid Runtime.callFunctionOn declaration, and jiti and tsx must agree on the leading token
|
|
8
|
+
// - a page function must be self-contained (no module-scope references), because only its own text travels;
|
|
9
|
+
// test/domains/page-functions-test.ts checks that for every exported page function
|
|
10
|
+
import { safeJs } from "../../util/js-template";
|
|
11
|
+
|
|
12
|
+
// Any page function. `this`-typed functions (the element handed over by Runtime.callFunctionOn) satisfy this too.
|
|
13
|
+
export type PageFunction = (...args: never[]) => unknown;
|
|
14
|
+
|
|
15
|
+
const DECLARATION = /^function\b/;
|
|
16
|
+
|
|
17
|
+
// The source text of a page function, as the `functionDeclaration` of Runtime.callFunctionOn. Throws at import time
|
|
18
|
+
// (callers evaluate it into module constants) when the loader has produced something other than a function declaration.
|
|
19
|
+
export const pageFunctionSource = (fn: PageFunction): string => {
|
|
20
|
+
const source = fn.toString();
|
|
21
|
+
if (!DECLARATION.test(source)) {
|
|
22
|
+
throw new Error(
|
|
23
|
+
`page function ${fn.name || "(anonymous)"} must be a \`function\` declaration; the loader produced: ${source.slice(0, 60)}`,
|
|
24
|
+
);
|
|
25
|
+
}
|
|
26
|
+
return source;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
// The `functionDeclaration` for Runtime.callFunctionOn of an ELEMENT function: one whose first parameter is the element.
|
|
30
|
+
// CDP hands the element over as `this`, so this wrapper forwards it as the first argument, keeping the page functions
|
|
31
|
+
// free of `this` (plain to type, plain to call in tests).
|
|
32
|
+
export const elementFunctionSource = (fn: PageFunction): string =>
|
|
33
|
+
"function (...args) { return (" + pageFunctionSource(fn) + ")(this, ...args); }";
|
|
34
|
+
|
|
35
|
+
// An expression for Runtime.evaluate that calls the page function with `args`, each JSON-encoded through safeJs.
|
|
36
|
+
export const pageCallExpression = (fn: PageFunction, ...args: ReadonlyArray<unknown>): string =>
|
|
37
|
+
"(" + pageFunctionSource(fn) + ")(" + args.map((a) => safeJs`${a}`).join(", ") + ")";
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
// Page-side code for browser_dispatch_key. See fill.page.ts for the rules.
|
|
2
|
+
|
|
3
|
+
export type KeyEventSpec = {
|
|
4
|
+
readonly type: string;
|
|
5
|
+
readonly key: string;
|
|
6
|
+
// Legacy handlers branch on e.keyCode/e.which rather than e.key, so both are populated from this.
|
|
7
|
+
readonly code: number;
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
// Dispatches a synthetic KeyboardEvent on `el`; returns the number of elements hit (always 1 here).
|
|
11
|
+
export function dispatchKeyEvent(el: Element, spec: KeyEventSpec): number {
|
|
12
|
+
el.dispatchEvent(
|
|
13
|
+
new KeyboardEvent(spec.type, { key: spec.key, keyCode: spec.code, which: spec.code, bubbles: true, cancelable: true }),
|
|
14
|
+
);
|
|
15
|
+
return 1;
|
|
16
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
// Page-side code for the snapshot's live value read. See fill.page.ts for the rules.
|
|
2
|
+
|
|
3
|
+
// The AX `value` is stale for freshly-typed or controlled inputs, so the live DOM property is read instead.
|
|
4
|
+
export function liveValue(el: Element): string | null {
|
|
5
|
+
if ("type" in el && (el.type === "checkbox" || el.type === "radio")) {
|
|
6
|
+
return "checked" in el && el.checked ? "checked" : "unchecked";
|
|
7
|
+
}
|
|
8
|
+
if ("value" in el && typeof el.value === "string") return el.value;
|
|
9
|
+
return null;
|
|
10
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
// Page-side code for browser_read_page. See fill.page.ts for the rules.
|
|
2
|
+
|
|
3
|
+
export type ContentBlockKind = "paragraph" | "heading" | "listitem" | "blockquote" | "other";
|
|
4
|
+
|
|
5
|
+
export type ContentBlock = {
|
|
6
|
+
readonly kind: ContentBlockKind;
|
|
7
|
+
readonly text: string;
|
|
8
|
+
readonly linkTextLength: number;
|
|
9
|
+
readonly inBoilerplate: boolean;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
export type PageCapture = {
|
|
13
|
+
readonly url: string;
|
|
14
|
+
readonly title: string;
|
|
15
|
+
readonly blocks: ReadonlyArray<ContentBlock>;
|
|
16
|
+
readonly bodyText: string;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
// Returns the capture as JSON text (not an object) so the isolated-tab evaluator can keep its string guard.
|
|
20
|
+
export function capturePage(bodyTextLimit: number): string {
|
|
21
|
+
const BOILERPLATE = new Set(["NAV", "HEADER", "FOOTER", "ASIDE"]);
|
|
22
|
+
const selector = "p, h1, h2, h3, h4, h5, h6, li, blockquote";
|
|
23
|
+
const root = document.querySelector("article, main, [role=main]") ?? document.body;
|
|
24
|
+
const blocks: ContentBlock[] = [];
|
|
25
|
+
for (const el of root.querySelectorAll<HTMLElement>(selector)) {
|
|
26
|
+
const text = el.innerText.replace(/\s+/g, " ").trim();
|
|
27
|
+
if (!text) continue;
|
|
28
|
+
let linkTextLength = 0;
|
|
29
|
+
for (const a of el.querySelectorAll("a")) linkTextLength += a.innerText.length;
|
|
30
|
+
const tag = el.tagName;
|
|
31
|
+
const kind: ContentBlockKind = /^H[1-6]$/.test(tag)
|
|
32
|
+
? "heading"
|
|
33
|
+
: tag === "LI"
|
|
34
|
+
? "listitem"
|
|
35
|
+
: tag === "BLOCKQUOTE"
|
|
36
|
+
? "blockquote"
|
|
37
|
+
: tag === "P"
|
|
38
|
+
? "paragraph"
|
|
39
|
+
: "other";
|
|
40
|
+
// Inside nav/header/footer/aside (by tag or landmark role) at any depth.
|
|
41
|
+
let inBoilerplate = false;
|
|
42
|
+
for (let n: Element | null = el; n && !inBoilerplate; n = n.parentElement) {
|
|
43
|
+
const role = n.getAttribute("role");
|
|
44
|
+
inBoilerplate = BOILERPLATE.has(n.tagName) || role === "navigation" || role === "banner" || role === "contentinfo";
|
|
45
|
+
}
|
|
46
|
+
blocks.push({ kind, text, linkTextLength, inBoilerplate });
|
|
47
|
+
}
|
|
48
|
+
const bodyText = document.body.innerText.replace(/\n{3,}/g, "\n\n").slice(0, bodyTextLimit);
|
|
49
|
+
const capture: PageCapture = { url: location.href, title: document.title, blocks, bodyText };
|
|
50
|
+
return JSON.stringify(capture);
|
|
51
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
// Page-side code for browser_web_search: pulls the organic result anchors off a Google results page. See fill.page.ts for the rules.
|
|
2
|
+
|
|
3
|
+
export type SerpAnchor = {
|
|
4
|
+
readonly href: string;
|
|
5
|
+
readonly heading: string;
|
|
6
|
+
readonly snippet: string;
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
export type SerpExtraction = {
|
|
10
|
+
readonly anchors: ReadonlyArray<SerpAnchor>;
|
|
11
|
+
readonly pageText: string;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
// Returns the extraction as JSON text (not an object) so the isolated-tab evaluator can keep its string guard.
|
|
15
|
+
export function extractSerp(pageTextLimit: number): string {
|
|
16
|
+
const main = document.querySelector<HTMLElement>("#search, #rso, #main") ?? document.body;
|
|
17
|
+
const headings = Array.from(main.querySelectorAll<HTMLElement>("a h3"));
|
|
18
|
+
const seen = new Set<string>();
|
|
19
|
+
const anchors: SerpAnchor[] = [];
|
|
20
|
+
for (const h3 of headings) {
|
|
21
|
+
const a = h3.closest<HTMLAnchorElement>("a[href]");
|
|
22
|
+
if (!a) continue;
|
|
23
|
+
const href = a.href;
|
|
24
|
+
if (!href || seen.has(href)) continue;
|
|
25
|
+
seen.add(href);
|
|
26
|
+
const container: HTMLElement | null =
|
|
27
|
+
a.closest<HTMLElement>("div.g") ??
|
|
28
|
+
a.closest<HTMLElement>("div[data-hveid]") ??
|
|
29
|
+
a.parentElement?.parentElement ??
|
|
30
|
+
a.parentElement;
|
|
31
|
+
const containerText = container ? container.innerText : "";
|
|
32
|
+
const heading = h3.innerText || h3.textContent;
|
|
33
|
+
const snippet = containerText.replace(heading, "").replace(/\s+/g, " ").trim().slice(0, 500);
|
|
34
|
+
anchors.push({ href, heading, snippet });
|
|
35
|
+
}
|
|
36
|
+
const pageText = document.body.innerText.slice(0, pageTextLimit);
|
|
37
|
+
const extraction: SerpExtraction = { anchors, pageText };
|
|
38
|
+
return JSON.stringify(extraction);
|
|
39
|
+
}
|