@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,976 @@
|
|
|
1
|
+
import { requireValue } from "./errors.js";
|
|
2
|
+
|
|
3
|
+
export const visualStyles = `[data-nexum-visual-layer="v1"]{position:fixed!important;left:0!important;top:0!important;width:24px!important;height:24px!important;z-index:2147483647!important;pointer-events:none!important;user-select:none!important;filter:drop-shadow(0 0 6px #3b82f6)!important;opacity:1;transition:opacity 120ms linear!important}[data-nexum-visual-layer="v1"][data-nexum-visual-state="thinking"]{animation:nexum-browser-thinking 1800ms ease-in-out infinite}@keyframes nexum-browser-thinking{0%,100%{opacity:0.85}50%{opacity:1}} [data-nexum-visual-layer="v1"] *{pointer-events:none!important}@media print{[data-nexum-visual-layer="v1"]{display:none!important}}@media(prefers-reduced-motion:reduce){[data-nexum-visual-layer="v1"]{transition:none!important;animation:none!important}}`;
|
|
4
|
+
|
|
5
|
+
/** Self-contained native isolated-world function; no module closure crosses executeScript. */
|
|
6
|
+
export async function visualPage(input) {
|
|
7
|
+
const slot = Symbol.for("nexum.browser.visual.v1");
|
|
8
|
+
const now = Date.now();
|
|
9
|
+
const state = (globalThis[slot] ??= { current: null, closed: new Map() });
|
|
10
|
+
// An updated worker can encounter tombstones left by the previous build.
|
|
11
|
+
// Preserve them for the maximum native dispatch window, then release them.
|
|
12
|
+
if (state.closed instanceof Set)
|
|
13
|
+
state.closed = new Map(
|
|
14
|
+
[...state.closed].map((token) => [token, now + 250]),
|
|
15
|
+
);
|
|
16
|
+
for (const [closedToken, deadline] of state.closed)
|
|
17
|
+
if (now >= deadline) state.closed.delete(closedToken);
|
|
18
|
+
const token = input.token,
|
|
19
|
+
inputDeadline = Number.isFinite(input.deadline) ? input.deadline : 0;
|
|
20
|
+
if (input.command === "release" || input.command === "clear") {
|
|
21
|
+
if (input.command === "release")
|
|
22
|
+
state.closed.set(
|
|
23
|
+
token,
|
|
24
|
+
Math.max(
|
|
25
|
+
now + 250,
|
|
26
|
+
inputDeadline,
|
|
27
|
+
state.closed.get(token) ?? 0,
|
|
28
|
+
state.current?.token === token ? (state.current.maxDeadline ?? 0) : 0,
|
|
29
|
+
),
|
|
30
|
+
);
|
|
31
|
+
if (state.current?.token === token) {
|
|
32
|
+
const current = state.current;
|
|
33
|
+
current.stopped = true;
|
|
34
|
+
current.layer?.remove();
|
|
35
|
+
current.icon?.remove();
|
|
36
|
+
state.current = null;
|
|
37
|
+
}
|
|
38
|
+
return { ok: true };
|
|
39
|
+
}
|
|
40
|
+
if (state.closed.has(token) || Date.now() >= input.deadline)
|
|
41
|
+
return { ok: false, reason: "VISUAL_TARGET_RELEASED" };
|
|
42
|
+
if (state.current?.token !== token) {
|
|
43
|
+
if (state.current) {
|
|
44
|
+
state.current.stopped = true;
|
|
45
|
+
state.current.layer?.remove();
|
|
46
|
+
state.current.icon?.remove();
|
|
47
|
+
}
|
|
48
|
+
state.current = {
|
|
49
|
+
token,
|
|
50
|
+
sequence: -1,
|
|
51
|
+
x: 0,
|
|
52
|
+
y: 0,
|
|
53
|
+
stopped: false,
|
|
54
|
+
maxDeadline: inputDeadline,
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
const current = state.current;
|
|
58
|
+
current.maxDeadline = Math.max(current.maxDeadline ?? 0, inputDeadline);
|
|
59
|
+
const valid = () =>
|
|
60
|
+
state.current === current &&
|
|
61
|
+
!current.stopped &&
|
|
62
|
+
!state.closed.has(token) &&
|
|
63
|
+
Date.now() < input.deadline;
|
|
64
|
+
const icon = async (mode) => {
|
|
65
|
+
if (!current.original)
|
|
66
|
+
current.original = (async () => {
|
|
67
|
+
const canvas = document.createElement("canvas");
|
|
68
|
+
canvas.width = 32;
|
|
69
|
+
canvas.height = 32;
|
|
70
|
+
const ctx = canvas.getContext("2d");
|
|
71
|
+
if (!ctx) return null;
|
|
72
|
+
const original = [
|
|
73
|
+
...document.querySelectorAll('link[rel~="icon"]'),
|
|
74
|
+
].find((link) => !link.hasAttribute("data-nexum-visual-icon"));
|
|
75
|
+
let image;
|
|
76
|
+
if (original?.href) {
|
|
77
|
+
const stop = new AbortController(),
|
|
78
|
+
timer = setTimeout(() => stop.abort(), 100);
|
|
79
|
+
let reader;
|
|
80
|
+
try {
|
|
81
|
+
const response = await fetch(original.href, {
|
|
82
|
+
credentials: "omit",
|
|
83
|
+
mode: "cors",
|
|
84
|
+
redirect: "error",
|
|
85
|
+
referrerPolicy: "no-referrer",
|
|
86
|
+
signal: stop.signal,
|
|
87
|
+
});
|
|
88
|
+
if (!response.ok || !response.body) throw Error("icon unavailable");
|
|
89
|
+
reader = response.body.getReader();
|
|
90
|
+
const chunks = [];
|
|
91
|
+
let count = 0;
|
|
92
|
+
while (true) {
|
|
93
|
+
const part = await reader.read();
|
|
94
|
+
if (part.done) break;
|
|
95
|
+
count += part.value.byteLength;
|
|
96
|
+
if (count > 65536) throw Error("icon budget");
|
|
97
|
+
chunks.push(part.value);
|
|
98
|
+
}
|
|
99
|
+
image = await createImageBitmap(
|
|
100
|
+
new Blob(chunks, {
|
|
101
|
+
type: response.headers.get("content-type") || "image/png",
|
|
102
|
+
}),
|
|
103
|
+
{ resizeWidth: 32, resizeHeight: 32 },
|
|
104
|
+
);
|
|
105
|
+
if (image.width > 4096 || image.height > 4096)
|
|
106
|
+
throw Error("icon dimensions");
|
|
107
|
+
ctx.drawImage(image, 0, 0, 32, 32);
|
|
108
|
+
} catch {
|
|
109
|
+
ctx.fillStyle = "#e2e8f0";
|
|
110
|
+
ctx.beginPath();
|
|
111
|
+
ctx.arc(16, 16, 13, 0, Math.PI * 2);
|
|
112
|
+
ctx.fill();
|
|
113
|
+
} finally {
|
|
114
|
+
clearTimeout(timer);
|
|
115
|
+
image?.close?.();
|
|
116
|
+
if (reader) {
|
|
117
|
+
try {
|
|
118
|
+
await reader.cancel();
|
|
119
|
+
} catch {}
|
|
120
|
+
reader.releaseLock();
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
} else {
|
|
124
|
+
ctx.fillStyle = "#e2e8f0";
|
|
125
|
+
ctx.beginPath();
|
|
126
|
+
ctx.arc(16, 16, 13, 0, Math.PI * 2);
|
|
127
|
+
ctx.fill();
|
|
128
|
+
}
|
|
129
|
+
return canvas;
|
|
130
|
+
})();
|
|
131
|
+
const original = await current.original;
|
|
132
|
+
if (!original || !valid()) return false;
|
|
133
|
+
const canvas = document.createElement("canvas");
|
|
134
|
+
canvas.width = 32;
|
|
135
|
+
canvas.height = 32;
|
|
136
|
+
const ctx = canvas.getContext("2d");
|
|
137
|
+
if (!ctx) return false;
|
|
138
|
+
ctx.globalAlpha = mode === "active" ? 0.45 : 1;
|
|
139
|
+
ctx.drawImage(original, 0, 0, 32, 32);
|
|
140
|
+
ctx.globalAlpha = 1;
|
|
141
|
+
if (mode === "active") {
|
|
142
|
+
ctx.fillStyle = "#2563eb";
|
|
143
|
+
ctx.strokeStyle = "#fff";
|
|
144
|
+
ctx.lineWidth = 1.5;
|
|
145
|
+
ctx.beginPath();
|
|
146
|
+
ctx.moveTo(9, 5);
|
|
147
|
+
ctx.lineTo(25, 18);
|
|
148
|
+
ctx.lineTo(18, 19);
|
|
149
|
+
ctx.lineTo(21, 27);
|
|
150
|
+
ctx.lineTo(16, 29);
|
|
151
|
+
ctx.lineTo(13, 21);
|
|
152
|
+
ctx.lineTo(8, 26);
|
|
153
|
+
ctx.closePath();
|
|
154
|
+
ctx.fill();
|
|
155
|
+
ctx.stroke();
|
|
156
|
+
} else {
|
|
157
|
+
ctx.fillStyle = "#2563eb";
|
|
158
|
+
ctx.strokeStyle = "#fff";
|
|
159
|
+
ctx.lineWidth = 2;
|
|
160
|
+
ctx.beginPath();
|
|
161
|
+
ctx.arc(25, 25, 6, 0, Math.PI * 2);
|
|
162
|
+
ctx.fill();
|
|
163
|
+
ctx.stroke();
|
|
164
|
+
}
|
|
165
|
+
if (!current.icon) {
|
|
166
|
+
current.icon = document.createElement("link");
|
|
167
|
+
current.icon.rel = "icon";
|
|
168
|
+
current.icon.setAttribute("data-nexum-visual-icon", "v1");
|
|
169
|
+
(document.head || document.documentElement).append(current.icon);
|
|
170
|
+
}
|
|
171
|
+
current.icon.type = "image/png";
|
|
172
|
+
current.icon.href = canvas.toDataURL("image/png");
|
|
173
|
+
return true;
|
|
174
|
+
};
|
|
175
|
+
if (input.command === "active" || input.command === "result") {
|
|
176
|
+
if (input.command === "result") {
|
|
177
|
+
current.layer?.remove();
|
|
178
|
+
current.layer = null;
|
|
179
|
+
}
|
|
180
|
+
return { ok: await icon(input.command) };
|
|
181
|
+
}
|
|
182
|
+
if (input.command !== "move" || !valid())
|
|
183
|
+
return { ok: false, reason: "VISUAL_UNAVAILABLE" };
|
|
184
|
+
if (input.moveSequence <= current.sequence) return { ok: true, stale: true };
|
|
185
|
+
current.sequence = input.moveSequence;
|
|
186
|
+
const sequence = current.sequence;
|
|
187
|
+
if (!current.layer) {
|
|
188
|
+
const layer = document.createElement("div");
|
|
189
|
+
layer.setAttribute("data-nexum-visual-layer", "v1");
|
|
190
|
+
layer.setAttribute("aria-hidden", "true");
|
|
191
|
+
layer.setAttribute("role", "presentation");
|
|
192
|
+
layer.style.pointerEvents = "none";
|
|
193
|
+
layer.style.opacity = "0";
|
|
194
|
+
layer.innerHTML =
|
|
195
|
+
'<svg width="24" height="24" viewBox="0 0 24 24" aria-hidden="true"><path d="M3 2L21 14L13.5 15.5L17 22L12.5 24L9.5 17L3 22Z" fill="#2563eb" stroke="white" stroke-width="1.4"/></svg>';
|
|
196
|
+
document.documentElement.append(layer);
|
|
197
|
+
layer.getBoundingClientRect?.();
|
|
198
|
+
layer.style.opacity = "1";
|
|
199
|
+
current.layer = layer;
|
|
200
|
+
}
|
|
201
|
+
const layer = current.layer;
|
|
202
|
+
layer.setAttribute("data-nexum-visual-state", "moving");
|
|
203
|
+
const startX = current.x,
|
|
204
|
+
startY = current.y,
|
|
205
|
+
dx = input.point.x - startX,
|
|
206
|
+
dy = input.point.y - startY,
|
|
207
|
+
distance = Math.hypot(dx, dy);
|
|
208
|
+
const duration =
|
|
209
|
+
globalThis.matchMedia?.("(prefers-reduced-motion: reduce)").matches ||
|
|
210
|
+
document.visibilityState === "hidden" ||
|
|
211
|
+
distance < 80
|
|
212
|
+
? 0
|
|
213
|
+
: Math.min(180, Math.max(0, input.deadline - Date.now() - 8));
|
|
214
|
+
const move = (x, y) => {
|
|
215
|
+
current.x = x;
|
|
216
|
+
current.y = y;
|
|
217
|
+
layer.style.transform = `translate3d(${x}px,${y}px,0)`;
|
|
218
|
+
};
|
|
219
|
+
if (!duration) {
|
|
220
|
+
move(input.point.x, input.point.y);
|
|
221
|
+
layer.setAttribute("data-nexum-visual-state", "thinking");
|
|
222
|
+
return { ok: true, arrived: true };
|
|
223
|
+
}
|
|
224
|
+
const started = performance.now();
|
|
225
|
+
return await new Promise((resolve) => {
|
|
226
|
+
let completed = false,
|
|
227
|
+
frame,
|
|
228
|
+
timer;
|
|
229
|
+
const finish = (value) => {
|
|
230
|
+
if (completed) return;
|
|
231
|
+
completed = true;
|
|
232
|
+
cancelAnimationFrame(frame);
|
|
233
|
+
clearTimeout(timer);
|
|
234
|
+
resolve(value);
|
|
235
|
+
};
|
|
236
|
+
const step = () => {
|
|
237
|
+
if (completed) return;
|
|
238
|
+
if (!valid() || current.sequence !== sequence) {
|
|
239
|
+
finish({ ok: false, reason: "VISUAL_MOVE_SUPERSEDED" });
|
|
240
|
+
return;
|
|
241
|
+
}
|
|
242
|
+
const t = Math.min(1, (performance.now() - started) / duration),
|
|
243
|
+
ease = 1 - (1 - t) ** 3,
|
|
244
|
+
bend = Math.sin(Math.PI * t) * Math.min(24, distance * 0.04);
|
|
245
|
+
move(
|
|
246
|
+
startX + dx * ease - (dy / (distance || 1)) * bend,
|
|
247
|
+
startY + dy * ease + (dx / (distance || 1)) * bend,
|
|
248
|
+
);
|
|
249
|
+
if (t === 1) {
|
|
250
|
+
move(input.point.x, input.point.y);
|
|
251
|
+
layer.setAttribute("data-nexum-visual-state", "thinking");
|
|
252
|
+
finish({ ok: true, arrived: true });
|
|
253
|
+
} else frame = requestAnimationFrame(step);
|
|
254
|
+
};
|
|
255
|
+
// Hidden or occluded documents can stop producing animation frames while
|
|
256
|
+
// their task queue remains live. Settle the real injected Promise too:
|
|
257
|
+
// an outer wait timeout cannot prove that its native execution has ended.
|
|
258
|
+
timer = setTimeout(
|
|
259
|
+
() => finish({ ok: false, reason: "VISUAL_TIMEOUT" }),
|
|
260
|
+
Math.max(1, input.deadline - Date.now()),
|
|
261
|
+
);
|
|
262
|
+
frame = requestAnimationFrame(step);
|
|
263
|
+
});
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
const id = (value) =>
|
|
267
|
+
typeof value === "string" &&
|
|
268
|
+
value.length > 0 &&
|
|
269
|
+
value.length <= 256 &&
|
|
270
|
+
!/[\0\r\n]/.test(value);
|
|
271
|
+
const nativeId = (value) => {
|
|
272
|
+
requireValue(
|
|
273
|
+
typeof value === "string" &&
|
|
274
|
+
/^[0-9]+$/.test(value) &&
|
|
275
|
+
Number.isSafeInteger(Number(value)),
|
|
276
|
+
"INVALID_TARGET",
|
|
277
|
+
);
|
|
278
|
+
return Number(value);
|
|
279
|
+
};
|
|
280
|
+
const key = (value) =>
|
|
281
|
+
JSON.stringify([
|
|
282
|
+
value.leaseId,
|
|
283
|
+
value.target.browserSessionId,
|
|
284
|
+
value.target.tabId,
|
|
285
|
+
value.target.controlEpoch ?? null,
|
|
286
|
+
]);
|
|
287
|
+
const identity = (value) => ({ ...value.target });
|
|
288
|
+
function current(value) {
|
|
289
|
+
value.signal?.throwIfAborted();
|
|
290
|
+
requireValue(
|
|
291
|
+
Number.isFinite(value.deadline) && Date.now() < value.deadline,
|
|
292
|
+
"TIMEOUT",
|
|
293
|
+
"The native visual preparation deadline expired.",
|
|
294
|
+
);
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
/** Cold optional UI controller. Decorative failures never replay a business operation. */
|
|
298
|
+
export function createVisualController(
|
|
299
|
+
chrome,
|
|
300
|
+
{ emit = () => {}, persist = async () => {} } = {},
|
|
301
|
+
) {
|
|
302
|
+
let started = false,
|
|
303
|
+
stopped = false,
|
|
304
|
+
browserSessionId,
|
|
305
|
+
disposal;
|
|
306
|
+
const records = new Map(),
|
|
307
|
+
groups = new Map(),
|
|
308
|
+
names = new Map(),
|
|
309
|
+
markers = new Map(),
|
|
310
|
+
ownActivations = new Map(),
|
|
311
|
+
closedKeys = new Map(),
|
|
312
|
+
styles = new Map(),
|
|
313
|
+
listeners = [],
|
|
314
|
+
tasks = new Set(),
|
|
315
|
+
taskTokens = new Map(),
|
|
316
|
+
wakeups = new Set();
|
|
317
|
+
let persistence = Promise.resolve();
|
|
318
|
+
const timeout = (deadline, callback) => {
|
|
319
|
+
const duration = Math.max(1, deadline - Date.now()),
|
|
320
|
+
due = performance.now() + duration;
|
|
321
|
+
const cancel = () => {
|
|
322
|
+
clearTimeout(timer);
|
|
323
|
+
wakeups.delete(wake);
|
|
324
|
+
};
|
|
325
|
+
const fire = () => {
|
|
326
|
+
cancel();
|
|
327
|
+
callback();
|
|
328
|
+
};
|
|
329
|
+
const wake = () => {
|
|
330
|
+
if (performance.now() >= due) fire();
|
|
331
|
+
};
|
|
332
|
+
const timer = setTimeout(fire, duration);
|
|
333
|
+
wakeups.add(wake);
|
|
334
|
+
return cancel;
|
|
335
|
+
};
|
|
336
|
+
const save = () => {
|
|
337
|
+
const snapshot = [...markers.values()].map((value) => ({
|
|
338
|
+
...value,
|
|
339
|
+
target: { ...value.target },
|
|
340
|
+
}));
|
|
341
|
+
const task = persistence.catch(() => {}).then(() => persist(snapshot));
|
|
342
|
+
persistence = task;
|
|
343
|
+
return task;
|
|
344
|
+
};
|
|
345
|
+
const notice = (ctx, reason) => {
|
|
346
|
+
try {
|
|
347
|
+
emit({
|
|
348
|
+
type: "event",
|
|
349
|
+
event: "visual_degraded",
|
|
350
|
+
leaseId: ctx.leaseId,
|
|
351
|
+
runId: ctx.runId,
|
|
352
|
+
operationId: ctx.operationId,
|
|
353
|
+
target: identity(ctx),
|
|
354
|
+
reason,
|
|
355
|
+
});
|
|
356
|
+
} catch {}
|
|
357
|
+
};
|
|
358
|
+
const observe = (task, token) => {
|
|
359
|
+
tasks.add(task);
|
|
360
|
+
if (token !== undefined) taskTokens.set(task, token);
|
|
361
|
+
void task
|
|
362
|
+
.finally(() => {
|
|
363
|
+
tasks.delete(task);
|
|
364
|
+
taskTokens.delete(task);
|
|
365
|
+
})
|
|
366
|
+
.catch(() => {});
|
|
367
|
+
return task;
|
|
368
|
+
};
|
|
369
|
+
const settled = async (token) => {
|
|
370
|
+
while (true) {
|
|
371
|
+
const pending = [...tasks].filter(
|
|
372
|
+
(task) => token === undefined || taskTokens.get(task) === token,
|
|
373
|
+
);
|
|
374
|
+
if (!pending.length) return;
|
|
375
|
+
await Promise.allSettled(pending);
|
|
376
|
+
}
|
|
377
|
+
};
|
|
378
|
+
const assertCtx = (ctx) => {
|
|
379
|
+
requireValue(
|
|
380
|
+
!stopped &&
|
|
381
|
+
id(ctx.leaseId) &&
|
|
382
|
+
id(ctx.runId) &&
|
|
383
|
+
id(ctx.operationId) &&
|
|
384
|
+
ctx.target &&
|
|
385
|
+
id(ctx.target.browserSessionId) &&
|
|
386
|
+
id(ctx.target.tabId),
|
|
387
|
+
"INVALID_TARGET",
|
|
388
|
+
"Native visual state needs its actual run and target.",
|
|
389
|
+
);
|
|
390
|
+
current(ctx);
|
|
391
|
+
requireValue(
|
|
392
|
+
!(closedKeys.get(key(ctx)) > Date.now()),
|
|
393
|
+
"STALE_REF",
|
|
394
|
+
"This visual control identity was released.",
|
|
395
|
+
);
|
|
396
|
+
if (browserSessionId !== undefined)
|
|
397
|
+
requireValue(
|
|
398
|
+
ctx.target.browserSessionId === browserSessionId,
|
|
399
|
+
"STALE_REF",
|
|
400
|
+
);
|
|
401
|
+
};
|
|
402
|
+
const page = async (record, command, ctx, extra = {}) => {
|
|
403
|
+
const cleanup = command === "release" || command === "clear";
|
|
404
|
+
const marker = markers.get(record.tabId),
|
|
405
|
+
passive =
|
|
406
|
+
command === "result" &&
|
|
407
|
+
marker?.documentId === record.documentId &&
|
|
408
|
+
key(markerCtx(marker)) === record.token;
|
|
409
|
+
const admit = () => {
|
|
410
|
+
if (passive) {
|
|
411
|
+
requireValue(!stopped, "EXTENSION_OFFLINE");
|
|
412
|
+
current(ctx);
|
|
413
|
+
} else assertCtx(ctx);
|
|
414
|
+
};
|
|
415
|
+
if (!cleanup) admit();
|
|
416
|
+
if (tasks.size >= 64 && !cleanup)
|
|
417
|
+
return { ok: false, reason: "VISUAL_BUSY" };
|
|
418
|
+
const deadline = Math.min(ctx.deadline, Date.now() + 250);
|
|
419
|
+
const target = { tabId: record.tabId, documentIds: [record.documentId] };
|
|
420
|
+
const flow = (async () => {
|
|
421
|
+
if (command === "move") {
|
|
422
|
+
let style = styles.get(record.tabId);
|
|
423
|
+
if (style?.documentId !== record.documentId) {
|
|
424
|
+
const created = {
|
|
425
|
+
documentId: record.documentId,
|
|
426
|
+
promise: chrome.scripting.insertCSS({ target, css: visualStyles }),
|
|
427
|
+
};
|
|
428
|
+
styles.set(record.tabId, created);
|
|
429
|
+
// A later action may retry its own visual preparation after a real
|
|
430
|
+
// native rejection. Never evict a newer document's pending style.
|
|
431
|
+
void created.promise.catch(() => {
|
|
432
|
+
if (styles.get(record.tabId) === created)
|
|
433
|
+
styles.delete(record.tabId);
|
|
434
|
+
});
|
|
435
|
+
style = created;
|
|
436
|
+
}
|
|
437
|
+
await style.promise;
|
|
438
|
+
}
|
|
439
|
+
if (!cleanup) admit();
|
|
440
|
+
const result = await chrome.scripting.executeScript({
|
|
441
|
+
target,
|
|
442
|
+
world: "ISOLATED",
|
|
443
|
+
func: visualPage,
|
|
444
|
+
args: [
|
|
445
|
+
{
|
|
446
|
+
command,
|
|
447
|
+
token: record.token,
|
|
448
|
+
runId: ctx.runId ?? record.runId,
|
|
449
|
+
operationId: ctx.operationId ?? record.operationId,
|
|
450
|
+
target: ctx.target ? { ...ctx.target } : record.target,
|
|
451
|
+
deadline,
|
|
452
|
+
...extra,
|
|
453
|
+
},
|
|
454
|
+
],
|
|
455
|
+
});
|
|
456
|
+
return result.length === 1 && result[0]?.result
|
|
457
|
+
? result[0].result
|
|
458
|
+
: { ok: false, reason: "VISUAL_UNAVAILABLE" };
|
|
459
|
+
})();
|
|
460
|
+
observe(flow, record.token);
|
|
461
|
+
let cancelTimer, onAbort;
|
|
462
|
+
const bounded = new Promise((resolve) => {
|
|
463
|
+
cancelTimer = timeout(deadline, () =>
|
|
464
|
+
resolve({ ok: false, reason: "VISUAL_TIMEOUT" }),
|
|
465
|
+
);
|
|
466
|
+
onAbort = () => resolve({ ok: false, reason: "VISUAL_CANCELLED" });
|
|
467
|
+
ctx.signal?.addEventListener("abort", onAbort, { once: true });
|
|
468
|
+
if (ctx.signal?.aborted) onAbort();
|
|
469
|
+
});
|
|
470
|
+
try {
|
|
471
|
+
return await Promise.race([flow, bounded]);
|
|
472
|
+
} catch {
|
|
473
|
+
return { ok: false, reason: "VISUAL_UNAVAILABLE" };
|
|
474
|
+
} finally {
|
|
475
|
+
cancelTimer();
|
|
476
|
+
ctx.signal?.removeEventListener("abort", onAbort);
|
|
477
|
+
}
|
|
478
|
+
};
|
|
479
|
+
const getRecord = async (ctx) => {
|
|
480
|
+
assertCtx(ctx);
|
|
481
|
+
const token = key(ctx),
|
|
482
|
+
existing = records.get(token);
|
|
483
|
+
for (const [entry, expires] of closedKeys)
|
|
484
|
+
if (Date.now() > expires) closedKeys.delete(entry);
|
|
485
|
+
requireValue(
|
|
486
|
+
!closedKeys.has(token),
|
|
487
|
+
"STALE_REF",
|
|
488
|
+
"This visual control identity was released.",
|
|
489
|
+
);
|
|
490
|
+
const tabId = nativeId(ctx.target.tabId),
|
|
491
|
+
frame = await chrome.webNavigation.getFrame({ tabId, frameId: 0 });
|
|
492
|
+
assertCtx(ctx);
|
|
493
|
+
requireValue(
|
|
494
|
+
!closedKeys.has(token),
|
|
495
|
+
"STALE_REF",
|
|
496
|
+
"This visual control identity was released.",
|
|
497
|
+
);
|
|
498
|
+
requireValue(
|
|
499
|
+
id(frame?.documentId),
|
|
500
|
+
"STALE_REF",
|
|
501
|
+
"The actual main document is unavailable for visual feedback.",
|
|
502
|
+
);
|
|
503
|
+
if (existing && existing.documentId === frame.documentId) return existing;
|
|
504
|
+
const record = {
|
|
505
|
+
token,
|
|
506
|
+
tabId,
|
|
507
|
+
documentId: frame.documentId,
|
|
508
|
+
target: identity(ctx),
|
|
509
|
+
leaseId: ctx.leaseId,
|
|
510
|
+
runId: ctx.runId,
|
|
511
|
+
operationId: ctx.operationId,
|
|
512
|
+
result: false,
|
|
513
|
+
};
|
|
514
|
+
records.set(token, record);
|
|
515
|
+
return record;
|
|
516
|
+
};
|
|
517
|
+
const group = async (record, ctx, tab) => {
|
|
518
|
+
const groupKey = JSON.stringify([ctx.runId, tab.windowId]);
|
|
519
|
+
let pending = groups.get(groupKey);
|
|
520
|
+
if (tab.groupId >= 0 && !pending) return; // Existing user/shared groups are never adopted.
|
|
521
|
+
if (!pending) {
|
|
522
|
+
pending = (async () => {
|
|
523
|
+
const latest = await chrome.tabs.get(record.tabId);
|
|
524
|
+
assertCtx(ctx);
|
|
525
|
+
if (latest.groupId >= 0) return null;
|
|
526
|
+
const groupId = await chrome.tabs.group({
|
|
527
|
+
tabIds: [record.tabId],
|
|
528
|
+
createProperties: { windowId: latest.windowId },
|
|
529
|
+
});
|
|
530
|
+
record.groupId = groupId;
|
|
531
|
+
try {
|
|
532
|
+
assertCtx(ctx);
|
|
533
|
+
} catch (cause) {
|
|
534
|
+
const actual = await chrome.tabs.get(record.tabId);
|
|
535
|
+
if (actual.groupId === groupId)
|
|
536
|
+
await chrome.tabs.ungroup([record.tabId]);
|
|
537
|
+
record.groupId = undefined;
|
|
538
|
+
throw cause;
|
|
539
|
+
}
|
|
540
|
+
await chrome.tabGroups.update(groupId, {
|
|
541
|
+
title: names.get(ctx.runId) || "Nexum browser",
|
|
542
|
+
color: "blue",
|
|
543
|
+
});
|
|
544
|
+
return { id: groupId, runId: ctx.runId, windowId: latest.windowId };
|
|
545
|
+
})();
|
|
546
|
+
groups.set(groupKey, pending);
|
|
547
|
+
void pending.catch(() => {
|
|
548
|
+
if (groups.get(groupKey) === pending) groups.delete(groupKey);
|
|
549
|
+
});
|
|
550
|
+
}
|
|
551
|
+
const owned = await pending;
|
|
552
|
+
if (!owned) return;
|
|
553
|
+
const latest = await chrome.tabs.get(record.tabId);
|
|
554
|
+
assertCtx(ctx);
|
|
555
|
+
if (latest.windowId !== owned.windowId) return;
|
|
556
|
+
if (latest.groupId < 0)
|
|
557
|
+
await chrome.tabs.group({ tabIds: [record.tabId], groupId: owned.id });
|
|
558
|
+
if (latest.groupId < 0 || latest.groupId === owned.id)
|
|
559
|
+
record.groupId = owned.id;
|
|
560
|
+
};
|
|
561
|
+
const beforeActionRaw = async (ctx) => {
|
|
562
|
+
assertCtx(ctx);
|
|
563
|
+
let record;
|
|
564
|
+
try {
|
|
565
|
+
record = await getRecord(ctx);
|
|
566
|
+
const tab = await chrome.tabs.get(record.tabId);
|
|
567
|
+
assertCtx(ctx);
|
|
568
|
+
record.result = false;
|
|
569
|
+
if (markers.delete(record.tabId)) await save();
|
|
570
|
+
// Grouping and the favicon use the same checked document but do not
|
|
571
|
+
// depend on one another. Both must settle before a business action.
|
|
572
|
+
const [result] = await Promise.all([
|
|
573
|
+
page(record, "active", ctx),
|
|
574
|
+
(async () => {
|
|
575
|
+
await group(record, ctx, tab);
|
|
576
|
+
assertCtx(ctx);
|
|
577
|
+
if (ctx.followSessionTab !== false && !tab.active) {
|
|
578
|
+
ownActivations.set(record.tabId, Date.now() + 1000);
|
|
579
|
+
await chrome.tabs.update(record.tabId, { active: true });
|
|
580
|
+
assertCtx(ctx);
|
|
581
|
+
}
|
|
582
|
+
})(),
|
|
583
|
+
]);
|
|
584
|
+
if (!result.ok) {
|
|
585
|
+
notice(ctx, result.reason || "VISUAL_UNAVAILABLE");
|
|
586
|
+
return { degraded: true };
|
|
587
|
+
}
|
|
588
|
+
return { degraded: false };
|
|
589
|
+
} catch (cause) {
|
|
590
|
+
current(ctx);
|
|
591
|
+
notice(ctx, "VISUAL_UNAVAILABLE");
|
|
592
|
+
return { degraded: true };
|
|
593
|
+
}
|
|
594
|
+
};
|
|
595
|
+
const bounded = async (ctx, perform) => {
|
|
596
|
+
assertCtx(ctx);
|
|
597
|
+
if (tasks.size >= 64) {
|
|
598
|
+
notice(ctx, "VISUAL_BUSY");
|
|
599
|
+
return { degraded: true };
|
|
600
|
+
}
|
|
601
|
+
const stop = new AbortController();
|
|
602
|
+
const limited = {
|
|
603
|
+
...ctx,
|
|
604
|
+
deadline: Math.min(ctx.deadline, Date.now() + 250),
|
|
605
|
+
signal: ctx.signal
|
|
606
|
+
? AbortSignal.any([ctx.signal, stop.signal])
|
|
607
|
+
: stop.signal,
|
|
608
|
+
};
|
|
609
|
+
const task = observe(
|
|
610
|
+
Promise.resolve().then(() => perform(limited)),
|
|
611
|
+
key(ctx),
|
|
612
|
+
);
|
|
613
|
+
let cancelTimer, onAbort;
|
|
614
|
+
try {
|
|
615
|
+
const status = await Promise.race([
|
|
616
|
+
task,
|
|
617
|
+
new Promise((resolve, reject) => {
|
|
618
|
+
cancelTimer = timeout(limited.deadline, () =>
|
|
619
|
+
resolve({ degraded: true }),
|
|
620
|
+
);
|
|
621
|
+
onAbort = () => reject(ctx.signal.reason);
|
|
622
|
+
ctx.signal?.addEventListener("abort", onAbort, { once: true });
|
|
623
|
+
if (ctx.signal?.aborted) onAbort();
|
|
624
|
+
}),
|
|
625
|
+
]);
|
|
626
|
+
if (status.degraded)
|
|
627
|
+
stop.abort(new Error("Native visual preparation stopped waiting."));
|
|
628
|
+
current(ctx);
|
|
629
|
+
if (status.degraded) notice(ctx, "VISUAL_UNAVAILABLE");
|
|
630
|
+
return status;
|
|
631
|
+
} catch {
|
|
632
|
+
current(ctx);
|
|
633
|
+
notice(ctx, "VISUAL_UNAVAILABLE");
|
|
634
|
+
return { degraded: true };
|
|
635
|
+
} finally {
|
|
636
|
+
cancelTimer?.();
|
|
637
|
+
ctx.signal?.removeEventListener("abort", onAbort);
|
|
638
|
+
}
|
|
639
|
+
};
|
|
640
|
+
const beforeAction = (ctx) => bounded(ctx, beforeActionRaw);
|
|
641
|
+
const beforePointer = async (ctx) => {
|
|
642
|
+
assertCtx(ctx);
|
|
643
|
+
requireValue(
|
|
644
|
+
ctx.point &&
|
|
645
|
+
Number.isFinite(ctx.point.x) &&
|
|
646
|
+
Number.isFinite(ctx.point.y) &&
|
|
647
|
+
Math.abs(ctx.point.x) <= 1000000 &&
|
|
648
|
+
Math.abs(ctx.point.y) <= 1000000 &&
|
|
649
|
+
Number.isSafeInteger(ctx.moveSequence) &&
|
|
650
|
+
ctx.moveSequence >= 0,
|
|
651
|
+
"INVALID_ARGUMENT",
|
|
652
|
+
);
|
|
653
|
+
const perform = async (limited) => {
|
|
654
|
+
const status =
|
|
655
|
+
(ctx.visualPrepared === true || ctx.wait === false) &&
|
|
656
|
+
records.has(key(ctx))
|
|
657
|
+
? { degraded: false }
|
|
658
|
+
: await beforeActionRaw(limited);
|
|
659
|
+
const record = records.get(key(ctx));
|
|
660
|
+
if (!record) return { degraded: true };
|
|
661
|
+
const result = await page(record, "move", limited, {
|
|
662
|
+
point: { x: ctx.point.x, y: ctx.point.y },
|
|
663
|
+
moveSequence: ctx.moveSequence,
|
|
664
|
+
});
|
|
665
|
+
if (!result.ok) notice(ctx, result.reason || "VISUAL_UNAVAILABLE");
|
|
666
|
+
return { degraded: status.degraded || !result.ok };
|
|
667
|
+
};
|
|
668
|
+
if (ctx.wait === false) {
|
|
669
|
+
void bounded(ctx, perform).catch(() => notice(ctx, "VISUAL_UNAVAILABLE"));
|
|
670
|
+
return { degraded: false };
|
|
671
|
+
}
|
|
672
|
+
return await bounded(ctx, perform);
|
|
673
|
+
};
|
|
674
|
+
const markDeliverable = async (ctx) => {
|
|
675
|
+
assertCtx(ctx);
|
|
676
|
+
try {
|
|
677
|
+
requireValue(
|
|
678
|
+
markers.has(nativeId(ctx.target.tabId)) || markers.size < 256,
|
|
679
|
+
"QUEUE_FULL",
|
|
680
|
+
"The visual result marker budget is full.",
|
|
681
|
+
);
|
|
682
|
+
const record = await getRecord(ctx);
|
|
683
|
+
const result = await page(record, "result", ctx);
|
|
684
|
+
assertCtx(ctx);
|
|
685
|
+
if (!result.ok) {
|
|
686
|
+
notice(ctx, result.reason || "VISUAL_UNAVAILABLE");
|
|
687
|
+
return { degraded: true };
|
|
688
|
+
}
|
|
689
|
+
record.result = true;
|
|
690
|
+
markers.set(record.tabId, {
|
|
691
|
+
tabId: record.tabId,
|
|
692
|
+
browserSessionId: ctx.target.browserSessionId,
|
|
693
|
+
documentId: record.documentId,
|
|
694
|
+
target: identity(ctx),
|
|
695
|
+
leaseId: ctx.leaseId,
|
|
696
|
+
runId: ctx.runId,
|
|
697
|
+
operationId: ctx.operationId,
|
|
698
|
+
});
|
|
699
|
+
await save();
|
|
700
|
+
return { degraded: false };
|
|
701
|
+
} catch {
|
|
702
|
+
current(ctx);
|
|
703
|
+
notice(ctx, "VISUAL_UNAVAILABLE");
|
|
704
|
+
return { degraded: true };
|
|
705
|
+
}
|
|
706
|
+
};
|
|
707
|
+
const forgetRun = (runId) => {
|
|
708
|
+
if ([...records.values()].some((record) => record.runId === runId)) return;
|
|
709
|
+
names.delete(runId);
|
|
710
|
+
for (const groupKey of groups.keys())
|
|
711
|
+
if (JSON.parse(groupKey)[0] === runId) groups.delete(groupKey);
|
|
712
|
+
};
|
|
713
|
+
const releaseTab = async (ctx) => {
|
|
714
|
+
const token = key(ctx);
|
|
715
|
+
if (ctx.result === true && !(closedKeys.get(token) > Date.now()))
|
|
716
|
+
await markDeliverable(ctx);
|
|
717
|
+
closedKeys.set(token, Number.POSITIVE_INFINITY);
|
|
718
|
+
await settled(token);
|
|
719
|
+
const record = records.get(token);
|
|
720
|
+
if (!record) {
|
|
721
|
+
closedKeys.set(token, Date.now() + 1000);
|
|
722
|
+
return { degraded: false };
|
|
723
|
+
}
|
|
724
|
+
let status;
|
|
725
|
+
const marker = markers.get(record.tabId);
|
|
726
|
+
if (record.result && marker && key(markerCtx(marker)) === token)
|
|
727
|
+
status = await page(
|
|
728
|
+
{ ...record, documentId: marker.documentId },
|
|
729
|
+
"result",
|
|
730
|
+
markerCtx(marker),
|
|
731
|
+
);
|
|
732
|
+
else {
|
|
733
|
+
record.result = false;
|
|
734
|
+
status = await page(record, "release", {
|
|
735
|
+
...ctx,
|
|
736
|
+
deadline: Date.now() + 250,
|
|
737
|
+
signal: undefined,
|
|
738
|
+
});
|
|
739
|
+
}
|
|
740
|
+
await settled(token);
|
|
741
|
+
records.delete(token);
|
|
742
|
+
closedKeys.set(token, Date.now() + 1000);
|
|
743
|
+
const styled = styles.get(record.tabId);
|
|
744
|
+
if (styled?.documentId === record.documentId) {
|
|
745
|
+
styles.delete(record.tabId);
|
|
746
|
+
try {
|
|
747
|
+
await chrome.scripting.removeCSS({
|
|
748
|
+
target: { tabId: record.tabId, documentIds: [record.documentId] },
|
|
749
|
+
css: visualStyles,
|
|
750
|
+
});
|
|
751
|
+
} catch {
|
|
752
|
+
notice(ctx, "VISUAL_UNAVAILABLE");
|
|
753
|
+
}
|
|
754
|
+
}
|
|
755
|
+
if (!record.result && record.groupId !== undefined)
|
|
756
|
+
try {
|
|
757
|
+
const tab = await chrome.tabs.get(record.tabId);
|
|
758
|
+
if (tab.groupId === record.groupId)
|
|
759
|
+
await chrome.tabs.ungroup([record.tabId]);
|
|
760
|
+
} catch {
|
|
761
|
+
notice(ctx, "VISUAL_GROUP_UNAVAILABLE");
|
|
762
|
+
}
|
|
763
|
+
forgetRun(record.runId);
|
|
764
|
+
return { degraded: !status.ok };
|
|
765
|
+
};
|
|
766
|
+
const nameSession = async (ctx) => {
|
|
767
|
+
requireValue(
|
|
768
|
+
!stopped &&
|
|
769
|
+
id(ctx.runId) &&
|
|
770
|
+
id(ctx.leaseId) &&
|
|
771
|
+
id(ctx.operationId) &&
|
|
772
|
+
ctx.target &&
|
|
773
|
+
id(ctx.target.browserSessionId),
|
|
774
|
+
"INVALID_TARGET",
|
|
775
|
+
);
|
|
776
|
+
requireValue(
|
|
777
|
+
typeof ctx.name === "string" &&
|
|
778
|
+
ctx.name.trim().length > 0 &&
|
|
779
|
+
ctx.name.length <= 120 &&
|
|
780
|
+
!/[\0\r\n]/.test(ctx.name),
|
|
781
|
+
"INVALID_ARGUMENT",
|
|
782
|
+
);
|
|
783
|
+
current(ctx);
|
|
784
|
+
names.set(ctx.runId, ctx.name.trim());
|
|
785
|
+
let degraded = false;
|
|
786
|
+
for (const pending of groups.values())
|
|
787
|
+
try {
|
|
788
|
+
const value = await pending;
|
|
789
|
+
if (value?.runId === ctx.runId)
|
|
790
|
+
await chrome.tabGroups.update(value.id, {
|
|
791
|
+
title: ctx.name.trim(),
|
|
792
|
+
color: "blue",
|
|
793
|
+
});
|
|
794
|
+
} catch {
|
|
795
|
+
degraded = true;
|
|
796
|
+
notice(ctx, "VISUAL_GROUP_UNAVAILABLE");
|
|
797
|
+
}
|
|
798
|
+
for (const target of ctx.targets ?? [])
|
|
799
|
+
await beforeAction({ ...ctx, target });
|
|
800
|
+
return { degraded };
|
|
801
|
+
};
|
|
802
|
+
const markerCtx = (marker) => ({
|
|
803
|
+
leaseId: marker.leaseId,
|
|
804
|
+
runId: marker.runId,
|
|
805
|
+
operationId: marker.operationId,
|
|
806
|
+
target: marker.target,
|
|
807
|
+
deadline: Date.now() + 250,
|
|
808
|
+
});
|
|
809
|
+
const start = async ({
|
|
810
|
+
browserSessionId: session,
|
|
811
|
+
markers: saved = [],
|
|
812
|
+
} = {}) => {
|
|
813
|
+
requireValue(!stopped, "EXTENSION_OFFLINE");
|
|
814
|
+
if (started) return;
|
|
815
|
+
started = true;
|
|
816
|
+
browserSessionId = session;
|
|
817
|
+
const listen = (event, fn) => {
|
|
818
|
+
if (event?.addListener) {
|
|
819
|
+
event.addListener(fn);
|
|
820
|
+
listeners.push(() => event.removeListener(fn));
|
|
821
|
+
}
|
|
822
|
+
};
|
|
823
|
+
listen(chrome.tabs.onActivated, (info) => {
|
|
824
|
+
const until = ownActivations.get(info.tabId);
|
|
825
|
+
ownActivations.delete(info.tabId);
|
|
826
|
+
if (until && Date.now() <= until) return;
|
|
827
|
+
const marker = markers.get(info.tabId);
|
|
828
|
+
if (!marker) return;
|
|
829
|
+
markers.delete(info.tabId);
|
|
830
|
+
const record = records.get(key(markerCtx(marker)));
|
|
831
|
+
if (record) record.result = false;
|
|
832
|
+
void observe(
|
|
833
|
+
(async () => {
|
|
834
|
+
await page(
|
|
835
|
+
{
|
|
836
|
+
tabId: marker.tabId,
|
|
837
|
+
documentId: marker.documentId,
|
|
838
|
+
token: key(markerCtx(marker)),
|
|
839
|
+
},
|
|
840
|
+
"clear",
|
|
841
|
+
markerCtx(marker),
|
|
842
|
+
);
|
|
843
|
+
await save();
|
|
844
|
+
})(),
|
|
845
|
+
key(markerCtx(marker)),
|
|
846
|
+
).catch(() => {});
|
|
847
|
+
});
|
|
848
|
+
listen(chrome.tabs.onRemoved, (tabId) => {
|
|
849
|
+
markers.delete(tabId);
|
|
850
|
+
ownActivations.delete(tabId);
|
|
851
|
+
for (const [token, record] of records)
|
|
852
|
+
if (record.tabId === tabId) records.delete(token);
|
|
853
|
+
void save().catch(() => {});
|
|
854
|
+
});
|
|
855
|
+
listen(chrome.tabs.onUpdated, (tabId, change) => {
|
|
856
|
+
if (change.status !== "complete") return;
|
|
857
|
+
const marker = markers.get(tabId);
|
|
858
|
+
if (!marker) return;
|
|
859
|
+
void observe(
|
|
860
|
+
(async () => {
|
|
861
|
+
const frame = await chrome.webNavigation.getFrame({
|
|
862
|
+
tabId,
|
|
863
|
+
frameId: 0,
|
|
864
|
+
});
|
|
865
|
+
if (stopped || markers.get(tabId) !== marker) return;
|
|
866
|
+
if (!id(frame?.documentId)) return;
|
|
867
|
+
marker.documentId = frame.documentId;
|
|
868
|
+
marker.target = { ...marker.target };
|
|
869
|
+
delete marker.target.pageVersion;
|
|
870
|
+
await page(
|
|
871
|
+
{
|
|
872
|
+
tabId,
|
|
873
|
+
documentId: marker.documentId,
|
|
874
|
+
token: key(markerCtx(marker)),
|
|
875
|
+
},
|
|
876
|
+
"result",
|
|
877
|
+
markerCtx(marker),
|
|
878
|
+
);
|
|
879
|
+
await save();
|
|
880
|
+
})(),
|
|
881
|
+
key(markerCtx(marker)),
|
|
882
|
+
).catch(() => {});
|
|
883
|
+
});
|
|
884
|
+
if (session !== undefined)
|
|
885
|
+
for (const value of saved.slice(0, 256))
|
|
886
|
+
if (
|
|
887
|
+
value &&
|
|
888
|
+
value.browserSessionId === session &&
|
|
889
|
+
value.target?.browserSessionId === session &&
|
|
890
|
+
id(value.documentId) &&
|
|
891
|
+
id(value.target?.tabId) &&
|
|
892
|
+
id(value.leaseId) &&
|
|
893
|
+
id(value.runId) &&
|
|
894
|
+
id(value.operationId)
|
|
895
|
+
) {
|
|
896
|
+
try {
|
|
897
|
+
const tabId = nativeId(value.target.tabId),
|
|
898
|
+
frame = await observe(
|
|
899
|
+
chrome.webNavigation.getFrame({
|
|
900
|
+
tabId,
|
|
901
|
+
frameId: 0,
|
|
902
|
+
}),
|
|
903
|
+
key(markerCtx(value)),
|
|
904
|
+
);
|
|
905
|
+
if (stopped) return;
|
|
906
|
+
if (!id(frame?.documentId)) continue;
|
|
907
|
+
const marker = {
|
|
908
|
+
...value,
|
|
909
|
+
tabId,
|
|
910
|
+
documentId: frame.documentId,
|
|
911
|
+
target: { ...value.target },
|
|
912
|
+
};
|
|
913
|
+
markers.set(tabId, marker);
|
|
914
|
+
await page(
|
|
915
|
+
{
|
|
916
|
+
tabId,
|
|
917
|
+
documentId: marker.documentId,
|
|
918
|
+
token: key(markerCtx(marker)),
|
|
919
|
+
},
|
|
920
|
+
"result",
|
|
921
|
+
markerCtx(marker),
|
|
922
|
+
);
|
|
923
|
+
} catch {}
|
|
924
|
+
}
|
|
925
|
+
};
|
|
926
|
+
const dispose = () => {
|
|
927
|
+
if (disposal) return disposal;
|
|
928
|
+
stopped = true;
|
|
929
|
+
for (const remove of listeners.splice(0)) remove();
|
|
930
|
+
for (const token of [...records.keys(), ...taskTokens.values()])
|
|
931
|
+
closedKeys.set(token, Number.POSITIVE_INFINITY);
|
|
932
|
+
disposal = (async () => {
|
|
933
|
+
await settled();
|
|
934
|
+
for (const record of records.values())
|
|
935
|
+
if (!record.result) {
|
|
936
|
+
await page(record, "release", { deadline: Date.now() + 250 });
|
|
937
|
+
if (record.groupId !== undefined)
|
|
938
|
+
try {
|
|
939
|
+
const tab = await chrome.tabs.get(record.tabId);
|
|
940
|
+
if (tab.groupId === record.groupId)
|
|
941
|
+
await chrome.tabs.ungroup([record.tabId]);
|
|
942
|
+
} catch {}
|
|
943
|
+
}
|
|
944
|
+
await settled();
|
|
945
|
+
for (const [tabId, style] of styles) {
|
|
946
|
+
try {
|
|
947
|
+
await chrome.scripting.removeCSS({
|
|
948
|
+
target: { tabId, documentIds: [style.documentId] },
|
|
949
|
+
css: visualStyles,
|
|
950
|
+
});
|
|
951
|
+
} catch {}
|
|
952
|
+
}
|
|
953
|
+
records.clear();
|
|
954
|
+
ownActivations.clear();
|
|
955
|
+
closedKeys.clear();
|
|
956
|
+
styles.clear();
|
|
957
|
+
groups.clear();
|
|
958
|
+
names.clear();
|
|
959
|
+
await persistence.catch(() => {});
|
|
960
|
+
})();
|
|
961
|
+
void disposal.catch(() => {});
|
|
962
|
+
return disposal;
|
|
963
|
+
};
|
|
964
|
+
return Object.freeze({
|
|
965
|
+
start,
|
|
966
|
+
wake() {
|
|
967
|
+
for (const wake of [...wakeups]) wake();
|
|
968
|
+
},
|
|
969
|
+
beforeAction,
|
|
970
|
+
beforePointer,
|
|
971
|
+
markDeliverable,
|
|
972
|
+
releaseTab,
|
|
973
|
+
nameSession,
|
|
974
|
+
dispose,
|
|
975
|
+
});
|
|
976
|
+
}
|