@nexum-hub/browser 0.0.0-stage → 0.0.2
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/README.md +22 -2
- package/browser-extension/assets.js +654 -0
- package/browser-extension/build.mjs +111 -0
- package/browser-extension/clipboard.js +397 -0
- package/browser-extension/connection.js +880 -0
- package/browser-extension/dist/development/assets.js +654 -0
- package/browser-extension/dist/development/clipboard.js +397 -0
- package/browser-extension/dist/development/connection.js +880 -0
- package/browser-extension/dist/development/errors.js +106 -0
- package/browser-extension/dist/development/evaluate.js +160 -0
- package/browser-extension/dist/development/frames.js +256 -0
- package/browser-extension/dist/development/keyboard.js +167 -0
- package/browser-extension/dist/development/manifest.json +35 -0
- package/browser-extension/dist/development/native-node.js +85 -0
- package/browser-extension/dist/development/page-data.js +497 -0
- package/browser-extension/dist/development/page.js +772 -0
- package/browser-extension/dist/development/profile.js +2 -0
- package/browser-extension/dist/development/router.js +5961 -0
- package/browser-extension/dist/development/site-source.js +149 -0
- package/browser-extension/dist/development/transport.js +405 -0
- package/browser-extension/dist/development/visual.js +976 -0
- package/browser-extension/dist/development/worker.js +24 -0
- package/browser-extension/dist/production/assets.js +654 -0
- package/browser-extension/dist/production/clipboard.js +397 -0
- package/browser-extension/dist/production/connection.js +880 -0
- package/browser-extension/dist/production/errors.js +106 -0
- package/browser-extension/dist/production/evaluate.js +160 -0
- package/browser-extension/dist/production/frames.js +256 -0
- package/browser-extension/dist/production/keyboard.js +167 -0
- package/browser-extension/dist/production/manifest.json +35 -0
- package/browser-extension/dist/production/native-node.js +85 -0
- package/browser-extension/dist/production/page-data.js +497 -0
- package/browser-extension/dist/production/page.js +772 -0
- package/browser-extension/dist/production/profile.js +2 -0
- package/browser-extension/dist/production/router.js +5961 -0
- package/browser-extension/dist/production/site-source.js +149 -0
- package/browser-extension/dist/production/transport.js +405 -0
- package/browser-extension/dist/production/visual.js +976 -0
- package/browser-extension/dist/production/worker.js +24 -0
- package/browser-extension/errors.js +106 -0
- package/browser-extension/evaluate.js +160 -0
- package/browser-extension/frames.js +256 -0
- package/browser-extension/keyboard.js +167 -0
- package/browser-extension/native-node.js +85 -0
- package/browser-extension/page-data.js +497 -0
- package/browser-extension/page.js +772 -0
- package/browser-extension/release-info.json +21 -0
- package/browser-extension/route-manifest.json +807 -0
- package/browser-extension/router.js +5961 -0
- package/browser-extension/site-source.js +149 -0
- package/browser-extension/transport.js +405 -0
- package/browser-extension/visual.js +976 -0
- package/browser-extension/worker.js +24 -0
- package/dist/client/index.d.ts +6 -0
- package/dist/client/index.js +894 -0
- package/dist/client/index.js.map +1 -0
- package/dist/client/routes.d.ts +6 -0
- package/dist/client/routes.js +777 -0
- package/dist/client/routes.js.map +1 -0
- package/dist/client/serialize.d.ts +16 -0
- package/dist/client/serialize.js +355 -0
- package/dist/client/serialize.js.map +1 -0
- package/dist/client/types.d.ts +1342 -0
- package/dist/client/types.js +2 -0
- package/dist/client/types.js.map +1 -0
- package/dist/extension/artifacts.d.ts +60 -0
- package/dist/extension/artifacts.js +493 -0
- package/dist/extension/artifacts.js.map +1 -0
- package/dist/extension/assets.d.ts +10 -0
- package/dist/extension/assets.js +90 -0
- package/dist/extension/assets.js.map +1 -0
- package/dist/extension/batch.d.ts +6 -0
- package/dist/extension/batch.js +138 -0
- package/dist/extension/batch.js.map +1 -0
- package/dist/extension/files.d.ts +16 -0
- package/dist/extension/files.js +594 -0
- package/dist/extension/files.js.map +1 -0
- package/dist/extension/index.d.ts +3 -0
- package/dist/extension/index.js +623 -0
- package/dist/extension/index.js.map +1 -0
- package/dist/extension/protocol.d.ts +81 -0
- package/dist/extension/protocol.js +475 -0
- package/dist/extension/protocol.js.map +1 -0
- package/dist/extension/scope.d.ts +55 -0
- package/dist/extension/scope.js +563 -0
- package/dist/extension/scope.js.map +1 -0
- package/dist/extension/service.d.ts +113 -0
- package/dist/extension/service.js +905 -0
- package/dist/extension/service.js.map +1 -0
- package/docs/README.md +10 -0
- package/docs/api.md +1375 -0
- package/docs/connection.md +9 -0
- package/docs/lifecycle.md +17 -0
- package/docs/usage.md +75 -0
- package/implementation.json +2962 -0
- package/instructions/README.md +8 -0
- package/package.json +80 -4
- package/scripts/discover.js +12 -0
- package/scripts/screenshot.js +9 -0
- package/skills/browser-control/SKILL.md +12 -0
|
@@ -0,0 +1,772 @@
|
|
|
1
|
+
/** Serialized by chrome.scripting into the extension's isolated page world. No outer closure is used. */
|
|
2
|
+
export async function pageCommand(input) {
|
|
3
|
+
const failure = (code, message) => {
|
|
4
|
+
throw Object.assign(new Error(message), { code });
|
|
5
|
+
};
|
|
6
|
+
const check = (
|
|
7
|
+
value,
|
|
8
|
+
code = "INVALID_ARGUMENT",
|
|
9
|
+
message = "The page input is invalid.",
|
|
10
|
+
) => {
|
|
11
|
+
if (!value) failure(code, message);
|
|
12
|
+
};
|
|
13
|
+
try {
|
|
14
|
+
const slot = Symbol.for("nexum.browser.document.v1");
|
|
15
|
+
let state = globalThis[slot];
|
|
16
|
+
if (
|
|
17
|
+
!state ||
|
|
18
|
+
state.document !== document ||
|
|
19
|
+
state.version !== input.pageVersion
|
|
20
|
+
) {
|
|
21
|
+
state = {
|
|
22
|
+
document,
|
|
23
|
+
version: input.pageVersion,
|
|
24
|
+
token: [...crypto.getRandomValues(new Uint8Array(16))]
|
|
25
|
+
.map((value) => value.toString(16).padStart(2, "0"))
|
|
26
|
+
.join(""),
|
|
27
|
+
next: 0,
|
|
28
|
+
refs: new Map(),
|
|
29
|
+
reverse: new WeakMap(),
|
|
30
|
+
};
|
|
31
|
+
Object.defineProperty(globalThis, slot, {
|
|
32
|
+
value: state,
|
|
33
|
+
configurable: true,
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
const maxBytes = Math.min(input.maxBytes ?? 65536, 1024 * 1024);
|
|
37
|
+
const encodeSize = (value) =>
|
|
38
|
+
new TextEncoder().encode(
|
|
39
|
+
typeof value === "string" ? value : JSON.stringify(value),
|
|
40
|
+
).byteLength;
|
|
41
|
+
const bounded = (value) => {
|
|
42
|
+
check(
|
|
43
|
+
encodeSize(value) <= maxBytes,
|
|
44
|
+
"OUTPUT_TOO_LARGE",
|
|
45
|
+
"Use a scoped observation or an exported file for this page result.",
|
|
46
|
+
);
|
|
47
|
+
return value;
|
|
48
|
+
};
|
|
49
|
+
const owned = (node) =>
|
|
50
|
+
!!node.closest?.(
|
|
51
|
+
'[data-nexum-visual-layer="v1"],[data-nexum-visual-icon="v1"]',
|
|
52
|
+
);
|
|
53
|
+
const elements = (root) =>
|
|
54
|
+
[...root.querySelectorAll("*")].filter((node) => !owned(node));
|
|
55
|
+
const visible = (node) => {
|
|
56
|
+
if (!(node instanceof Element) || !node.isConnected) return false;
|
|
57
|
+
const style = getComputedStyle(node),
|
|
58
|
+
rect = node.getBoundingClientRect();
|
|
59
|
+
return (
|
|
60
|
+
style.display !== "none" &&
|
|
61
|
+
style.visibility !== "hidden" &&
|
|
62
|
+
style.visibility !== "collapse" &&
|
|
63
|
+
rect.width > 0 &&
|
|
64
|
+
rect.height > 0
|
|
65
|
+
);
|
|
66
|
+
};
|
|
67
|
+
const enabled = (node) => {
|
|
68
|
+
// The native pseudo-class includes controls disabled by a fieldset or
|
|
69
|
+
// optgroup, even when their own `disabled` property remains false.
|
|
70
|
+
if (node.disabled || node.matches?.(":disabled")) return false;
|
|
71
|
+
for (
|
|
72
|
+
let current = node;
|
|
73
|
+
current;
|
|
74
|
+
current = current.parentElement ?? current.getRootNode?.()?.host ?? null
|
|
75
|
+
) {
|
|
76
|
+
const state = current.getAttribute("aria-disabled")?.toLowerCase();
|
|
77
|
+
if (state === "true") return false;
|
|
78
|
+
if (state === "false") return true;
|
|
79
|
+
}
|
|
80
|
+
return true;
|
|
81
|
+
};
|
|
82
|
+
const roleExposed = (node) => {
|
|
83
|
+
const style = getComputedStyle(node);
|
|
84
|
+
if (["hidden", "collapse"].includes(style.visibility)) return false;
|
|
85
|
+
for (let current = node; current; current = current.parentElement) {
|
|
86
|
+
if (
|
|
87
|
+
current.getAttribute("aria-hidden")?.toLowerCase() === "true" ||
|
|
88
|
+
current.inert ||
|
|
89
|
+
getComputedStyle(current).display === "none"
|
|
90
|
+
)
|
|
91
|
+
return false;
|
|
92
|
+
}
|
|
93
|
+
return true;
|
|
94
|
+
};
|
|
95
|
+
const role = (node) =>
|
|
96
|
+
node.getAttribute("role") ||
|
|
97
|
+
{
|
|
98
|
+
BUTTON: "button",
|
|
99
|
+
A: node.hasAttribute("href") ? "link" : "generic",
|
|
100
|
+
TEXTAREA: "textbox",
|
|
101
|
+
SELECT: "combobox",
|
|
102
|
+
IMG: "img",
|
|
103
|
+
H1: "heading",
|
|
104
|
+
H2: "heading",
|
|
105
|
+
H3: "heading",
|
|
106
|
+
INPUT: ["checkbox", "radio"].includes(node.type)
|
|
107
|
+
? node.type
|
|
108
|
+
: ["button", "submit", "reset"].includes(node.type)
|
|
109
|
+
? "button"
|
|
110
|
+
: "textbox",
|
|
111
|
+
}[node.tagName] ||
|
|
112
|
+
"generic";
|
|
113
|
+
const name = (node) =>
|
|
114
|
+
(node.getAttribute("aria-labelledby") || "")
|
|
115
|
+
.split(/\s+/)
|
|
116
|
+
.map((id) => document.getElementById(id)?.textContent?.trim() || "")
|
|
117
|
+
.filter(Boolean)
|
|
118
|
+
.join(" ")
|
|
119
|
+
.trim() ||
|
|
120
|
+
node.getAttribute("aria-label") ||
|
|
121
|
+
node.labels?.[0]?.textContent?.trim() ||
|
|
122
|
+
node.getAttribute("alt") ||
|
|
123
|
+
node.innerText?.trim() ||
|
|
124
|
+
node.textContent?.trim() ||
|
|
125
|
+
"";
|
|
126
|
+
const matcher = (value, rule, normalizeText = false) => {
|
|
127
|
+
check(rule && ["text", "regexp"].includes(rule.kind));
|
|
128
|
+
if (rule.kind === "regexp") {
|
|
129
|
+
check(
|
|
130
|
+
typeof rule.source === "string" &&
|
|
131
|
+
encodeSize(rule.source) <= 1024 &&
|
|
132
|
+
typeof rule.flags === "string",
|
|
133
|
+
);
|
|
134
|
+
return new RegExp(rule.source, rule.flags).test(value);
|
|
135
|
+
}
|
|
136
|
+
check(typeof rule.value === "string");
|
|
137
|
+
const normalize = (text) =>
|
|
138
|
+
text
|
|
139
|
+
.replace(/[\u200b\u00ad]/g, "")
|
|
140
|
+
.trim()
|
|
141
|
+
.replace(/\s+/g, " ");
|
|
142
|
+
const actual = normalizeText ? normalize(value) : value;
|
|
143
|
+
const expected = normalizeText ? normalize(rule.value) : rule.value;
|
|
144
|
+
return rule.exact
|
|
145
|
+
? actual === expected
|
|
146
|
+
: actual.toLocaleLowerCase().includes(expected.toLocaleLowerCase());
|
|
147
|
+
};
|
|
148
|
+
const skipText = (node) =>
|
|
149
|
+
["SCRIPT", "STYLE", "NOSCRIPT"].includes(node.tagName) ||
|
|
150
|
+
!!node.ownerDocument?.head?.contains(node);
|
|
151
|
+
const textValue = (node) => {
|
|
152
|
+
if (skipText(node)) return "";
|
|
153
|
+
const buttonValue = (element) =>
|
|
154
|
+
element.tagName === "INPUT" &&
|
|
155
|
+
["button", "submit", "reset"].includes(element.type?.toLowerCase());
|
|
156
|
+
if (buttonValue(node)) return node.value || "";
|
|
157
|
+
if (!node.childNodes) return node.textContent || "";
|
|
158
|
+
const pending = [...node.childNodes].reverse(),
|
|
159
|
+
parts = [];
|
|
160
|
+
while (pending.length) {
|
|
161
|
+
const child = pending.pop();
|
|
162
|
+
if (child.nodeType === 3) parts.push(child.nodeValue || "");
|
|
163
|
+
else if (child.nodeType === 1 && !skipText(child)) {
|
|
164
|
+
if (buttonValue(child)) parts.push(child.value || "");
|
|
165
|
+
else
|
|
166
|
+
for (let index = child.childNodes.length - 1; index >= 0; index--)
|
|
167
|
+
pending.push(child.childNodes[index]);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
return parts.join("");
|
|
171
|
+
};
|
|
172
|
+
const makeRef = (node) => {
|
|
173
|
+
let byLease = state.reverse.get(node);
|
|
174
|
+
if (!byLease) {
|
|
175
|
+
byLease = new Map();
|
|
176
|
+
state.reverse.set(node, byLease);
|
|
177
|
+
}
|
|
178
|
+
let ref = byLease.get(input.leaseId);
|
|
179
|
+
if (!ref) {
|
|
180
|
+
check(
|
|
181
|
+
state.refs.size < 10000,
|
|
182
|
+
"QUEUE_FULL",
|
|
183
|
+
"This document reached its fixed-reference budget.",
|
|
184
|
+
);
|
|
185
|
+
ref = `r_${input.leaseId}_${state.version}_${state.token}_${++state.next}`;
|
|
186
|
+
byLease.set(input.leaseId, ref);
|
|
187
|
+
state.refs.set(ref, node);
|
|
188
|
+
}
|
|
189
|
+
return ref;
|
|
190
|
+
};
|
|
191
|
+
const getRef = (ref, pageVersion = input.pageVersion) => {
|
|
192
|
+
check(
|
|
193
|
+
pageVersion === state.version &&
|
|
194
|
+
typeof ref === "string" &&
|
|
195
|
+
ref.startsWith(`r_${input.leaseId}_${state.version}_${state.token}_`),
|
|
196
|
+
"STALE_REF",
|
|
197
|
+
"The element observation belongs to another document.",
|
|
198
|
+
);
|
|
199
|
+
const node = state.refs.get(ref);
|
|
200
|
+
check(
|
|
201
|
+
node && node.isConnected,
|
|
202
|
+
"STALE_REF",
|
|
203
|
+
"The observed element is no longer available.",
|
|
204
|
+
);
|
|
205
|
+
return node;
|
|
206
|
+
};
|
|
207
|
+
let astNodes = new Set();
|
|
208
|
+
const query = (root, ast, depth = 0) => {
|
|
209
|
+
check(depth <= 16 && ast && typeof ast === "object");
|
|
210
|
+
astNodes.add(ast);
|
|
211
|
+
check(astNodes.size <= 128);
|
|
212
|
+
let result;
|
|
213
|
+
const sub = (value, scope = root) => query(scope, value, depth + 1);
|
|
214
|
+
switch (ast.kind) {
|
|
215
|
+
case "selector":
|
|
216
|
+
check(typeof ast.selector === "string");
|
|
217
|
+
result = [...root.querySelectorAll(ast.selector)];
|
|
218
|
+
break;
|
|
219
|
+
case "role":
|
|
220
|
+
result = elements(root).filter(
|
|
221
|
+
(n) =>
|
|
222
|
+
role(n) === ast.role &&
|
|
223
|
+
roleExposed(n) &&
|
|
224
|
+
(!ast.name ||
|
|
225
|
+
matcher(
|
|
226
|
+
name(n),
|
|
227
|
+
ast.name.kind === "text" && ast.exact !== undefined
|
|
228
|
+
? { ...ast.name, exact: ast.exact }
|
|
229
|
+
: ast.name,
|
|
230
|
+
)),
|
|
231
|
+
);
|
|
232
|
+
break;
|
|
233
|
+
case "label":
|
|
234
|
+
result = elements(root).filter(
|
|
235
|
+
(n) =>
|
|
236
|
+
matcher(name(n), ast.matcher) &&
|
|
237
|
+
(n.labels?.length ||
|
|
238
|
+
n.hasAttribute("aria-label") ||
|
|
239
|
+
n.hasAttribute("aria-labelledby")),
|
|
240
|
+
);
|
|
241
|
+
break;
|
|
242
|
+
case "placeholder":
|
|
243
|
+
result = elements(root).filter((n) =>
|
|
244
|
+
matcher(n.getAttribute("placeholder") || "", ast.matcher),
|
|
245
|
+
);
|
|
246
|
+
break;
|
|
247
|
+
case "testId":
|
|
248
|
+
result = elements(root).filter(
|
|
249
|
+
(n) => n.getAttribute("data-testid") === ast.value,
|
|
250
|
+
);
|
|
251
|
+
break;
|
|
252
|
+
case "text":
|
|
253
|
+
result = elements(root).filter(
|
|
254
|
+
(n) =>
|
|
255
|
+
!skipText(n) &&
|
|
256
|
+
matcher(textValue(n), ast.matcher, true) &&
|
|
257
|
+
![...n.children].some(
|
|
258
|
+
(c) => !skipText(c) && matcher(textValue(c), ast.matcher, true),
|
|
259
|
+
),
|
|
260
|
+
);
|
|
261
|
+
break;
|
|
262
|
+
case "descendant":
|
|
263
|
+
result = sub(ast.root).flatMap((n) => sub(ast.query, n));
|
|
264
|
+
break;
|
|
265
|
+
case "and": {
|
|
266
|
+
const right = new Set(sub(ast.right));
|
|
267
|
+
result = sub(ast.left).filter((n) => right.has(n));
|
|
268
|
+
break;
|
|
269
|
+
}
|
|
270
|
+
case "or":
|
|
271
|
+
result = [...new Set([...sub(ast.left), ...sub(ast.right)])].sort(
|
|
272
|
+
(left, right) => {
|
|
273
|
+
const position = left.compareDocumentPosition(right);
|
|
274
|
+
if (position & Node.DOCUMENT_POSITION_DISCONNECTED) return 0;
|
|
275
|
+
if (position & Node.DOCUMENT_POSITION_FOLLOWING) return -1;
|
|
276
|
+
if (position & Node.DOCUMENT_POSITION_PRECEDING) return 1;
|
|
277
|
+
return 0;
|
|
278
|
+
},
|
|
279
|
+
);
|
|
280
|
+
break;
|
|
281
|
+
case "position": {
|
|
282
|
+
const all = sub(ast.query);
|
|
283
|
+
check(
|
|
284
|
+
ast.at === "first" ||
|
|
285
|
+
ast.at === "last" ||
|
|
286
|
+
(Number.isInteger(ast.at) && ast.at >= 0),
|
|
287
|
+
);
|
|
288
|
+
const at =
|
|
289
|
+
ast.at === "first"
|
|
290
|
+
? 0
|
|
291
|
+
: ast.at === "last"
|
|
292
|
+
? all.length - 1
|
|
293
|
+
: ast.at;
|
|
294
|
+
result = at >= 0 && all[at] ? [all[at]] : [];
|
|
295
|
+
break;
|
|
296
|
+
}
|
|
297
|
+
case "filter":
|
|
298
|
+
result = sub(ast.query).filter(
|
|
299
|
+
(n) =>
|
|
300
|
+
(ast.visible === undefined || visible(n) === ast.visible) &&
|
|
301
|
+
(!ast.has || sub(ast.has, n).length > 0) &&
|
|
302
|
+
(!ast.hasNot || sub(ast.hasNot, n).length === 0) &&
|
|
303
|
+
(!ast.hasText || matcher(textValue(n), ast.hasText, true)) &&
|
|
304
|
+
(!ast.hasNotText || !matcher(textValue(n), ast.hasNotText, true)),
|
|
305
|
+
);
|
|
306
|
+
break;
|
|
307
|
+
case "ref":
|
|
308
|
+
result = [getRef(ast.ref, ast.pageVersion)];
|
|
309
|
+
break;
|
|
310
|
+
default:
|
|
311
|
+
failure("INVALID_ARGUMENT", "Unknown locator AST node.");
|
|
312
|
+
}
|
|
313
|
+
return [...new Set(result)].filter((node) => !owned(node));
|
|
314
|
+
};
|
|
315
|
+
let root = document;
|
|
316
|
+
const framePath = input.locator?.framePath || input.framePath || [];
|
|
317
|
+
check(Array.isArray(framePath) && framePath.length <= 8);
|
|
318
|
+
for (const selector of framePath) {
|
|
319
|
+
const frames = [...root.querySelectorAll(selector)];
|
|
320
|
+
check(
|
|
321
|
+
frames.length === 1,
|
|
322
|
+
frames.length > 1 ? "AMBIGUOUS_TARGET" : "INVALID_TARGET",
|
|
323
|
+
"A frame selector must match one frame.",
|
|
324
|
+
);
|
|
325
|
+
check(
|
|
326
|
+
frames[0].contentDocument,
|
|
327
|
+
"CAPABILITY_NOT_ALLOWED",
|
|
328
|
+
"This page-world route cannot enter this frame; use a registered native frame target.",
|
|
329
|
+
);
|
|
330
|
+
root = frames[0].contentDocument;
|
|
331
|
+
}
|
|
332
|
+
const all = () => {
|
|
333
|
+
astNodes = new Set();
|
|
334
|
+
if (input.ref) return [getRef(input.ref)];
|
|
335
|
+
if (input.nodeId) return [getRef(input.nodeId)];
|
|
336
|
+
return query(root, input.locator?.query ?? input.query);
|
|
337
|
+
};
|
|
338
|
+
const actionGeometry = (node, pending = false) => {
|
|
339
|
+
if (pending && !(visible(node) && enabled(node))) return null;
|
|
340
|
+
check(
|
|
341
|
+
visible(node) && enabled(node),
|
|
342
|
+
"INVALID_TARGET",
|
|
343
|
+
"The current element is not visible and enabled.",
|
|
344
|
+
);
|
|
345
|
+
node.scrollIntoView({
|
|
346
|
+
block: "center",
|
|
347
|
+
inline: "center",
|
|
348
|
+
behavior: "instant",
|
|
349
|
+
});
|
|
350
|
+
const r = rect(node),
|
|
351
|
+
position = input.command === "resolve" ? input.position : undefined;
|
|
352
|
+
const x = position?.x ?? r.width / 2,
|
|
353
|
+
y = position?.y ?? r.height / 2;
|
|
354
|
+
check(
|
|
355
|
+
Number.isFinite(x) &&
|
|
356
|
+
Number.isFinite(y) &&
|
|
357
|
+
x >= 0 &&
|
|
358
|
+
y >= 0 &&
|
|
359
|
+
x < r.width &&
|
|
360
|
+
y < r.height,
|
|
361
|
+
"INVALID_ARGUMENT",
|
|
362
|
+
"The requested position must be inside the current element rectangle.",
|
|
363
|
+
);
|
|
364
|
+
const point = { x: r.x + x, y: r.y + y };
|
|
365
|
+
const top = node.ownerDocument.elementFromPoint(point.x, point.y);
|
|
366
|
+
if (pending && !(top === node || node.contains(top))) return null;
|
|
367
|
+
check(
|
|
368
|
+
top === node || node.contains(top),
|
|
369
|
+
"INVALID_TARGET",
|
|
370
|
+
"Another element blocks the action target.",
|
|
371
|
+
);
|
|
372
|
+
return { rect: r, point };
|
|
373
|
+
};
|
|
374
|
+
const unique = (action = false, pending = false) => {
|
|
375
|
+
const found = all();
|
|
376
|
+
if (pending && found.length === 0) return null;
|
|
377
|
+
check(
|
|
378
|
+
found.length === 1,
|
|
379
|
+
found.length > 1 ? "AMBIGUOUS_TARGET" : "INVALID_TARGET",
|
|
380
|
+
"The locator must resolve to exactly one current element.",
|
|
381
|
+
);
|
|
382
|
+
const node = found[0];
|
|
383
|
+
if (action) actionGeometry(node);
|
|
384
|
+
return node;
|
|
385
|
+
};
|
|
386
|
+
const rect = (node) => {
|
|
387
|
+
const r = node.getBoundingClientRect();
|
|
388
|
+
return { x: r.x, y: r.y, width: r.width, height: r.height };
|
|
389
|
+
};
|
|
390
|
+
const describe = (node) => ({
|
|
391
|
+
ref: makeRef(node),
|
|
392
|
+
role: role(node),
|
|
393
|
+
name: name(node).slice(0, 4096),
|
|
394
|
+
disabled: !enabled(node),
|
|
395
|
+
...(typeof node.checked === "boolean" ? { checked: node.checked } : {}),
|
|
396
|
+
});
|
|
397
|
+
const setValue = (node, value) => {
|
|
398
|
+
check(typeof value === "string");
|
|
399
|
+
check(!node.readOnly, "INVALID_TARGET", "The element is readonly.");
|
|
400
|
+
if (node.isContentEditable) node.textContent = value;
|
|
401
|
+
else {
|
|
402
|
+
check(
|
|
403
|
+
["INPUT", "TEXTAREA"].includes(node.tagName),
|
|
404
|
+
"INVALID_TARGET",
|
|
405
|
+
"The target is not a text input.",
|
|
406
|
+
);
|
|
407
|
+
const proto =
|
|
408
|
+
node.tagName === "INPUT"
|
|
409
|
+
? HTMLInputElement.prototype
|
|
410
|
+
: HTMLTextAreaElement.prototype;
|
|
411
|
+
Object.getOwnPropertyDescriptor(proto, "value").set.call(node, value);
|
|
412
|
+
}
|
|
413
|
+
node.dispatchEvent(new Event("input", { bubbles: true }));
|
|
414
|
+
node.dispatchEvent(new Event("change", { bubbles: true }));
|
|
415
|
+
};
|
|
416
|
+
let value;
|
|
417
|
+
switch (input.command) {
|
|
418
|
+
// Core-only native bridge: the caller retains the actual remote Node(s), never serializes them.
|
|
419
|
+
case "nativeNodes":
|
|
420
|
+
return input.multiple ? all() : unique();
|
|
421
|
+
case "scroll": {
|
|
422
|
+
const x = input.x ?? 0,
|
|
423
|
+
y = input.y ?? 0;
|
|
424
|
+
check(
|
|
425
|
+
Number.isFinite(x) &&
|
|
426
|
+
Number.isFinite(y) &&
|
|
427
|
+
Math.abs(x) <= 1000000000 &&
|
|
428
|
+
Math.abs(y) <= 1000000000,
|
|
429
|
+
);
|
|
430
|
+
check(["auto", "smooth"].includes(input.behavior ?? "auto"));
|
|
431
|
+
const node = input.ref || input.nodeId ? unique() : globalThis;
|
|
432
|
+
node.scrollBy({ left: x, top: y, behavior: input.behavior ?? "auto" });
|
|
433
|
+
value = null;
|
|
434
|
+
break;
|
|
435
|
+
}
|
|
436
|
+
case "waitProbe": {
|
|
437
|
+
const condition = input.condition;
|
|
438
|
+
let matched = false;
|
|
439
|
+
if (["element", "selector"].includes(condition.kind)) {
|
|
440
|
+
const found = query(root, {
|
|
441
|
+
kind: "selector",
|
|
442
|
+
selector: condition.selector ?? condition.value,
|
|
443
|
+
}),
|
|
444
|
+
wanted = condition.state ?? "visible";
|
|
445
|
+
check(["attached", "detached", "visible", "hidden"].includes(wanted));
|
|
446
|
+
matched =
|
|
447
|
+
wanted === "attached"
|
|
448
|
+
? found.length > 0
|
|
449
|
+
: wanted === "detached"
|
|
450
|
+
? found.length === 0
|
|
451
|
+
: wanted === "visible"
|
|
452
|
+
? found.some(visible)
|
|
453
|
+
: !found.some(visible);
|
|
454
|
+
} else if (condition.kind === "text")
|
|
455
|
+
matched = (
|
|
456
|
+
document.body?.innerText ??
|
|
457
|
+
document.documentElement?.textContent ??
|
|
458
|
+
""
|
|
459
|
+
).includes(condition.value);
|
|
460
|
+
else if (condition.kind === "url")
|
|
461
|
+
matched = location.href === condition.value;
|
|
462
|
+
else if (condition.kind === "navigation")
|
|
463
|
+
matched = !condition.url || location.href === condition.url;
|
|
464
|
+
else if (condition.kind === "state") matched = true;
|
|
465
|
+
else failure("INVALID_ARGUMENT", "Unknown page wait condition.");
|
|
466
|
+
value = {
|
|
467
|
+
matched,
|
|
468
|
+
documentToken: state.token,
|
|
469
|
+
url: location.href,
|
|
470
|
+
readyState: document.readyState,
|
|
471
|
+
};
|
|
472
|
+
break;
|
|
473
|
+
}
|
|
474
|
+
case "readyState":
|
|
475
|
+
value = document.readyState;
|
|
476
|
+
break;
|
|
477
|
+
case "devicePixelRatio":
|
|
478
|
+
value = globalThis.devicePixelRatio;
|
|
479
|
+
break;
|
|
480
|
+
case "mediaSource": {
|
|
481
|
+
let node =
|
|
482
|
+
input.locator || input.ref || input.nodeId
|
|
483
|
+
? unique()
|
|
484
|
+
: root.elementFromPoint(input.x, input.y);
|
|
485
|
+
check(
|
|
486
|
+
node,
|
|
487
|
+
"INVALID_TARGET",
|
|
488
|
+
"There is no current media target at the requested point.",
|
|
489
|
+
);
|
|
490
|
+
node = node.closest?.("img,video,audio,source") ?? node;
|
|
491
|
+
if (!["IMG", "VIDEO", "AUDIO", "SOURCE"].includes(node.tagName))
|
|
492
|
+
node = node.querySelector?.("img,video,audio,source");
|
|
493
|
+
check(
|
|
494
|
+
node && ["IMG", "VIDEO", "AUDIO", "SOURCE"].includes(node.tagName),
|
|
495
|
+
"INVALID_TARGET",
|
|
496
|
+
"The exact target contains no readable native media source.",
|
|
497
|
+
);
|
|
498
|
+
const raw =
|
|
499
|
+
node.currentSrc ||
|
|
500
|
+
node.src ||
|
|
501
|
+
node.getAttribute("src") ||
|
|
502
|
+
node.querySelector?.("source[src]")?.src;
|
|
503
|
+
check(
|
|
504
|
+
typeof raw === "string" && raw.length > 0,
|
|
505
|
+
"FILE_UNAVAILABLE",
|
|
506
|
+
"The actual media element has no current source URL.",
|
|
507
|
+
);
|
|
508
|
+
const url = new URL(raw, node.ownerDocument.baseURI).href;
|
|
509
|
+
check(
|
|
510
|
+
/^(?:https?:|blob:|data:)/.test(url),
|
|
511
|
+
"FILE_UNAVAILABLE",
|
|
512
|
+
"The actual media source URL is not readable by this adapter.",
|
|
513
|
+
);
|
|
514
|
+
value = { url, ref: makeRef(node), mimeType: node.type || null };
|
|
515
|
+
bounded(value);
|
|
516
|
+
break;
|
|
517
|
+
}
|
|
518
|
+
case "fileInfo": {
|
|
519
|
+
const node = unique();
|
|
520
|
+
check(
|
|
521
|
+
node.tagName === "INPUT" && node.type === "file",
|
|
522
|
+
"INVALID_TARGET",
|
|
523
|
+
"The observed node is not a file input.",
|
|
524
|
+
);
|
|
525
|
+
value = { multiple: node.multiple };
|
|
526
|
+
break;
|
|
527
|
+
}
|
|
528
|
+
case "snapshot": {
|
|
529
|
+
const nodes = elements(root)
|
|
530
|
+
.filter(
|
|
531
|
+
(n) =>
|
|
532
|
+
visible(n) && (role(n) !== "generic" || n.children.length === 0),
|
|
533
|
+
)
|
|
534
|
+
.map(describe);
|
|
535
|
+
value = {
|
|
536
|
+
url: location.href,
|
|
537
|
+
title: document.title,
|
|
538
|
+
root: {
|
|
539
|
+
ref: "document",
|
|
540
|
+
role: "document",
|
|
541
|
+
name: document.title,
|
|
542
|
+
children: nodes,
|
|
543
|
+
},
|
|
544
|
+
truncated: false,
|
|
545
|
+
};
|
|
546
|
+
bounded(value);
|
|
547
|
+
break;
|
|
548
|
+
}
|
|
549
|
+
case "domSnapshot":
|
|
550
|
+
value = elements(root)
|
|
551
|
+
.filter(visible)
|
|
552
|
+
.map(
|
|
553
|
+
(n) =>
|
|
554
|
+
`${makeRef(n)} ${role(n)} ${JSON.stringify(name(n).slice(0, 2048))}`,
|
|
555
|
+
)
|
|
556
|
+
.join("\n");
|
|
557
|
+
bounded(value);
|
|
558
|
+
break;
|
|
559
|
+
case "visibleTree":
|
|
560
|
+
value = {
|
|
561
|
+
nodes: elements(root)
|
|
562
|
+
.filter(visible)
|
|
563
|
+
.map((n) => ({
|
|
564
|
+
nodeId: makeRef(n),
|
|
565
|
+
role: role(n),
|
|
566
|
+
name: name(n).slice(0, 4096),
|
|
567
|
+
rect: rect(n),
|
|
568
|
+
})),
|
|
569
|
+
truncated: false,
|
|
570
|
+
};
|
|
571
|
+
bounded(value);
|
|
572
|
+
break;
|
|
573
|
+
case "count":
|
|
574
|
+
value = all().length;
|
|
575
|
+
break;
|
|
576
|
+
case "all":
|
|
577
|
+
value = all().map((n) => ({
|
|
578
|
+
kind: "ref",
|
|
579
|
+
ref: makeRef(n),
|
|
580
|
+
pageVersion: state.version,
|
|
581
|
+
}));
|
|
582
|
+
break;
|
|
583
|
+
case "allTextContents":
|
|
584
|
+
value = all().map((n) => n.textContent || "");
|
|
585
|
+
bounded(value);
|
|
586
|
+
break;
|
|
587
|
+
case "textContent":
|
|
588
|
+
value = unique().textContent;
|
|
589
|
+
bounded(value);
|
|
590
|
+
break;
|
|
591
|
+
case "innerText":
|
|
592
|
+
value = unique().innerText;
|
|
593
|
+
bounded(value);
|
|
594
|
+
break;
|
|
595
|
+
case "isVisible": {
|
|
596
|
+
const n = all();
|
|
597
|
+
check(n.length <= 1, "AMBIGUOUS_TARGET");
|
|
598
|
+
value = n.length === 1 && visible(n[0]);
|
|
599
|
+
break;
|
|
600
|
+
}
|
|
601
|
+
case "isEnabled":
|
|
602
|
+
value = enabled(unique());
|
|
603
|
+
break;
|
|
604
|
+
case "getAttribute":
|
|
605
|
+
check(typeof input.name === "string");
|
|
606
|
+
value = unique().getAttribute(input.name);
|
|
607
|
+
bounded(value);
|
|
608
|
+
break;
|
|
609
|
+
case "resolve": {
|
|
610
|
+
const node = unique(false, true),
|
|
611
|
+
geometry = node && actionGeometry(node, true);
|
|
612
|
+
value = geometry
|
|
613
|
+
? { ...describe(node), ...geometry, tagName: node.tagName }
|
|
614
|
+
: null;
|
|
615
|
+
break;
|
|
616
|
+
}
|
|
617
|
+
case "fill":
|
|
618
|
+
setValue(unique(true), input.value ?? input.text);
|
|
619
|
+
value = null;
|
|
620
|
+
break;
|
|
621
|
+
case "focus":
|
|
622
|
+
unique(true).focus();
|
|
623
|
+
value = null;
|
|
624
|
+
break;
|
|
625
|
+
case "selectOption": {
|
|
626
|
+
const node = unique(true);
|
|
627
|
+
check(
|
|
628
|
+
node.tagName === "SELECT",
|
|
629
|
+
"INVALID_TARGET",
|
|
630
|
+
"The target is not a select element.",
|
|
631
|
+
);
|
|
632
|
+
const values = Array.isArray(input.values ?? input.value)
|
|
633
|
+
? (input.values ?? input.value)
|
|
634
|
+
: [input.values ?? input.value];
|
|
635
|
+
check(node.multiple || values.length <= 1);
|
|
636
|
+
const matches = values.map((item) =>
|
|
637
|
+
[...node.options].find((o, index) =>
|
|
638
|
+
typeof item === "string"
|
|
639
|
+
? o.value === item
|
|
640
|
+
: (item.value === undefined || o.value === item.value) &&
|
|
641
|
+
(item.label === undefined || o.label === item.label) &&
|
|
642
|
+
(item.index === undefined || index === item.index),
|
|
643
|
+
),
|
|
644
|
+
);
|
|
645
|
+
check(
|
|
646
|
+
matches.every(Boolean),
|
|
647
|
+
"INVALID_TARGET",
|
|
648
|
+
"A requested select option is missing.",
|
|
649
|
+
);
|
|
650
|
+
for (const option of node.options)
|
|
651
|
+
option.selected = matches.includes(option);
|
|
652
|
+
node.dispatchEvent(new Event("input", { bubbles: true }));
|
|
653
|
+
node.dispatchEvent(new Event("change", { bubbles: true }));
|
|
654
|
+
value = matches.map((o) => o.value);
|
|
655
|
+
break;
|
|
656
|
+
}
|
|
657
|
+
case "checked": {
|
|
658
|
+
const node = unique();
|
|
659
|
+
check(
|
|
660
|
+
typeof node.checked === "boolean",
|
|
661
|
+
"INVALID_TARGET",
|
|
662
|
+
"The target is not checkable.",
|
|
663
|
+
);
|
|
664
|
+
value = node.checked;
|
|
665
|
+
break;
|
|
666
|
+
}
|
|
667
|
+
case "waitState": {
|
|
668
|
+
const found = all();
|
|
669
|
+
value =
|
|
670
|
+
input.state === "attached"
|
|
671
|
+
? found.length > 0
|
|
672
|
+
: input.state === "detached"
|
|
673
|
+
? found.length === 0
|
|
674
|
+
: input.state === "visible"
|
|
675
|
+
? found.some(visible)
|
|
676
|
+
: !found.some(visible);
|
|
677
|
+
break;
|
|
678
|
+
}
|
|
679
|
+
case "elementInfo":
|
|
680
|
+
case "elementScreenshot": {
|
|
681
|
+
const node = root.elementFromPoint(input.x, input.y);
|
|
682
|
+
if (input.command === "elementScreenshot") {
|
|
683
|
+
check(
|
|
684
|
+
node,
|
|
685
|
+
"INVALID_TARGET",
|
|
686
|
+
"No element exists at these coordinates.",
|
|
687
|
+
);
|
|
688
|
+
const box = rect(node);
|
|
689
|
+
// Observe the element and its document scroll in the same native
|
|
690
|
+
// execution. Screenshot clips use document CSS coordinates.
|
|
691
|
+
value = {
|
|
692
|
+
...box,
|
|
693
|
+
x: box.x + globalThis.scrollX,
|
|
694
|
+
y: box.y + globalThis.scrollY,
|
|
695
|
+
};
|
|
696
|
+
break;
|
|
697
|
+
}
|
|
698
|
+
value = !node
|
|
699
|
+
? []
|
|
700
|
+
: [
|
|
701
|
+
{
|
|
702
|
+
preview: node.outerHTML.slice(0, 4096),
|
|
703
|
+
tagName: node.tagName.toLowerCase(),
|
|
704
|
+
role: role(node),
|
|
705
|
+
ariaName: name(node),
|
|
706
|
+
visibleText: (node.innerText || "").slice(0, 4096),
|
|
707
|
+
boundingBox: rect(node),
|
|
708
|
+
testId: node.getAttribute("data-testid"),
|
|
709
|
+
selector: { candidates: [], primary: null },
|
|
710
|
+
ref: makeRef(node),
|
|
711
|
+
},
|
|
712
|
+
];
|
|
713
|
+
break;
|
|
714
|
+
}
|
|
715
|
+
case "content":
|
|
716
|
+
if (input.format === "html") {
|
|
717
|
+
const copy = root.documentElement.cloneNode(true);
|
|
718
|
+
for (const node of copy.querySelectorAll(
|
|
719
|
+
'[data-nexum-visual-layer="v1"],[data-nexum-visual-icon="v1"]',
|
|
720
|
+
))
|
|
721
|
+
node.remove();
|
|
722
|
+
value = copy.outerHTML;
|
|
723
|
+
} else
|
|
724
|
+
value =
|
|
725
|
+
root.body?.innerText || root.documentElement.textContent || "";
|
|
726
|
+
bounded(value);
|
|
727
|
+
break;
|
|
728
|
+
case "readText":
|
|
729
|
+
value = await navigator.clipboard.readText();
|
|
730
|
+
bounded(value);
|
|
731
|
+
break;
|
|
732
|
+
case "writeText":
|
|
733
|
+
check(typeof input.text === "string");
|
|
734
|
+
await navigator.clipboard.writeText(input.text);
|
|
735
|
+
value = null;
|
|
736
|
+
break;
|
|
737
|
+
case "assets":
|
|
738
|
+
value = [
|
|
739
|
+
...new Set([
|
|
740
|
+
...elements(root).flatMap((n) =>
|
|
741
|
+
[n.src, n.href, n.poster].filter(
|
|
742
|
+
(v) => typeof v === "string" && /^https?:/.test(v),
|
|
743
|
+
),
|
|
744
|
+
),
|
|
745
|
+
...performance
|
|
746
|
+
.getEntriesByType("resource")
|
|
747
|
+
.map((n) => n.name)
|
|
748
|
+
.filter((v) => /^https?:/.test(v)),
|
|
749
|
+
]),
|
|
750
|
+
];
|
|
751
|
+
bounded(value);
|
|
752
|
+
break;
|
|
753
|
+
default:
|
|
754
|
+
failure(
|
|
755
|
+
"CAPABILITY_NOT_ALLOWED",
|
|
756
|
+
"This build has no implementation for that page command.",
|
|
757
|
+
);
|
|
758
|
+
}
|
|
759
|
+
return { ok: true, value };
|
|
760
|
+
} catch (cause) {
|
|
761
|
+
return {
|
|
762
|
+
ok: false,
|
|
763
|
+
error: {
|
|
764
|
+
code: typeof cause?.code === "string" ? cause.code : "INVALID_ARGUMENT",
|
|
765
|
+
message:
|
|
766
|
+
typeof cause?.code === "string"
|
|
767
|
+
? cause.message
|
|
768
|
+
: "The page command could not be completed.",
|
|
769
|
+
},
|
|
770
|
+
};
|
|
771
|
+
}
|
|
772
|
+
}
|