/** * Firefox accessibility-style snapshot, built by walking the live DOM in-page. * * Chrome gets take_snapshot for free from Accessibility.getFullAXTree. BiDi * has no equivalent domain at all, so this module assembles the same shape * of output (see src/tools/snapshot.ts, the format spec) by generating a * self-contained JavaScript function SOURCE and handing it to whatever * `evaluate` the caller supplies. This file never opens a socket and never * imports a transport: takeStampedSnapshot is testable with a fake evaluate, * which is the whole point of the split (driver.ts owns BiDi wiring). * * Per ADR-001 (src/driver.ts), Firefox uids are PAGE STATE, not protocol * state: every emitted node gets a stamp written onto it as the STAMP_ATTR * attribute, and that stamp (not a script/browsingContext handle) is what * survives a dropped socket, a fresh session, and a fresh CLI process. */ /** Attribute stamped onto every emitted element, mirroring driver.ts UID_STAMP_ATTR. */ export const STAMP_ATTR = "data-cdp-uid"; export interface StampedSnapshot { snapshot: string; nodeCount: number; } /** * Take a snapshot of the current page by evaluating a generated JS function * in it. `evaluate` is handed the function SOURCE, not a closure: the source * is stringified and must not reference anything outside itself. It must * resolve to whatever that function returns. */ export async function takeStampedSnapshot( evaluate: (functionDeclaration: string) => Promise, ): Promise { const raw = await evaluate(buildSnapshotFunctionSource()); return coerceSnapshotResult(raw); } /** Validate + narrow the untyped evaluate() return into a StampedSnapshot. */ export function coerceSnapshotResult(raw: unknown): StampedSnapshot { if ( typeof raw !== "object" || raw === null || typeof (raw as { snapshot?: unknown }).snapshot !== "string" || typeof (raw as { nodeCount?: unknown }).nodeCount !== "number" ) { throw new Error("takeStampedSnapshot: evaluate() returned an unexpected shape"); } return { snapshot: (raw as StampedSnapshot).snapshot, nodeCount: (raw as StampedSnapshot).nodeCount }; } /** * The reduced role + accessible-name heuristics, as in-page JS source. Shared by the snapshot * walker below and by `describeElementSource` (src/shared-tools.ts), which names the element an * action is about to touch, so a click result and a snapshot line agree on what "button \"Save\"" * means. Expects a `MAX_NAME_LENGTH` var in the enclosing function; interpolates nothing itself. */ export const ACCNAME_HELPERS_SOURCE = ` function truncate(s) { s = s.trim().replace(/\\s+/g, " "); if (s.length > MAX_NAME_LENGTH) return s.slice(0, MAX_NAME_LENGTH - 1) + "\\u2026"; return s; } function textOf(el) { return el.textContent || ""; } function idRefsText(doc, ids) { var parts = []; var list = ids.trim().split(/\\s+/); for (var i = 0; i < list.length; i++) { var ref = doc.getElementById(list[i]); if (ref) parts.push(textOf(ref)); } return parts.join(" ").trim(); } // Text of a