src/domains/ax-tree.ts (view raw)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
import type { AxNodeResult } from "../cdp/commands";
import type { BrowserClient } from "../client";
import type { Box } from "./box";
import { cdpCall } from "./cdp-call";
export type RawAxNode = AxNodeResult;
type AxValue = NonNullable<RawAxNode["role"]>;
export type SlimNode = {
role: string;
name?: string;
value?: string;
description?: string;
state?: string;
children: SlimNode[];
_backendId?: number;
ref?: string;
box?: Box;
};
export const INTERACTIVE_ROLES = new Set([
"button",
"link",
"textbox",
"checkbox",
"radio",
"combobox",
"menuitem",
"tab",
"switch",
"slider",
"searchbox",
"spinbutton",
]);
// The AX `value` is stale for freshly-typed or controlled inputs, so for these roles the live DOM property is read instead.
export const VALUE_ROLES = new Set([
"textbox",
"searchbox",
"spinbutton",
"combobox",
"checkbox",
"radio",
"switch",
"slider",
]);
const stringOf = (v: AxValue | undefined): string | undefined => {
const x = v?.value;
if (typeof x === "string" && x !== "") return x;
if (typeof x === "number") return String(x);
return undefined;
};
const collectState = (props: RawAxNode["properties"]): string | undefined => {
if (!props || props.length === 0) return undefined;
const flags: string[] = [];
for (const p of props) {
const name = p.name;
if (!name) continue;
const raw = p.value?.value;
if (raw === true && (name === "focused" || name === "required" || name === "disabled" || name === "checked" || name === "expanded" || name === "selected" || name === "pressed" || name === "modal")) {
flags.push(name);
}
if (typeof raw === "number" && name === "level") flags.push(`level ${raw}`);
}
return flags.length > 0 ? flags.join(", ") : undefined;
};
export const buildTree = (
rawNodes: ReadonlyArray<RawAxNode>,
opts: { interestingOnly: boolean; maxNodes: number },
): SlimNode[] => {
const byId = new Map<string, RawAxNode>();
for (const n of rawNodes) byId.set(n.nodeId, n);
const rootIds: string[] = [];
for (const n of rawNodes) {
if (!n.parentId || !byId.has(n.parentId)) rootIds.push(n.nodeId);
}
let budget = opts.maxNodes;
const slim = (node: RawAxNode): SlimNode | undefined => {
if (budget <= 0) return undefined;
if (opts.interestingOnly && node.ignored) {
// Chrome often ignores wrapper divs while their descendants remain meaningful, so recurse through an ignored node's children.
const out: SlimNode[] = [];
for (const cid of node.childIds ?? []) {
const child = byId.get(cid);
if (!child) continue;
const slimChild = slim(child);
if (slimChild) out.push(slimChild);
}
return out.length > 0 ? { role: "_hoist", children: out } : undefined;
}
budget--;
const role = stringOf(node.role) ?? "unknown";
const name = stringOf(node.name);
const value = stringOf(node.value);
const description = stringOf(node.description);
const state = collectState(node.properties);
const children: SlimNode[] = [];
for (const cid of node.childIds ?? []) {
const child = byId.get(cid);
if (!child) continue;
const slimChild = slim(child);
if (!slimChild) continue;
if (slimChild.role === "_hoist") children.push(...slimChild.children);
else children.push(slimChild);
}
const out: SlimNode = { role, children };
if (name !== undefined) out.name = name;
if (value !== undefined) out.value = value;
if (description !== undefined) out.description = description;
if (state !== undefined) out.state = state;
if (node.backendDOMNodeId !== undefined) out._backendId = node.backendDOMNodeId;
return out;
};
const result: SlimNode[] = [];
for (const rid of rootIds) {
const root = byId.get(rid);
if (!root) continue;
const s = slim(root);
if (!s) continue;
if (s.role === "_hoist") result.push(...s.children);
else result.push(s);
}
return result;
};
export const countNodes = (nodes: ReadonlyArray<SlimNode>): number => {
let n = 0;
for (const node of nodes) n += 1 + countNodes(node.children);
return n;
};
export const stripInternals = (nodes: ReadonlyArray<SlimNode>): SlimNode[] =>
nodes.map((n) => {
const { _backendId: _bid, ...rest } = n;
return { ...rest, children: stripInternals(n.children) };
});
export const collectInteractiveTargets = (
nodes: ReadonlyArray<SlimNode>,
): Array<{ node: SlimNode; backendId: number }> => {
const out: Array<{ node: SlimNode; backendId: number }> = [];
const walk = (ns: ReadonlyArray<SlimNode>): void => {
for (const n of ns) {
if (n._backendId !== undefined && INTERACTIVE_ROLES.has(n.role)) {
out.push({ node: n, backendId: n._backendId });
}
walk(n.children);
}
};
walk(nodes);
return out;
};
export const liveValue = async (client: BrowserClient, backendId: number): Promise<string | undefined> => {
const resolved = await cdpCall(client, "DOM.resolveNode", { backendNodeId: backendId });
if (!resolved.success) return undefined;
const objectId = resolved.data.object.objectId;
if (!objectId) return undefined;
const fn = `function () {
if (this.type === "checkbox" || this.type === "radio") return this.checked ? "checked" : "unchecked";
if (typeof this.value === "string") return this.value;
return null;
}`;
const r = await cdpCall(client, "Runtime.callFunctionOn", {
objectId,
functionDeclaration: fn,
returnByValue: true,
});
if (!r.success) return undefined;
const value = r.data.result.value;
return typeof value === "string" ? value : undefined;
};
|