@nexum-hub/browser 0.0.0-stage → 0.0.1
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 +2352 -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,24 @@
|
|
|
1
|
+
import { profile, extensionVersion } from "./profile.js";
|
|
2
|
+
import { createBrowserRouter } from "./router.js";
|
|
3
|
+
import { createExtensionConnection } from "./connection.js";
|
|
4
|
+
import { createVisualController } from "./visual.js";
|
|
5
|
+
|
|
6
|
+
let connection;
|
|
7
|
+
const visual = createVisualController(chrome, {
|
|
8
|
+
emit: (event) => connection?.publish(event),
|
|
9
|
+
persist: (markers) =>
|
|
10
|
+
chrome.storage.session.set({ nexumVisualMarkers: markers }),
|
|
11
|
+
});
|
|
12
|
+
const router = createBrowserRouter(chrome, {
|
|
13
|
+
visual,
|
|
14
|
+
emit: (event) => connection?.publish(event),
|
|
15
|
+
persistOwned: (records) =>
|
|
16
|
+
chrome.storage.session.set({ nexumOwnedControls: records }),
|
|
17
|
+
});
|
|
18
|
+
connection = createExtensionConnection({
|
|
19
|
+
chrome,
|
|
20
|
+
router,
|
|
21
|
+
profile,
|
|
22
|
+
extensionVersion,
|
|
23
|
+
});
|
|
24
|
+
void connection.start().catch(() => {});
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
export class BrowserActionError extends Error {
|
|
2
|
+
constructor(
|
|
3
|
+
code,
|
|
4
|
+
message,
|
|
5
|
+
{ dispatched = false, effect = "none", retryable = false } = {},
|
|
6
|
+
) {
|
|
7
|
+
super(message);
|
|
8
|
+
this.name = "BrowserError";
|
|
9
|
+
this.code = code;
|
|
10
|
+
this.dispatched = dispatched;
|
|
11
|
+
this.effect = effect;
|
|
12
|
+
this.retryable = retryable;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
export function requireValue(
|
|
16
|
+
value,
|
|
17
|
+
code = "INVALID_ARGUMENT",
|
|
18
|
+
message = "The browser input is invalid.",
|
|
19
|
+
) {
|
|
20
|
+
if (!value) throw new BrowserActionError(code, message);
|
|
21
|
+
}
|
|
22
|
+
export function record(value, keys) {
|
|
23
|
+
requireValue(
|
|
24
|
+
value !== null && typeof value === "object" && !Array.isArray(value),
|
|
25
|
+
);
|
|
26
|
+
requireValue(
|
|
27
|
+
Object.getPrototypeOf(value) === Object.prototype ||
|
|
28
|
+
Object.getPrototypeOf(value) === null,
|
|
29
|
+
);
|
|
30
|
+
for (const key of Reflect.ownKeys(value)) {
|
|
31
|
+
const field = Object.getOwnPropertyDescriptor(value, key);
|
|
32
|
+
requireValue(
|
|
33
|
+
typeof key === "string" &&
|
|
34
|
+
(!keys || keys.includes(key)) &&
|
|
35
|
+
field.enumerable &&
|
|
36
|
+
"value" in field,
|
|
37
|
+
);
|
|
38
|
+
}
|
|
39
|
+
return value;
|
|
40
|
+
}
|
|
41
|
+
export function text(value, maximum = 8192) {
|
|
42
|
+
requireValue(
|
|
43
|
+
typeof value === "string" &&
|
|
44
|
+
value.length > 0 &&
|
|
45
|
+
new TextEncoder().encode(value).byteLength <= maximum &&
|
|
46
|
+
!value.includes("\0"),
|
|
47
|
+
);
|
|
48
|
+
return value;
|
|
49
|
+
}
|
|
50
|
+
export function nativeId(value) {
|
|
51
|
+
requireValue(
|
|
52
|
+
typeof value === "string" &&
|
|
53
|
+
/^(0|[1-9][0-9]*)$/.test(value) &&
|
|
54
|
+
Number.isSafeInteger(Number(value)),
|
|
55
|
+
"INVALID_TARGET",
|
|
56
|
+
"A target needs a real native browser identity.",
|
|
57
|
+
);
|
|
58
|
+
return Number(value);
|
|
59
|
+
}
|
|
60
|
+
export function httpUrl(value) {
|
|
61
|
+
let url;
|
|
62
|
+
try {
|
|
63
|
+
url = new URL(text(value));
|
|
64
|
+
} catch {
|
|
65
|
+
throw new BrowserActionError(
|
|
66
|
+
"INVALID_ARGUMENT",
|
|
67
|
+
"Use a valid HTTP(S) URL.",
|
|
68
|
+
);
|
|
69
|
+
}
|
|
70
|
+
requireValue(
|
|
71
|
+
url.protocol === "http:" || url.protocol === "https:",
|
|
72
|
+
"INVALID_ARGUMENT",
|
|
73
|
+
"Use a valid HTTP(S) URL.",
|
|
74
|
+
);
|
|
75
|
+
return url.href;
|
|
76
|
+
}
|
|
77
|
+
export function publicUrl(value) {
|
|
78
|
+
try {
|
|
79
|
+
const url = new URL(value);
|
|
80
|
+
url.username = "";
|
|
81
|
+
url.password = "";
|
|
82
|
+
url.search = "";
|
|
83
|
+
url.hash = "";
|
|
84
|
+
return url.href;
|
|
85
|
+
} catch {
|
|
86
|
+
return "";
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
export function publicError(cause, dispatched = false) {
|
|
90
|
+
if (cause instanceof BrowserActionError)
|
|
91
|
+
return {
|
|
92
|
+
code: cause.code,
|
|
93
|
+
message: cause.message,
|
|
94
|
+
dispatched: cause.dispatched || dispatched,
|
|
95
|
+
effect: cause.effect === "none" && dispatched ? "unknown" : cause.effect,
|
|
96
|
+
retryable: cause.retryable,
|
|
97
|
+
};
|
|
98
|
+
// Native errors can contain page contents, URLs or input; retain a bounded diagnostic category.
|
|
99
|
+
return {
|
|
100
|
+
code: "PERMISSION_DENIED",
|
|
101
|
+
message: "The browser rejected the native operation.",
|
|
102
|
+
dispatched,
|
|
103
|
+
effect: dispatched ? "unknown" : "none",
|
|
104
|
+
retryable: false,
|
|
105
|
+
};
|
|
106
|
+
}
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
import { BrowserActionError, requireValue, text } from "./errors.js";
|
|
2
|
+
import { pageCommand } from "./page.js";
|
|
3
|
+
|
|
4
|
+
/** Transfer only native DOM identities into MAIN world; user code never closes over the resolver. */
|
|
5
|
+
export async function evaluateLocated(ctx, args, multiple, scope) {
|
|
6
|
+
text(args.expression, 65536);
|
|
7
|
+
const encoded = JSON.stringify(args.arg ?? null);
|
|
8
|
+
requireValue(new TextEncoder().encode(encoded).byteLength <= 1024 * 1024);
|
|
9
|
+
const { native, frameId, executionContextId, input } = scope;
|
|
10
|
+
const objects = new Set();
|
|
11
|
+
const retain = (object) => {
|
|
12
|
+
if (typeof object?.objectId === "string") objects.add(object.objectId);
|
|
13
|
+
return object;
|
|
14
|
+
};
|
|
15
|
+
const call = async (method, params) => {
|
|
16
|
+
const result = await native(method, params);
|
|
17
|
+
retain(result.result);
|
|
18
|
+
retain(result.object);
|
|
19
|
+
if (method === "Runtime.getProperties")
|
|
20
|
+
for (const property of [
|
|
21
|
+
...(result.result ?? []),
|
|
22
|
+
...(result.internalProperties ?? []),
|
|
23
|
+
...(result.privateProperties ?? []),
|
|
24
|
+
])
|
|
25
|
+
for (const key of ["value", "get", "set"]) retain(property[key]);
|
|
26
|
+
requireValue(
|
|
27
|
+
!result.exceptionDetails,
|
|
28
|
+
"INVALID_ARGUMENT",
|
|
29
|
+
"The locator page expression rejected.",
|
|
30
|
+
);
|
|
31
|
+
return result;
|
|
32
|
+
};
|
|
33
|
+
try {
|
|
34
|
+
const observed = await call("Runtime.callFunctionOn", {
|
|
35
|
+
executionContextId,
|
|
36
|
+
functionDeclaration: pageCommand.toString(),
|
|
37
|
+
arguments: [{ value: { ...input, command: "nativeNodes", multiple } }],
|
|
38
|
+
awaitPromise: true,
|
|
39
|
+
returnByValue: false,
|
|
40
|
+
});
|
|
41
|
+
const root = observed.result;
|
|
42
|
+
if (root?.subtype !== (multiple ? "array" : "node")) {
|
|
43
|
+
const error = root?.objectId
|
|
44
|
+
? await call("Runtime.callFunctionOn", {
|
|
45
|
+
objectId: root.objectId,
|
|
46
|
+
functionDeclaration: "function(){return this.error}",
|
|
47
|
+
returnByValue: true,
|
|
48
|
+
})
|
|
49
|
+
: null;
|
|
50
|
+
throw new BrowserActionError(
|
|
51
|
+
error?.result?.value?.code ?? "INVALID_TARGET",
|
|
52
|
+
error?.result?.value?.message ??
|
|
53
|
+
"The locator no longer resolves to the required native nodes.",
|
|
54
|
+
);
|
|
55
|
+
}
|
|
56
|
+
let nodes = [root];
|
|
57
|
+
if (multiple) {
|
|
58
|
+
const properties = await call("Runtime.getProperties", {
|
|
59
|
+
objectId: root.objectId,
|
|
60
|
+
ownProperties: true,
|
|
61
|
+
accessorPropertiesOnly: false,
|
|
62
|
+
});
|
|
63
|
+
const length = properties.result.find(
|
|
64
|
+
(property) => property.name === "length",
|
|
65
|
+
)?.value?.value;
|
|
66
|
+
requireValue(
|
|
67
|
+
Number.isInteger(length) && length >= 0 && length <= 10000,
|
|
68
|
+
"OUTPUT_TOO_LARGE",
|
|
69
|
+
"The locator node collection exceeds its native bridge budget.",
|
|
70
|
+
);
|
|
71
|
+
nodes = properties.result
|
|
72
|
+
.filter((property) => /^(?:0|[1-9]\d*)$/.test(property.name))
|
|
73
|
+
.sort((a, b) => Number(a.name) - Number(b.name))
|
|
74
|
+
.map((property) => retain(property.value));
|
|
75
|
+
requireValue(
|
|
76
|
+
nodes.length === length &&
|
|
77
|
+
nodes.every((node) => node?.subtype === "node" && node.objectId),
|
|
78
|
+
"STALE_REF",
|
|
79
|
+
"The original native node collection is no longer available.",
|
|
80
|
+
);
|
|
81
|
+
}
|
|
82
|
+
const main = await ctx.router.frameContext(ctx, frameId, "main");
|
|
83
|
+
const selected = [];
|
|
84
|
+
for (const node of nodes) {
|
|
85
|
+
const description = await call("DOM.describeNode", {
|
|
86
|
+
objectId: node.objectId,
|
|
87
|
+
});
|
|
88
|
+
requireValue(
|
|
89
|
+
Number.isInteger(description.node?.backendNodeId),
|
|
90
|
+
"STALE_REF",
|
|
91
|
+
"The original locator node was detached.",
|
|
92
|
+
);
|
|
93
|
+
const resolved = await call("DOM.resolveNode", {
|
|
94
|
+
backendNodeId: description.node.backendNodeId,
|
|
95
|
+
executionContextId: main,
|
|
96
|
+
});
|
|
97
|
+
requireValue(
|
|
98
|
+
resolved.object?.subtype === "node" && resolved.object.objectId,
|
|
99
|
+
"STALE_REF",
|
|
100
|
+
"The original locator node has no live page-world identity.",
|
|
101
|
+
);
|
|
102
|
+
selected.push(resolved.object.objectId);
|
|
103
|
+
}
|
|
104
|
+
let first = selected[0];
|
|
105
|
+
if (multiple) {
|
|
106
|
+
const array = await call("Runtime.callFunctionOn", {
|
|
107
|
+
executionContextId: main,
|
|
108
|
+
functionDeclaration: "function(...nodes){return nodes}",
|
|
109
|
+
arguments: selected.map((objectId) => ({ objectId })),
|
|
110
|
+
returnByValue: false,
|
|
111
|
+
});
|
|
112
|
+
first = array.result?.objectId;
|
|
113
|
+
requireValue(
|
|
114
|
+
first,
|
|
115
|
+
"STALE_REF",
|
|
116
|
+
"The native page-world node collection was not created.",
|
|
117
|
+
);
|
|
118
|
+
}
|
|
119
|
+
const fn = await call("Runtime.evaluate", {
|
|
120
|
+
expression: `(${args.expression})`,
|
|
121
|
+
contextId: main,
|
|
122
|
+
returnByValue: false,
|
|
123
|
+
});
|
|
124
|
+
requireValue(
|
|
125
|
+
fn.result?.type === "function" && fn.result.objectId,
|
|
126
|
+
"INVALID_ARGUMENT",
|
|
127
|
+
"Locator evaluation requires a page function.",
|
|
128
|
+
);
|
|
129
|
+
const result = await call("Runtime.callFunctionOn", {
|
|
130
|
+
objectId: fn.result.objectId,
|
|
131
|
+
functionDeclaration: "function(nodes,arg){return this(nodes,arg)}",
|
|
132
|
+
arguments: [
|
|
133
|
+
{ objectId: first },
|
|
134
|
+
...(args.arg === undefined ? [{}] : [{ value: JSON.parse(encoded) }]),
|
|
135
|
+
],
|
|
136
|
+
awaitPromise: true,
|
|
137
|
+
returnByValue: true,
|
|
138
|
+
userGesture: true,
|
|
139
|
+
});
|
|
140
|
+
const value = result.result?.value ?? null;
|
|
141
|
+
requireValue(
|
|
142
|
+
new TextEncoder().encode(JSON.stringify(value)).byteLength <= 1024 * 1024,
|
|
143
|
+
"OUTPUT_TOO_LARGE",
|
|
144
|
+
"The page expression exceeds its inline result budget.",
|
|
145
|
+
);
|
|
146
|
+
return value;
|
|
147
|
+
} finally {
|
|
148
|
+
// Cancellation cannot skip native RemoteObject release, and one failed release cannot skip the rest.
|
|
149
|
+
const released = await Promise.allSettled(
|
|
150
|
+
[...objects].map((objectId) =>
|
|
151
|
+
ctx.router.releaseFrameObject(ctx, frameId, objectId),
|
|
152
|
+
),
|
|
153
|
+
);
|
|
154
|
+
requireValue(
|
|
155
|
+
released.every((result) => result.status === "fulfilled"),
|
|
156
|
+
"CDP_DISCONNECTED",
|
|
157
|
+
"Some locator page objects have not confirmed native release.",
|
|
158
|
+
);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
import { BrowserActionError, requireValue, text } from "./errors.js";
|
|
2
|
+
import { pageCommand } from "./page.js";
|
|
3
|
+
|
|
4
|
+
/** Runs in a native isolated world. Returns the actual owner node, never a URL-derived guess. */
|
|
5
|
+
export function findFrameOwner(selector) {
|
|
6
|
+
const nodes = document.querySelectorAll(selector);
|
|
7
|
+
if (nodes.length !== 1 || !["IFRAME", "FRAME"].includes(nodes[0].tagName))
|
|
8
|
+
throw new Error(
|
|
9
|
+
"The frame selector must resolve to exactly one current frame owner.",
|
|
10
|
+
);
|
|
11
|
+
return nodes[0];
|
|
12
|
+
}
|
|
13
|
+
export function frameOwnerGeometry() {
|
|
14
|
+
if (!this.isConnected) throw new Error("The frame owner was detached.");
|
|
15
|
+
for (let node = this; node; node = node.parentElement) {
|
|
16
|
+
const transform = getComputedStyle(node).transform;
|
|
17
|
+
if (transform !== "none") {
|
|
18
|
+
const matrix = new DOMMatrixReadOnly(transform);
|
|
19
|
+
if (
|
|
20
|
+
!matrix.is2D ||
|
|
21
|
+
Math.abs(matrix.b) > 0.000001 ||
|
|
22
|
+
Math.abs(matrix.c) > 0.000001 ||
|
|
23
|
+
matrix.a <= 0 ||
|
|
24
|
+
matrix.d <= 0
|
|
25
|
+
)
|
|
26
|
+
throw new Error(
|
|
27
|
+
"The frame transform is not an axis-aligned positive scale.",
|
|
28
|
+
);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
const rect = this.getBoundingClientRect(),
|
|
32
|
+
style = getComputedStyle(this);
|
|
33
|
+
const scaleX = rect.width / this.offsetWidth,
|
|
34
|
+
scaleY = rect.height / this.offsetHeight;
|
|
35
|
+
if (![scaleX, scaleY].every(Number.isFinite) || scaleX <= 0 || scaleY <= 0)
|
|
36
|
+
throw new Error("The frame has no native visible content area.");
|
|
37
|
+
return {
|
|
38
|
+
x:
|
|
39
|
+
rect.x +
|
|
40
|
+
(this.clientLeft + parseFloat(style.paddingLeft || "0")) * scaleX,
|
|
41
|
+
y: rect.y + (this.clientTop + parseFloat(style.paddingTop || "0")) * scaleY,
|
|
42
|
+
scaleX,
|
|
43
|
+
scaleY,
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
export function frameHitTest(input) {
|
|
47
|
+
const node = this.ownerDocument.elementFromPoint(input.x, input.y);
|
|
48
|
+
return this.isConnected && (node === this || this.contains(node));
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** Let visible frame scrolls reach native input routing before reading the map. */
|
|
52
|
+
export function frameOwnerReadyForInput() {
|
|
53
|
+
if (!this.isConnected) return false;
|
|
54
|
+
// Inactive documents do not run animation frames. Preserve their existing
|
|
55
|
+
// native layout route instead of turning background input into a paint wait.
|
|
56
|
+
if (document.visibilityState === "hidden") return true;
|
|
57
|
+
return new Promise((resolve) => {
|
|
58
|
+
let frame, timer;
|
|
59
|
+
const finish = (ready) => {
|
|
60
|
+
cancelAnimationFrame(frame);
|
|
61
|
+
clearTimeout(timer);
|
|
62
|
+
resolve(ready && this.isConnected);
|
|
63
|
+
};
|
|
64
|
+
timer = setTimeout(() => finish(false), 250);
|
|
65
|
+
frame = requestAnimationFrame(() => {
|
|
66
|
+
frame = requestAnimationFrame(() => finish(true));
|
|
67
|
+
});
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function mappedPoint(point, owners) {
|
|
72
|
+
let result = { x: point.x, y: point.y };
|
|
73
|
+
for (const owner of [...owners].reverse())
|
|
74
|
+
result = {
|
|
75
|
+
x: owner.geometry.x + result.x * owner.geometry.scaleX,
|
|
76
|
+
y: owner.geometry.y + result.y * owner.geometry.scaleY,
|
|
77
|
+
};
|
|
78
|
+
return result;
|
|
79
|
+
}
|
|
80
|
+
function mappedRect(rect, owners) {
|
|
81
|
+
const start = mappedPoint(rect, owners),
|
|
82
|
+
end = mappedPoint(
|
|
83
|
+
{ x: rect.x + rect.width, y: rect.y + rect.height },
|
|
84
|
+
owners,
|
|
85
|
+
);
|
|
86
|
+
return { ...start, width: end.x - start.x, height: end.y - start.y };
|
|
87
|
+
}
|
|
88
|
+
export async function framePage(ctx, payload, selectors, execute) {
|
|
89
|
+
requireValue(
|
|
90
|
+
Array.isArray(selectors) && selectors.length > 0 && selectors.length <= 8,
|
|
91
|
+
);
|
|
92
|
+
selectors.forEach((selector) => text(selector, 8192));
|
|
93
|
+
await ctx.router.enableFrameTargets(ctx);
|
|
94
|
+
const tree = await ctx.router.send(ctx, "Page.getFrameTree", {});
|
|
95
|
+
let frameId = tree.frameTree?.frame?.id;
|
|
96
|
+
text(frameId, 512);
|
|
97
|
+
const owners = [];
|
|
98
|
+
const native = async (method, params, id = frameId) =>
|
|
99
|
+
ctx.router.frameSend(ctx, id, method, params);
|
|
100
|
+
const world = async () => {
|
|
101
|
+
const result = await native("Page.createIsolatedWorld", {
|
|
102
|
+
frameId,
|
|
103
|
+
worldName: "nexum.browser.frame.v1",
|
|
104
|
+
grantUniveralAccess: false,
|
|
105
|
+
});
|
|
106
|
+
requireValue(
|
|
107
|
+
Number.isInteger(result.executionContextId),
|
|
108
|
+
"STALE_REF",
|
|
109
|
+
"The browser did not create the actual frame world.",
|
|
110
|
+
);
|
|
111
|
+
return result.executionContextId;
|
|
112
|
+
};
|
|
113
|
+
try {
|
|
114
|
+
for (const selector of selectors) {
|
|
115
|
+
ctx.signal?.throwIfAborted();
|
|
116
|
+
const result = await native("Runtime.callFunctionOn", {
|
|
117
|
+
executionContextId: await world(),
|
|
118
|
+
functionDeclaration: findFrameOwner.toString(),
|
|
119
|
+
arguments: [{ value: selector }],
|
|
120
|
+
returnByValue: false,
|
|
121
|
+
});
|
|
122
|
+
requireValue(
|
|
123
|
+
!result.exceptionDetails &&
|
|
124
|
+
result.result?.subtype === "node" &&
|
|
125
|
+
typeof result.result.objectId === "string",
|
|
126
|
+
"INVALID_TARGET",
|
|
127
|
+
"The frame selector must resolve to one current native frame owner.",
|
|
128
|
+
);
|
|
129
|
+
const owner = { frameId, objectId: result.result.objectId };
|
|
130
|
+
owners.push(owner);
|
|
131
|
+
const node = await native("DOM.describeNode", {
|
|
132
|
+
objectId: owner.objectId,
|
|
133
|
+
});
|
|
134
|
+
requireValue(
|
|
135
|
+
typeof node.node?.frameId === "string" && node.node.frameId !== frameId,
|
|
136
|
+
"STALE_REF",
|
|
137
|
+
"The observed native node has no live child-frame identity.",
|
|
138
|
+
);
|
|
139
|
+
const geometry = await native("Runtime.callFunctionOn", {
|
|
140
|
+
objectId: owner.objectId,
|
|
141
|
+
functionDeclaration: frameOwnerGeometry.toString(),
|
|
142
|
+
returnByValue: true,
|
|
143
|
+
});
|
|
144
|
+
requireValue(
|
|
145
|
+
!geometry.exceptionDetails && geometry.result?.value,
|
|
146
|
+
"INVALID_TARGET",
|
|
147
|
+
"The current frame transform has no supported native coordinate mapping.",
|
|
148
|
+
);
|
|
149
|
+
owner.geometry = geometry.result.value;
|
|
150
|
+
ctx.router.noteFrameParent(ctx, node.node.frameId, frameId);
|
|
151
|
+
frameId = node.node.frameId;
|
|
152
|
+
}
|
|
153
|
+
const input = {
|
|
154
|
+
...payload,
|
|
155
|
+
framePath: [],
|
|
156
|
+
...(payload.locator
|
|
157
|
+
? { locator: { ...payload.locator, framePath: [] } }
|
|
158
|
+
: {}),
|
|
159
|
+
pageVersion: ctx.router.version(ctx.target.tabId),
|
|
160
|
+
leaseId: ctx.router.referenceLease(ctx),
|
|
161
|
+
};
|
|
162
|
+
const executionContextId = await world();
|
|
163
|
+
if (execute)
|
|
164
|
+
return await execute({ input, native, frameId, executionContextId });
|
|
165
|
+
const response = await native("Runtime.callFunctionOn", {
|
|
166
|
+
executionContextId,
|
|
167
|
+
functionDeclaration: pageCommand.toString(),
|
|
168
|
+
arguments: [{ value: input }],
|
|
169
|
+
awaitPromise: true,
|
|
170
|
+
returnByValue: true,
|
|
171
|
+
userGesture: true,
|
|
172
|
+
});
|
|
173
|
+
requireValue(
|
|
174
|
+
!response.exceptionDetails && response.result?.value,
|
|
175
|
+
"STALE_REF",
|
|
176
|
+
"The actual frame execution context is no longer available.",
|
|
177
|
+
);
|
|
178
|
+
const result = response.result.value;
|
|
179
|
+
if (!result.ok)
|
|
180
|
+
throw new BrowserActionError(result.error.code, result.error.message);
|
|
181
|
+
if (input.command === "resolve" && result.value?.rect) {
|
|
182
|
+
// scrollIntoView in the resolved descendant can scroll every native ancestor.
|
|
183
|
+
// Coordinates read during traversal are no longer an observation of this input target.
|
|
184
|
+
for (const owner of owners) {
|
|
185
|
+
const ready = await ctx.router.frameSend(
|
|
186
|
+
ctx,
|
|
187
|
+
owner.frameId,
|
|
188
|
+
"Runtime.callFunctionOn",
|
|
189
|
+
{
|
|
190
|
+
objectId: owner.objectId,
|
|
191
|
+
functionDeclaration: frameOwnerReadyForInput.toString(),
|
|
192
|
+
awaitPromise: true,
|
|
193
|
+
returnByValue: true,
|
|
194
|
+
},
|
|
195
|
+
);
|
|
196
|
+
ctx.signal?.throwIfAborted();
|
|
197
|
+
requireValue(
|
|
198
|
+
!ready.exceptionDetails && typeof ready.result?.value === "boolean",
|
|
199
|
+
"STALE_REF",
|
|
200
|
+
"The ancestor frame's current rendering state is unavailable.",
|
|
201
|
+
);
|
|
202
|
+
if (!ready.result.value) return null;
|
|
203
|
+
const geometry = await ctx.router.frameSend(
|
|
204
|
+
ctx,
|
|
205
|
+
owner.frameId,
|
|
206
|
+
"Runtime.callFunctionOn",
|
|
207
|
+
{
|
|
208
|
+
objectId: owner.objectId,
|
|
209
|
+
functionDeclaration: frameOwnerGeometry.toString(),
|
|
210
|
+
returnByValue: true,
|
|
211
|
+
},
|
|
212
|
+
);
|
|
213
|
+
requireValue(
|
|
214
|
+
!geometry.exceptionDetails && geometry.result?.value,
|
|
215
|
+
"STALE_REF",
|
|
216
|
+
"An ancestor frame moved or detached before native input.",
|
|
217
|
+
);
|
|
218
|
+
owner.geometry = geometry.result.value;
|
|
219
|
+
}
|
|
220
|
+
let point = result.value.point;
|
|
221
|
+
for (const owner of [...owners].reverse()) {
|
|
222
|
+
point = {
|
|
223
|
+
x: owner.geometry.x + point.x * owner.geometry.scaleX,
|
|
224
|
+
y: owner.geometry.y + point.y * owner.geometry.scaleY,
|
|
225
|
+
};
|
|
226
|
+
const hit = await ctx.router.frameSend(
|
|
227
|
+
ctx,
|
|
228
|
+
owner.frameId,
|
|
229
|
+
"Runtime.callFunctionOn",
|
|
230
|
+
{
|
|
231
|
+
objectId: owner.objectId,
|
|
232
|
+
functionDeclaration: frameHitTest.toString(),
|
|
233
|
+
arguments: [{ value: point }],
|
|
234
|
+
returnByValue: true,
|
|
235
|
+
},
|
|
236
|
+
);
|
|
237
|
+
requireValue(
|
|
238
|
+
!hit.exceptionDetails && typeof hit.result?.value === "boolean",
|
|
239
|
+
"STALE_REF",
|
|
240
|
+
"The ancestor frame's native hit test is no longer available.",
|
|
241
|
+
);
|
|
242
|
+
if (!hit.result.value) return null;
|
|
243
|
+
}
|
|
244
|
+
result.value.point = point;
|
|
245
|
+
result.value.rect = mappedRect(result.value.rect, owners);
|
|
246
|
+
}
|
|
247
|
+
if (input.command === "elementInfo" && Array.isArray(result.value))
|
|
248
|
+
for (const entry of result.value)
|
|
249
|
+
if (entry.boundingBox)
|
|
250
|
+
entry.boundingBox = mappedRect(entry.boundingBox, owners);
|
|
251
|
+
return result.value;
|
|
252
|
+
} finally {
|
|
253
|
+
for (const owner of owners.reverse())
|
|
254
|
+
await ctx.router.releaseFrameObject(ctx, owner.frameId, owner.objectId);
|
|
255
|
+
}
|
|
256
|
+
}
|