@nexum-hub/browser 0.0.3-next.15 → 0.0.3-next.17

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.
@@ -371,14 +371,15 @@ route("tabs.get", async (ctx, args) =>
371
371
  );
372
372
  route(
373
373
  "tabs.new",
374
- async (ctx, args) =>
375
- tabInfo(
376
- ctx,
377
- await ctx.chrome.tabs.create({
378
- ...(args.url === undefined ? {} : { url: httpUrl(args.url) }),
379
- active: options(args).active ?? true,
380
- }),
381
- ),
374
+ async (ctx, args) => {
375
+ const tab = await ctx.chrome.tabs.create({
376
+ ...(args.url === undefined ? {} : { url: httpUrl(args.url) }),
377
+ active: options(args).active ?? true,
378
+ });
379
+ const info = tabInfo(ctx, tab);
380
+ await ctx.router.trackVisualTab({ ...ctx, target: info.target });
381
+ return info;
382
+ },
382
383
  { mutating: true },
383
384
  );
384
385
  route("tabs.selected", async (ctx) => {
@@ -394,6 +395,10 @@ route(
394
395
  const id = tabIdOf(ctx, args);
395
396
  ctx.router.claim(ctx.lease, id);
396
397
  await ctx.chrome.tabs.update(id, { active: true });
398
+ await ctx.router.trackVisualTab({
399
+ ...ctx,
400
+ target: ctx.router.target(String(id), ctx.lease),
401
+ });
397
402
  return ctx.router.actionResult(ctx, id);
398
403
  },
399
404
  { mutating: true },
@@ -1121,6 +1126,31 @@ async function point(ctx, args) {
1121
1126
  requireValue(Number.isFinite(args.x) && Number.isFinite(args.y));
1122
1127
  return { x: args.x, y: args.y };
1123
1128
  }
1129
+
1130
+ // Build the points for real Chrome Input.dispatchMouseEvent calls, not a
1131
+ // decorative animation that runs independently of the actual input.
1132
+ export function humanPointerPath(from, to) {
1133
+ const dx = to.x - from.x,
1134
+ dy = to.y - from.y,
1135
+ distance = Math.hypot(dx, dy);
1136
+ if (distance < 12) return [{ x: to.x, y: to.y, delayMs: 0 }];
1137
+ const steps = Math.min(28, Math.max(3, Math.ceil(distance / 28))),
1138
+ durationMs = Math.min(420, Math.max(110, 85 + distance * 0.35)),
1139
+ bend = Math.min(18, distance * 0.08) *
1140
+ (Math.sin(from.x * 0.013 + to.y * 0.017) < 0 ? -1 : 1);
1141
+ const points = [];
1142
+ for (let i = 1; i <= steps; i++) {
1143
+ const t = i / steps,
1144
+ eased = t * t * (3 - 2 * t),
1145
+ arc = Math.sin(Math.PI * t) * bend;
1146
+ points.push({
1147
+ x: i === steps ? to.x : from.x + dx * eased - (dy / distance) * arc,
1148
+ y: i === steps ? to.y : from.y + dy * eased + (dx / distance) * arc,
1149
+ delayMs: i === steps ? 0 : durationMs / steps,
1150
+ });
1151
+ }
1152
+ return points;
1153
+ }
1124
1154
  async function click(ctx, args, double = false) {
1125
1155
  const p = await point(ctx, args),
1126
1156
  button = buttons[args.button ?? "left"];
@@ -1179,6 +1209,13 @@ route(
1179
1209
  requireValue(
1180
1210
  Number.isFinite(input.scrollX) && Number.isFinite(input.scrollY),
1181
1211
  );
1212
+ // Position the real pointer along the same human trajectory before
1213
+ // applying the wheel at that point; never teleport the visual cursor.
1214
+ await ctx.router.send(ctx, "Input.dispatchMouseEvent", {
1215
+ type: "mouseMoved",
1216
+ ...(await point(ctx, input)),
1217
+ modifiers: modifierMask(input.keypress),
1218
+ });
1182
1219
  await ctx.router.send(ctx, "Input.dispatchMouseEvent", {
1183
1220
  type: "mouseWheel",
1184
1221
  ...(await point(ctx, input)),
@@ -1684,6 +1721,7 @@ export function createBrowserRouter(
1684
1721
  journalTail = Promise.resolve();
1685
1722
  const versions = new Map(),
1686
1723
  documents = new Map(),
1724
+ pointerPositions = new Map(),
1687
1725
  leases = new Map(),
1688
1726
  closedLeases = new Map(),
1689
1727
  owners = new Map(),
@@ -2255,11 +2293,51 @@ export function createBrowserRouter(
2255
2293
  throw cause;
2256
2294
  }
2257
2295
  };
2258
- const releaseControl = async (lease, tabId, { detach = true } = {}) => {
2296
+ // Chrome may hold a native command indefinitely while a JavaScript dialog
2297
+ // pauses its renderer. A cancelled action must not hold the run's pending
2298
+ // queue forever; the native command may still have an unknown page effect,
2299
+ // so actual debugger detach remains a separate required cleanup step.
2300
+ const nativeWithStop = async (signal, invoke) => {
2301
+ signal?.throwIfAborted();
2302
+ const native = Promise.resolve().then(() => {
2303
+ signal?.throwIfAborted();
2304
+ return invoke();
2305
+ });
2306
+ if (!signal) return native;
2307
+ let onAbort;
2308
+ const cancelled = new Promise((_, reject) => {
2309
+ onAbort = () => reject(signal.reason);
2310
+ signal.addEventListener("abort", onAbort, { once: true });
2311
+ });
2312
+ try {
2313
+ if (signal.aborted) onAbort();
2314
+ return await Promise.race([native, cancelled]);
2315
+ } finally {
2316
+ signal.removeEventListener("abort", onAbort);
2317
+ }
2318
+ };
2319
+ const releaseControl = async (
2320
+ lease,
2321
+ tabId,
2322
+ { detach = true, dismissDialog = false } = {},
2323
+ ) => {
2259
2324
  if (!lease.attached.has(tabId) && !lease.intents.has(tabId)) return;
2260
2325
  for (const waiter of waiters.values())
2261
2326
  if (waiter.leaseId === lease.id && waiter.tabId === tabId)
2262
2327
  cancelWaiter(waiter);
2328
+ // An observed JavaScript dialog pauses its renderer. Dismiss the dialog
2329
+ // for a terminating run before other native cleanup commands; explicit
2330
+ // handoff keeps the dialog available for the user instead.
2331
+ const dialog = dialogs.get(String(tabId));
2332
+ if (dismissDialog && dialog && lease.attached.has(tabId)) {
2333
+ await chrome.debugger.sendCommand(
2334
+ { tabId },
2335
+ "Page.handleJavaScriptDialog",
2336
+ { accept: false },
2337
+ );
2338
+ if (dialogs.get(String(tabId)) === dialog)
2339
+ dialogs.delete(String(tabId));
2340
+ }
2263
2341
  await stopLocalDownloadClick(lease, tabId);
2264
2342
  if (lease.attached.has(tabId)) await syncDownloadIntercept(lease, tabId);
2265
2343
  if (!lease.attached.has(tabId) && !lease.externallyDetached.has(tabId)) {
@@ -2522,6 +2600,7 @@ export function createBrowserRouter(
2522
2600
  finishNavigation(details, "load"),
2523
2601
  );
2524
2602
  on(chrome.tabs.onRemoved, (id) => {
2603
+ pointerPositions.delete(id);
2525
2604
  versions.set(String(id), version(id) + 1);
2526
2605
  dialogs.delete(String(id));
2527
2606
  emit({
@@ -3081,7 +3160,7 @@ export function createBrowserRouter(
3081
3160
  .filter((id) => id !== undefined),
3082
3161
  ))
3083
3162
  try {
3084
- await releaseControl(lease, tabId);
3163
+ await releaseControl(lease, tabId, { dismissDialog: true });
3085
3164
  } catch (cause) {
3086
3165
  tempErrors.push(cause);
3087
3166
  }
@@ -3100,7 +3179,7 @@ export function createBrowserRouter(
3100
3179
  }
3101
3180
  for (const tabId of new Set([...lease.attached, ...lease.intents])) {
3102
3181
  try {
3103
- await releaseControl(lease, tabId);
3182
+ await releaseControl(lease, tabId, { dismissDialog: true });
3104
3183
  } catch (cause) {
3105
3184
  errors.push(cause);
3106
3185
  }
@@ -3331,6 +3410,14 @@ export function createBrowserRouter(
3331
3410
  ].includes(input.method)
3332
3411
  )
3333
3412
  await router.prepareVisual(ctx);
3413
+ if (
3414
+ visual &&
3415
+ definition.needsTab &&
3416
+ !definition.mutating &&
3417
+ input.target.tabId !== undefined &&
3418
+ !lease.temporaryTabs?.has(nativeId(input.target.tabId))
3419
+ )
3420
+ await router.trackVisualTab(ctx);
3334
3421
  actionSignal.throwIfAborted();
3335
3422
  managementHandling = true;
3336
3423
  value = await definition.handle(ctx, input.args ?? {});
@@ -3671,12 +3758,14 @@ export function createBrowserRouter(
3671
3758
  const framePath = payload.locator?.framePath ?? payload.framePath ?? [];
3672
3759
  if (framePath.length > 0) return framePage(ctx, payload, framePath);
3673
3760
  ctx.dispatched();
3674
- const result = await chrome.scripting.executeScript({
3675
- target: { tabId: nativeId(ctx.target.tabId) },
3676
- world: "ISOLATED",
3677
- func: pageCommand,
3678
- args: [input],
3679
- });
3761
+ const result = await nativeWithStop(ctx.signal, () =>
3762
+ chrome.scripting.executeScript({
3763
+ target: { tabId: nativeId(ctx.target.tabId) },
3764
+ world: "ISOLATED",
3765
+ func: pageCommand,
3766
+ args: [input],
3767
+ }),
3768
+ );
3680
3769
  // A main-frame replacement can finish an injection with no result. Only
3681
3770
  // a readiness observation may wait for a browser-confirmed transition;
3682
3771
  // actions and empty results from settled pages must still fail.
@@ -3865,6 +3954,17 @@ export function createBrowserRouter(
3865
3954
  ctx.dispatched();
3866
3955
  await chrome.debugger.attach({ tabId: id }, "1.3");
3867
3956
  ctx.lease.attached.add(id);
3957
+ // Observe Page.javascriptDialogOpening before *any* page action can
3958
+ // synchronously open a prompt/confirm. Waiting until getJsDialog is
3959
+ // called loses the event while Runtime.evaluate may remain blocked.
3960
+ try {
3961
+ await nativeWithStop(ctx.signal, () =>
3962
+ chrome.debugger.sendCommand({ tabId: id }, "Page.enable", {}),
3963
+ );
3964
+ } catch (cause) {
3965
+ await releaseControl(ctx.lease, id).catch(() => {});
3966
+ throw cause;
3967
+ }
3868
3968
  if (ctx.lease.ended) {
3869
3969
  await releaseControl(ctx.lease, id);
3870
3970
  throw new BrowserActionError(
@@ -3880,9 +3980,36 @@ export function createBrowserRouter(
3880
3980
  ctx.lease.attaching.delete(id);
3881
3981
  }
3882
3982
  },
3883
- async send(ctx, method, params, child) {
3983
+ async send(ctx, method, params, child, nativePointerStep = false) {
3884
3984
  active(ctx.lease);
3885
3985
  ctx.signal?.throwIfAborted();
3986
+ // Every high-level mouse move, click, hover and drag shares the same
3987
+ // physical trajectory. Raw cdp.send remains exact and unmodified.
3988
+ if (
3989
+ !nativePointerStep &&
3990
+ !child &&
3991
+ ctx.method !== "cdp.send" &&
3992
+ method === "Input.dispatchMouseEvent" &&
3993
+ params.type === "mouseMoved" &&
3994
+ Number.isFinite(params.x) &&
3995
+ Number.isFinite(params.y)
3996
+ ) {
3997
+ const tabId = nativeId(ctx.target.tabId),
3998
+ previous = pointerPositions.get(tabId) ?? { x: 32, y: 72 },
3999
+ path = humanPointerPath(previous, { x: params.x, y: params.y });
4000
+ let result;
4001
+ for (const step of path) {
4002
+ result = await router.send(
4003
+ ctx,
4004
+ method,
4005
+ { ...params, x: step.x, y: step.y },
4006
+ child,
4007
+ true,
4008
+ );
4009
+ if (step.delayMs > 0) await router.delay(ctx, step.delayMs);
4010
+ }
4011
+ return result;
4012
+ }
3886
4013
  await router.attach(ctx);
3887
4014
  ctx.signal?.throwIfAborted();
3888
4015
  if (
@@ -4035,7 +4162,16 @@ export function createBrowserRouter(
4035
4162
  ctx.pendingInfo.nativeNetworkTabId = tabId;
4036
4163
  let result;
4037
4164
  try {
4038
- result = await chrome.debugger.sendCommand(source, method, params);
4165
+ // A native Runtime.evaluate can stay suspended behind a modal dialog
4166
+ // after its action is cancelled. Other native commands (notably IO.read
4167
+ // and IO.close) must retain their actual settlement: dropping their
4168
+ // replies early can abandon a native stream or misreport cleanup.
4169
+ result =
4170
+ method === "Runtime.evaluate"
4171
+ ? await nativeWithStop(ctx.signal, () =>
4172
+ chrome.debugger.sendCommand(source, method, params),
4173
+ )
4174
+ : await chrome.debugger.sendCommand(source, method, params);
4039
4175
  } catch (cause) {
4040
4176
  if (armedLocalDownload) await stopLocalDownloadClick(ctx.lease, tabId);
4041
4177
  throw cause;
@@ -4044,6 +4180,14 @@ export function createBrowserRouter(
4044
4180
  delete ctx.pendingInfo.nativeNetworkTabId;
4045
4181
  if (watchEnd) await stopLocalDownloadClick(ctx.lease, tabId);
4046
4182
  }
4183
+ if (
4184
+ !child &&
4185
+ method === "Input.dispatchMouseEvent" &&
4186
+ params.type === "mouseMoved" &&
4187
+ Number.isFinite(params.x) &&
4188
+ Number.isFinite(params.y)
4189
+ )
4190
+ pointerPositions.set(tabId, { x: params.x, y: params.y });
4047
4191
  if (rawFetch && method === "Fetch.disable") {
4048
4192
  ctx.lease.rawFetch?.delete(tabId);
4049
4193
  await syncDownloadIntercept(ctx.lease, tabId);
@@ -5308,6 +5452,22 @@ export function createBrowserRouter(
5308
5452
  });
5309
5453
  }
5310
5454
  },
5455
+ async trackVisualTab(ctx) {
5456
+ try {
5457
+ await visual?.trackTab(router.visualContext(ctx));
5458
+ } catch {
5459
+ ctx.signal?.throwIfAborted();
5460
+ emit({
5461
+ type: "event",
5462
+ event: "visual_degraded",
5463
+ leaseId: ctx.lease.id,
5464
+ runId: ctx.runId,
5465
+ operationId: ctx.operationId,
5466
+ target: ctx.target,
5467
+ reason: "VISUAL_UNAVAILABLE",
5468
+ });
5469
+ }
5470
+ },
5311
5471
  async preparePointer(ctx, point) {
5312
5472
  if (
5313
5473
  ctx.lastVisualPoint?.x === point.x &&