@howaboua/pi-browser 0.0.10 → 0.0.12
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 +11 -4
- package/README.md +11 -1
- package/biome.json +1 -1
- package/dist/extension.d.ts +3 -0
- package/dist/extension.js +29 -0
- package/dist/index.d.ts +1 -2
- package/dist/index.js +1 -100
- package/dist/remote-worker.js +137 -116
- package/dist/src/browser/config.d.ts +2 -0
- package/dist/src/browser/config.js +38 -5
- package/dist/src/browser/help.js +7 -7
- package/dist/src/browser/voice-start.d.ts +16 -0
- package/dist/src/browser/voice-start.js +110 -0
- package/dist/src/browser-registration.d.ts +6 -0
- package/dist/src/browser-registration.js +103 -0
- package/dist/src/browser-tool-definition.d.ts +12 -0
- package/dist/src/browser-tool-definition.js +63 -0
- package/dist/src/browser-tool.d.ts +1 -4
- package/dist/src/browser-tool.js +2 -62
- package/dist/src/cdp/session.d.ts +3 -8
- package/dist/src/cdp/session.js +1 -104
- package/dist/src/cdp/snapshot-tree.d.ts +8 -0
- package/dist/src/cdp/snapshot-tree.js +238 -0
- package/dist/src/cdp/snapshot.js +2 -234
- package/dist/src/cdp/tab-bridge.d.ts +25 -0
- package/dist/src/cdp/tab-bridge.js +107 -0
- package/index.ts +1 -127
- package/package.json +12 -8
- package/src/browser/config.ts +48 -7
- package/src/browser/help.ts +7 -7
- package/src/browser/voice-start.ts +154 -0
- package/src/browser-registration.ts +138 -0
- package/src/browser-tool-definition.ts +81 -0
- package/src/browser-tool.ts +3 -69
- package/src/cdp/session.ts +3 -145
- package/src/cdp/snapshot-tree.ts +291 -0
- package/src/cdp/snapshot.ts +5 -282
- package/src/cdp/tab-bridge.ts +149 -0
- package/tsconfig.json +1 -1
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
import { asRecord } from "./types.js";
|
|
2
|
+
const INTERACTIVE_ROLES = new Set([
|
|
3
|
+
"button",
|
|
4
|
+
"checkbox",
|
|
5
|
+
"combobox",
|
|
6
|
+
"link",
|
|
7
|
+
"listbox",
|
|
8
|
+
"menuitem",
|
|
9
|
+
"option",
|
|
10
|
+
"menuitemcheckbox",
|
|
11
|
+
"menuitemradio",
|
|
12
|
+
"radio",
|
|
13
|
+
"searchbox",
|
|
14
|
+
"slider",
|
|
15
|
+
"spinbutton",
|
|
16
|
+
"switch",
|
|
17
|
+
"tab",
|
|
18
|
+
"textbox",
|
|
19
|
+
"treeitem",
|
|
20
|
+
]);
|
|
21
|
+
const NEXT_ELEMENT_IDS = new WeakMap();
|
|
22
|
+
function axProperty(value) {
|
|
23
|
+
const record = asRecord(value, "accessibility property");
|
|
24
|
+
if (typeof record["name"] !== "string") {
|
|
25
|
+
throw new Error("Accessibility property has no name");
|
|
26
|
+
}
|
|
27
|
+
return {
|
|
28
|
+
name: record["name"],
|
|
29
|
+
...(record["value"] ? { value: asRecord(record["value"]) } : {}),
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
function axNode(value) {
|
|
33
|
+
const record = asRecord(value, "accessibility node");
|
|
34
|
+
if (typeof record["nodeId"] !== "string") {
|
|
35
|
+
throw new Error("Accessibility node has no nodeId");
|
|
36
|
+
}
|
|
37
|
+
return {
|
|
38
|
+
nodeId: record["nodeId"],
|
|
39
|
+
...(typeof record["parentId"] === "string"
|
|
40
|
+
? { parentId: record["parentId"] }
|
|
41
|
+
: {}),
|
|
42
|
+
...(Array.isArray(record["childIds"])
|
|
43
|
+
? {
|
|
44
|
+
childIds: record["childIds"].filter((id) => typeof id === "string"),
|
|
45
|
+
}
|
|
46
|
+
: {}),
|
|
47
|
+
...(typeof record["backendDOMNodeId"] === "number"
|
|
48
|
+
? { backendDOMNodeId: record["backendDOMNodeId"] }
|
|
49
|
+
: {}),
|
|
50
|
+
...(typeof record["ignored"] === "boolean"
|
|
51
|
+
? { ignored: record["ignored"] }
|
|
52
|
+
: {}),
|
|
53
|
+
...(record["role"] ? { role: asRecord(record["role"]) } : {}),
|
|
54
|
+
...(record["name"] ? { name: asRecord(record["name"]) } : {}),
|
|
55
|
+
...(record["value"] ? { value: asRecord(record["value"]) } : {}),
|
|
56
|
+
...(Array.isArray(record["properties"])
|
|
57
|
+
? { properties: record["properties"].map(axProperty) }
|
|
58
|
+
: {}),
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
function shouldShow(node) {
|
|
62
|
+
const role = String(node.role?.value ?? "");
|
|
63
|
+
const name = node.name?.value ?? "";
|
|
64
|
+
const value = node.value?.value;
|
|
65
|
+
if (role === "InlineTextBox")
|
|
66
|
+
return false;
|
|
67
|
+
if (INTERACTIVE_ROLES.has(role))
|
|
68
|
+
return true;
|
|
69
|
+
return (role !== "none" &&
|
|
70
|
+
role !== "generic" &&
|
|
71
|
+
!(name === "" && (value === "" || value == null)));
|
|
72
|
+
}
|
|
73
|
+
function orderedChildren(node, nodesById, childrenByParent) {
|
|
74
|
+
const children = [];
|
|
75
|
+
const seen = new Set();
|
|
76
|
+
for (const childId of node.childIds ?? []) {
|
|
77
|
+
const child = nodesById.get(childId);
|
|
78
|
+
if (child && !seen.has(child.nodeId)) {
|
|
79
|
+
seen.add(child.nodeId);
|
|
80
|
+
children.push(child);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
for (const child of childrenByParent.get(node.nodeId) ?? []) {
|
|
84
|
+
if (!seen.has(child.nodeId)) {
|
|
85
|
+
seen.add(child.nodeId);
|
|
86
|
+
children.push(child);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
return children;
|
|
90
|
+
}
|
|
91
|
+
function normalizeText(value) {
|
|
92
|
+
return String(value ?? "")
|
|
93
|
+
.replace(/\s+/g, " ")
|
|
94
|
+
.trim();
|
|
95
|
+
}
|
|
96
|
+
function splitText(value, max = 700) {
|
|
97
|
+
const text = normalizeText(value);
|
|
98
|
+
if (!text)
|
|
99
|
+
return [];
|
|
100
|
+
const parts = [];
|
|
101
|
+
for (let start = 0; start < text.length; start += max) {
|
|
102
|
+
parts.push(text.slice(start, start + max));
|
|
103
|
+
}
|
|
104
|
+
return parts;
|
|
105
|
+
}
|
|
106
|
+
function interactiveState(node) {
|
|
107
|
+
const values = new Map((node.properties ?? []).map((property) => [
|
|
108
|
+
property.name,
|
|
109
|
+
property.value?.value,
|
|
110
|
+
]));
|
|
111
|
+
const checkedValue = values.get("checked");
|
|
112
|
+
const checked = checkedValue === "true"
|
|
113
|
+
? true
|
|
114
|
+
: checkedValue === "false"
|
|
115
|
+
? false
|
|
116
|
+
: typeof checkedValue === "boolean" || checkedValue === "mixed"
|
|
117
|
+
? checkedValue
|
|
118
|
+
: undefined;
|
|
119
|
+
const selected = values.get("selected");
|
|
120
|
+
const expanded = values.get("expanded");
|
|
121
|
+
const disabled = values.get("disabled");
|
|
122
|
+
return {
|
|
123
|
+
...(checked === undefined ? {} : { checked }),
|
|
124
|
+
...(typeof selected === "boolean" ? { selected } : {}),
|
|
125
|
+
...(typeof expanded === "boolean" ? { expanded } : {}),
|
|
126
|
+
...(typeof disabled === "boolean" ? { disabled } : {}),
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
function interactiveText(role, name, value, state) {
|
|
130
|
+
const states = [
|
|
131
|
+
state.checked === undefined ? undefined : `checked=${state.checked}`,
|
|
132
|
+
state.selected === undefined ? undefined : `selected=${state.selected}`,
|
|
133
|
+
state.expanded === undefined ? undefined : `expanded=${state.expanded}`,
|
|
134
|
+
state.disabled === undefined ? undefined : `disabled=${state.disabled}`,
|
|
135
|
+
].filter((entry) => entry !== undefined);
|
|
136
|
+
return `${role}${name ? ` ${name}` : ""}${value === "" || value == null ? "" : ` = ${JSON.stringify(value)}`}${states.length > 0 ? ` [${states.join(", ")}]` : ""}`;
|
|
137
|
+
}
|
|
138
|
+
export function renderSnapshotTree(rawNodes, elementRefs) {
|
|
139
|
+
const nodes = rawNodes.map(axNode);
|
|
140
|
+
const nodesById = new Map(nodes.map((node) => [node.nodeId, node]));
|
|
141
|
+
const childrenByParent = new Map();
|
|
142
|
+
for (const node of nodes) {
|
|
143
|
+
if (!node.parentId)
|
|
144
|
+
continue;
|
|
145
|
+
const children = childrenByParent.get(node.parentId) ?? [];
|
|
146
|
+
children.push(node);
|
|
147
|
+
childrenByParent.set(node.parentId, children);
|
|
148
|
+
}
|
|
149
|
+
elementRefs.clear();
|
|
150
|
+
const lines = [];
|
|
151
|
+
const elements = [];
|
|
152
|
+
const visited = new Set();
|
|
153
|
+
const nextElementId = () => {
|
|
154
|
+
const id = NEXT_ELEMENT_IDS.get(elementRefs) ?? 1;
|
|
155
|
+
if (!Number.isSafeInteger(id)) {
|
|
156
|
+
throw new Error("Element id limit reached; restart the tab bridge");
|
|
157
|
+
}
|
|
158
|
+
NEXT_ELEMENT_IDS.set(elementRefs, id + 1);
|
|
159
|
+
return id;
|
|
160
|
+
};
|
|
161
|
+
const addLine = (text, element, kind = "text") => {
|
|
162
|
+
for (const part of splitText(text)) {
|
|
163
|
+
lines.push({
|
|
164
|
+
line: lines.length + 1,
|
|
165
|
+
text: part,
|
|
166
|
+
kind,
|
|
167
|
+
...(element ? { element_id: element.id } : {}),
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
};
|
|
171
|
+
const addStaticText = (text) => {
|
|
172
|
+
const normalized = normalizeText(text);
|
|
173
|
+
if (!normalized)
|
|
174
|
+
return;
|
|
175
|
+
const previous = lines.at(-1);
|
|
176
|
+
if (previous?.kind === "text" &&
|
|
177
|
+
previous.text.length + normalized.length + 1 <= 700) {
|
|
178
|
+
previous.text += ` ${normalized}`;
|
|
179
|
+
return;
|
|
180
|
+
}
|
|
181
|
+
addLine(normalized);
|
|
182
|
+
};
|
|
183
|
+
const visit = (node, parentName = "") => {
|
|
184
|
+
if (!node || visited.has(node.nodeId))
|
|
185
|
+
return;
|
|
186
|
+
visited.add(node.nodeId);
|
|
187
|
+
const role = String(node.role?.value ?? "");
|
|
188
|
+
const name = normalizeText(node.name?.value ?? "");
|
|
189
|
+
const value = node.value?.value;
|
|
190
|
+
let renderedName = parentName;
|
|
191
|
+
if (!node.ignored && shouldShow(node)) {
|
|
192
|
+
if (INTERACTIVE_ROLES.has(role)) {
|
|
193
|
+
const state = interactiveState(node);
|
|
194
|
+
const text = interactiveText(role, name, value, state);
|
|
195
|
+
if (!node.backendDOMNodeId) {
|
|
196
|
+
addLine(text, undefined, "interactive");
|
|
197
|
+
renderedName = name;
|
|
198
|
+
}
|
|
199
|
+
else {
|
|
200
|
+
const href = role === "link"
|
|
201
|
+
? node.properties?.find((property) => property.name === "url")
|
|
202
|
+
?.value?.value
|
|
203
|
+
: undefined;
|
|
204
|
+
const element = {
|
|
205
|
+
id: nextElementId(),
|
|
206
|
+
role,
|
|
207
|
+
...(name ? { name } : {}),
|
|
208
|
+
...(typeof href === "string" && href ? { href } : {}),
|
|
209
|
+
...(value === "" || value == null ? {} : { value }),
|
|
210
|
+
...state,
|
|
211
|
+
};
|
|
212
|
+
elementRefs.set(element.id, node.backendDOMNodeId);
|
|
213
|
+
elements.push(element);
|
|
214
|
+
addLine(`[${element.id}] ${text}`, element, "interactive");
|
|
215
|
+
renderedName = name;
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
else if (role === "StaticText") {
|
|
219
|
+
if (name && name !== parentName)
|
|
220
|
+
addStaticText(name);
|
|
221
|
+
}
|
|
222
|
+
else if (role === "heading" || role === "image") {
|
|
223
|
+
if (name)
|
|
224
|
+
addLine(`${role}: ${name}`, undefined, role);
|
|
225
|
+
renderedName = name || parentName;
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
for (const child of orderedChildren(node, nodesById, childrenByParent)) {
|
|
229
|
+
visit(child, renderedName);
|
|
230
|
+
}
|
|
231
|
+
};
|
|
232
|
+
for (const root of nodes.filter((node) => !node.parentId || !nodesById.has(node.parentId))) {
|
|
233
|
+
visit(root);
|
|
234
|
+
}
|
|
235
|
+
for (const node of nodes)
|
|
236
|
+
visit(node);
|
|
237
|
+
return { lines, elements };
|
|
238
|
+
}
|
package/dist/src/cdp/snapshot.js
CHANGED
|
@@ -1,142 +1,7 @@
|
|
|
1
1
|
import { evaluate } from "./evaluate.js";
|
|
2
2
|
import { positiveInteger, SNAPSHOT_LIMITS, snapshotResponseLength, } from "./snapshot-contract.js";
|
|
3
|
+
import { renderSnapshotTree } from "./snapshot-tree.js";
|
|
3
4
|
import { asRecord } from "./types.js";
|
|
4
|
-
const INTERACTIVE_ROLES = new Set([
|
|
5
|
-
"button",
|
|
6
|
-
"checkbox",
|
|
7
|
-
"combobox",
|
|
8
|
-
"link",
|
|
9
|
-
"listbox",
|
|
10
|
-
"menuitem",
|
|
11
|
-
"option",
|
|
12
|
-
"menuitemcheckbox",
|
|
13
|
-
"menuitemradio",
|
|
14
|
-
"radio",
|
|
15
|
-
"searchbox",
|
|
16
|
-
"slider",
|
|
17
|
-
"spinbutton",
|
|
18
|
-
"switch",
|
|
19
|
-
"tab",
|
|
20
|
-
"textbox",
|
|
21
|
-
"treeitem",
|
|
22
|
-
]);
|
|
23
|
-
const NEXT_ELEMENT_IDS = new WeakMap();
|
|
24
|
-
function axProperty(value) {
|
|
25
|
-
const record = asRecord(value, "accessibility property");
|
|
26
|
-
if (typeof record["name"] !== "string") {
|
|
27
|
-
throw new Error("Accessibility property has no name");
|
|
28
|
-
}
|
|
29
|
-
return {
|
|
30
|
-
name: record["name"],
|
|
31
|
-
...(record["value"] ? { value: asRecord(record["value"]) } : {}),
|
|
32
|
-
};
|
|
33
|
-
}
|
|
34
|
-
function axNode(value) {
|
|
35
|
-
const record = asRecord(value, "accessibility node");
|
|
36
|
-
if (typeof record["nodeId"] !== "string") {
|
|
37
|
-
throw new Error("Accessibility node has no nodeId");
|
|
38
|
-
}
|
|
39
|
-
return {
|
|
40
|
-
nodeId: record["nodeId"],
|
|
41
|
-
...(typeof record["parentId"] === "string"
|
|
42
|
-
? { parentId: record["parentId"] }
|
|
43
|
-
: {}),
|
|
44
|
-
...(Array.isArray(record["childIds"])
|
|
45
|
-
? {
|
|
46
|
-
childIds: record["childIds"].filter((id) => typeof id === "string"),
|
|
47
|
-
}
|
|
48
|
-
: {}),
|
|
49
|
-
...(typeof record["backendDOMNodeId"] === "number"
|
|
50
|
-
? { backendDOMNodeId: record["backendDOMNodeId"] }
|
|
51
|
-
: {}),
|
|
52
|
-
...(typeof record["ignored"] === "boolean"
|
|
53
|
-
? { ignored: record["ignored"] }
|
|
54
|
-
: {}),
|
|
55
|
-
...(record["role"] ? { role: asRecord(record["role"]) } : {}),
|
|
56
|
-
...(record["name"] ? { name: asRecord(record["name"]) } : {}),
|
|
57
|
-
...(record["value"] ? { value: asRecord(record["value"]) } : {}),
|
|
58
|
-
...(Array.isArray(record["properties"])
|
|
59
|
-
? { properties: record["properties"].map(axProperty) }
|
|
60
|
-
: {}),
|
|
61
|
-
};
|
|
62
|
-
}
|
|
63
|
-
function shouldShow(node) {
|
|
64
|
-
const role = String(node.role?.value ?? "");
|
|
65
|
-
const name = node.name?.value ?? "";
|
|
66
|
-
const value = node.value?.value;
|
|
67
|
-
if (role === "InlineTextBox")
|
|
68
|
-
return false;
|
|
69
|
-
if (INTERACTIVE_ROLES.has(role))
|
|
70
|
-
return true;
|
|
71
|
-
return (role !== "none" &&
|
|
72
|
-
role !== "generic" &&
|
|
73
|
-
!(name === "" && (value === "" || value == null)));
|
|
74
|
-
}
|
|
75
|
-
function orderedChildren(node, nodesById, childrenByParent) {
|
|
76
|
-
const children = [];
|
|
77
|
-
const seen = new Set();
|
|
78
|
-
for (const childId of node.childIds ?? []) {
|
|
79
|
-
const child = nodesById.get(childId);
|
|
80
|
-
if (child && !seen.has(child.nodeId)) {
|
|
81
|
-
seen.add(child.nodeId);
|
|
82
|
-
children.push(child);
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
for (const child of childrenByParent.get(node.nodeId) ?? []) {
|
|
86
|
-
if (!seen.has(child.nodeId)) {
|
|
87
|
-
seen.add(child.nodeId);
|
|
88
|
-
children.push(child);
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
return children;
|
|
92
|
-
}
|
|
93
|
-
function normalizeText(value) {
|
|
94
|
-
return String(value ?? "")
|
|
95
|
-
.replace(/\s+/g, " ")
|
|
96
|
-
.trim();
|
|
97
|
-
}
|
|
98
|
-
function splitText(value, max = 700) {
|
|
99
|
-
const text = normalizeText(value);
|
|
100
|
-
if (!text)
|
|
101
|
-
return [];
|
|
102
|
-
const parts = [];
|
|
103
|
-
for (let start = 0; start < text.length; start += max) {
|
|
104
|
-
parts.push(text.slice(start, start + max));
|
|
105
|
-
}
|
|
106
|
-
return parts;
|
|
107
|
-
}
|
|
108
|
-
function interactiveState(node) {
|
|
109
|
-
const values = new Map((node.properties ?? []).map((property) => [
|
|
110
|
-
property.name,
|
|
111
|
-
property.value?.value,
|
|
112
|
-
]));
|
|
113
|
-
const checkedValue = values.get("checked");
|
|
114
|
-
const checked = checkedValue === "true"
|
|
115
|
-
? true
|
|
116
|
-
: checkedValue === "false"
|
|
117
|
-
? false
|
|
118
|
-
: typeof checkedValue === "boolean" || checkedValue === "mixed"
|
|
119
|
-
? checkedValue
|
|
120
|
-
: undefined;
|
|
121
|
-
const selected = values.get("selected");
|
|
122
|
-
const expanded = values.get("expanded");
|
|
123
|
-
const disabled = values.get("disabled");
|
|
124
|
-
return {
|
|
125
|
-
...(checked === undefined ? {} : { checked }),
|
|
126
|
-
...(typeof selected === "boolean" ? { selected } : {}),
|
|
127
|
-
...(typeof expanded === "boolean" ? { expanded } : {}),
|
|
128
|
-
...(typeof disabled === "boolean" ? { disabled } : {}),
|
|
129
|
-
};
|
|
130
|
-
}
|
|
131
|
-
function interactiveText(role, name, value, state) {
|
|
132
|
-
const states = [
|
|
133
|
-
state.checked === undefined ? undefined : `checked=${state.checked}`,
|
|
134
|
-
state.selected === undefined ? undefined : `selected=${state.selected}`,
|
|
135
|
-
state.expanded === undefined ? undefined : `expanded=${state.expanded}`,
|
|
136
|
-
state.disabled === undefined ? undefined : `disabled=${state.disabled}`,
|
|
137
|
-
].filter((entry) => entry !== undefined);
|
|
138
|
-
return `${role}${name ? ` ${name}` : ""}${value === "" || value == null ? "" : ` = ${JSON.stringify(value)}`}${states.length > 0 ? ` [${states.join(", ")}]` : ""}`;
|
|
139
|
-
}
|
|
140
5
|
export async function snapshotData(cdp, sessionId, elementRefs, options = {}) {
|
|
141
6
|
const lineno = positiveInteger(options.lineno ?? 1, "line cursor");
|
|
142
7
|
const responseLength = snapshotResponseLength(options.responseLength);
|
|
@@ -144,104 +9,7 @@ export async function snapshotData(cdp, sessionId, elementRefs, options = {}) {
|
|
|
144
9
|
if (!Array.isArray(response["nodes"])) {
|
|
145
10
|
throw new Error("Accessibility response has no nodes");
|
|
146
11
|
}
|
|
147
|
-
const
|
|
148
|
-
const nodesById = new Map(nodes.map((node) => [node.nodeId, node]));
|
|
149
|
-
const childrenByParent = new Map();
|
|
150
|
-
for (const node of nodes) {
|
|
151
|
-
if (!node.parentId)
|
|
152
|
-
continue;
|
|
153
|
-
const children = childrenByParent.get(node.parentId) ?? [];
|
|
154
|
-
children.push(node);
|
|
155
|
-
childrenByParent.set(node.parentId, children);
|
|
156
|
-
}
|
|
157
|
-
elementRefs.clear();
|
|
158
|
-
const lines = [];
|
|
159
|
-
const elements = [];
|
|
160
|
-
const visited = new Set();
|
|
161
|
-
const nextElementId = () => {
|
|
162
|
-
const id = NEXT_ELEMENT_IDS.get(elementRefs) ?? 1;
|
|
163
|
-
if (!Number.isSafeInteger(id)) {
|
|
164
|
-
throw new Error("Element id limit reached; restart the tab bridge");
|
|
165
|
-
}
|
|
166
|
-
NEXT_ELEMENT_IDS.set(elementRefs, id + 1);
|
|
167
|
-
return id;
|
|
168
|
-
};
|
|
169
|
-
const addLine = (text, element, kind = "text") => {
|
|
170
|
-
for (const part of splitText(text)) {
|
|
171
|
-
lines.push({
|
|
172
|
-
line: lines.length + 1,
|
|
173
|
-
text: part,
|
|
174
|
-
kind,
|
|
175
|
-
...(element ? { element_id: element.id } : {}),
|
|
176
|
-
});
|
|
177
|
-
}
|
|
178
|
-
};
|
|
179
|
-
const addStaticText = (text) => {
|
|
180
|
-
const normalized = normalizeText(text);
|
|
181
|
-
if (!normalized)
|
|
182
|
-
return;
|
|
183
|
-
const previous = lines.at(-1);
|
|
184
|
-
if (previous?.kind === "text" &&
|
|
185
|
-
previous.text.length + normalized.length + 1 <= 700) {
|
|
186
|
-
previous.text += ` ${normalized}`;
|
|
187
|
-
return;
|
|
188
|
-
}
|
|
189
|
-
addLine(normalized);
|
|
190
|
-
};
|
|
191
|
-
const visit = (node, parentName = "") => {
|
|
192
|
-
if (!node || visited.has(node.nodeId))
|
|
193
|
-
return;
|
|
194
|
-
visited.add(node.nodeId);
|
|
195
|
-
const role = String(node.role?.value ?? "");
|
|
196
|
-
const name = normalizeText(node.name?.value ?? "");
|
|
197
|
-
const value = node.value?.value;
|
|
198
|
-
let renderedName = parentName;
|
|
199
|
-
if (!node.ignored && shouldShow(node)) {
|
|
200
|
-
if (INTERACTIVE_ROLES.has(role)) {
|
|
201
|
-
const state = interactiveState(node);
|
|
202
|
-
const text = interactiveText(role, name, value, state);
|
|
203
|
-
if (!node.backendDOMNodeId) {
|
|
204
|
-
addLine(text, undefined, "interactive");
|
|
205
|
-
renderedName = name;
|
|
206
|
-
}
|
|
207
|
-
else {
|
|
208
|
-
const href = role === "link"
|
|
209
|
-
? node.properties?.find((property) => property.name === "url")
|
|
210
|
-
?.value?.value
|
|
211
|
-
: undefined;
|
|
212
|
-
const element = {
|
|
213
|
-
id: nextElementId(),
|
|
214
|
-
role,
|
|
215
|
-
...(name ? { name } : {}),
|
|
216
|
-
...(typeof href === "string" && href ? { href } : {}),
|
|
217
|
-
...(value === "" || value == null ? {} : { value }),
|
|
218
|
-
...state,
|
|
219
|
-
};
|
|
220
|
-
elementRefs.set(element.id, node.backendDOMNodeId);
|
|
221
|
-
elements.push(element);
|
|
222
|
-
addLine(`[${element.id}] ${text}`, element, "interactive");
|
|
223
|
-
renderedName = name;
|
|
224
|
-
}
|
|
225
|
-
}
|
|
226
|
-
else if (role === "StaticText") {
|
|
227
|
-
if (name && name !== parentName)
|
|
228
|
-
addStaticText(name);
|
|
229
|
-
}
|
|
230
|
-
else if (role === "heading" || role === "image") {
|
|
231
|
-
if (name)
|
|
232
|
-
addLine(`${role}: ${name}`, undefined, role);
|
|
233
|
-
renderedName = name || parentName;
|
|
234
|
-
}
|
|
235
|
-
}
|
|
236
|
-
for (const child of orderedChildren(node, nodesById, childrenByParent)) {
|
|
237
|
-
visit(child, renderedName);
|
|
238
|
-
}
|
|
239
|
-
};
|
|
240
|
-
for (const root of nodes.filter((node) => !node.parentId || !nodesById.has(node.parentId))) {
|
|
241
|
-
visit(root);
|
|
242
|
-
}
|
|
243
|
-
for (const node of nodes)
|
|
244
|
-
visit(node);
|
|
12
|
+
const { lines, elements } = renderSnapshotTree(response["nodes"], elementRefs);
|
|
245
13
|
const metadata = asRecord(await evaluate(cdp, sessionId, "({title: document.title, url: location.href})", options.signal), "page metadata");
|
|
246
14
|
const pattern = options.pattern?.toLowerCase();
|
|
247
15
|
const matching = pattern
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { CdpClient } from "./client.js";
|
|
2
|
+
import type { ElementRefs } from "./types.js";
|
|
3
|
+
export interface ActiveTab {
|
|
4
|
+
cdp: CdpClient;
|
|
5
|
+
sessionId: string;
|
|
6
|
+
elementRefs: ElementRefs;
|
|
7
|
+
refId: string;
|
|
8
|
+
}
|
|
9
|
+
export declare class TabBridge {
|
|
10
|
+
readonly elementRefs: ElementRefs;
|
|
11
|
+
readonly targetId: string;
|
|
12
|
+
readonly cdp: CdpClient;
|
|
13
|
+
readonly sessionId: string;
|
|
14
|
+
private tail;
|
|
15
|
+
private idleTimer;
|
|
16
|
+
private closed;
|
|
17
|
+
private backgroundRendering;
|
|
18
|
+
private readonly onClose;
|
|
19
|
+
private constructor();
|
|
20
|
+
static connect(targetId: string, onClose: () => void, signal?: AbortSignal): Promise<TabBridge>;
|
|
21
|
+
run<T>(refId: string, signal: AbortSignal | undefined, action: (tab: ActiveTab) => Promise<T>): Promise<T>;
|
|
22
|
+
setBackgroundRendering(enabled: boolean, signal?: AbortSignal): Promise<void>;
|
|
23
|
+
close(detach?: boolean): void;
|
|
24
|
+
private resetIdle;
|
|
25
|
+
}
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { CdpClient } from "./client.js";
|
|
2
|
+
import { getWebSocketUrl } from "./discovery.js";
|
|
3
|
+
import { waitForTurn } from "./serial.js";
|
|
4
|
+
import { asRecord } from "./types.js";
|
|
5
|
+
const TAB_IDLE_MS = 20 * 60 * 1_000;
|
|
6
|
+
export class TabBridge {
|
|
7
|
+
elementRefs = new Map();
|
|
8
|
+
targetId;
|
|
9
|
+
cdp;
|
|
10
|
+
sessionId;
|
|
11
|
+
tail = Promise.resolve();
|
|
12
|
+
idleTimer;
|
|
13
|
+
closed = false;
|
|
14
|
+
backgroundRendering = false;
|
|
15
|
+
onClose;
|
|
16
|
+
constructor(targetId, cdp, sessionId, onClose) {
|
|
17
|
+
this.targetId = targetId;
|
|
18
|
+
this.cdp = cdp;
|
|
19
|
+
this.sessionId = sessionId;
|
|
20
|
+
this.onClose = onClose;
|
|
21
|
+
this.resetIdle();
|
|
22
|
+
cdp.onEvent("Target.targetDestroyed", (value) => {
|
|
23
|
+
const event = asRecord(value, "target destroyed event");
|
|
24
|
+
if (event["targetId"] === targetId)
|
|
25
|
+
this.close(false);
|
|
26
|
+
});
|
|
27
|
+
cdp.onEvent("Target.detachedFromTarget", (value) => {
|
|
28
|
+
const event = asRecord(value, "target detached event");
|
|
29
|
+
if (event["sessionId"] === sessionId)
|
|
30
|
+
this.close(false);
|
|
31
|
+
});
|
|
32
|
+
cdp.onClose(() => this.close(false));
|
|
33
|
+
}
|
|
34
|
+
static async connect(targetId, onClose, signal) {
|
|
35
|
+
const cdp = new CdpClient();
|
|
36
|
+
try {
|
|
37
|
+
await cdp.connect(await getWebSocketUrl(), signal);
|
|
38
|
+
const response = asRecord(await cdp.send("Target.attachToTarget", { targetId, flatten: true }, undefined, signal), "Target.attachToTarget response");
|
|
39
|
+
if (typeof response["sessionId"] !== "string") {
|
|
40
|
+
throw new Error("Chrome did not return a tab session");
|
|
41
|
+
}
|
|
42
|
+
return new TabBridge(targetId, cdp, response["sessionId"], onClose);
|
|
43
|
+
}
|
|
44
|
+
catch (error) {
|
|
45
|
+
cdp.close();
|
|
46
|
+
throw error;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
async run(refId, signal, action) {
|
|
50
|
+
if (this.closed)
|
|
51
|
+
throw new Error("Tab bridge is closed");
|
|
52
|
+
let release;
|
|
53
|
+
const turn = new Promise((resolveValue) => {
|
|
54
|
+
release = resolveValue;
|
|
55
|
+
});
|
|
56
|
+
const previous = this.tail;
|
|
57
|
+
this.tail = previous.then(() => turn, () => turn);
|
|
58
|
+
try {
|
|
59
|
+
await waitForTurn(previous, signal);
|
|
60
|
+
if (this.closed)
|
|
61
|
+
throw new Error("Tab bridge is closed");
|
|
62
|
+
this.resetIdle();
|
|
63
|
+
return await action({
|
|
64
|
+
cdp: this.cdp,
|
|
65
|
+
sessionId: this.sessionId,
|
|
66
|
+
elementRefs: this.elementRefs,
|
|
67
|
+
refId,
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
finally {
|
|
71
|
+
release();
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
async setBackgroundRendering(enabled, signal) {
|
|
75
|
+
if (this.backgroundRendering === enabled)
|
|
76
|
+
return;
|
|
77
|
+
// An interrupted command may already have applied. Keep the state unknown
|
|
78
|
+
// until Chrome acknowledges it, so the next action reestablishes it.
|
|
79
|
+
this.backgroundRendering = undefined;
|
|
80
|
+
await this.cdp.send("Emulation.setFocusEmulationEnabled", { enabled }, this.sessionId, signal);
|
|
81
|
+
this.backgroundRendering = enabled;
|
|
82
|
+
}
|
|
83
|
+
close(detach = true) {
|
|
84
|
+
if (this.closed)
|
|
85
|
+
return;
|
|
86
|
+
this.closed = true;
|
|
87
|
+
if (this.idleTimer)
|
|
88
|
+
clearTimeout(this.idleTimer);
|
|
89
|
+
this.elementRefs.clear();
|
|
90
|
+
if (detach) {
|
|
91
|
+
void this.cdp
|
|
92
|
+
.send("Target.detachFromTarget", { sessionId: this.sessionId }, undefined, undefined)
|
|
93
|
+
.catch(() => undefined)
|
|
94
|
+
.finally(() => this.cdp.close());
|
|
95
|
+
}
|
|
96
|
+
else {
|
|
97
|
+
this.cdp.close();
|
|
98
|
+
}
|
|
99
|
+
this.onClose();
|
|
100
|
+
}
|
|
101
|
+
resetIdle() {
|
|
102
|
+
if (this.idleTimer)
|
|
103
|
+
clearTimeout(this.idleTimer);
|
|
104
|
+
this.idleTimer = setTimeout(() => this.close(), TAB_IDLE_MS);
|
|
105
|
+
this.idleTimer.unref?.();
|
|
106
|
+
}
|
|
107
|
+
}
|