@jobshimo/browser-link 0.23.7 → 0.23.8
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/dist/agent-instructions/content.js +12 -6
- package/dist/agent-instructions/content.js.map +1 -1
- package/dist/cdp/flow.d.ts +67 -10
- package/dist/cdp/flow.js +51 -20
- package/dist/cdp/flow.js.map +1 -1
- package/dist/cdp/transport.js +5 -0
- package/dist/cdp/transport.js.map +1 -1
- package/dist/commands/about.js +2 -2
- package/dist/commands/about.js.map +1 -1
- package/dist/extension/background.js +295 -276
- package/dist/extension/background.js.map +1 -1
- package/dist/extension/flow.d.ts +67 -10
- package/dist/extension/flow.js +51 -20
- package/dist/extension/flow.js.map +1 -1
- package/dist/extension/manifest.json +1 -1
- package/dist/map/tools.js +2 -2
- package/dist/map/tools.js.map +1 -1
- package/dist/permissions.js +1 -1
- package/dist/permissions.js.map +1 -1
- package/dist/tools/browser-definitions.js +32 -5
- package/dist/tools/browser-definitions.js.map +1 -1
- package/dist/tools/browser-dispatch.js +62 -2
- package/dist/tools/browser-dispatch.js.map +1 -1
- package/dist/tools/server-instructions.js +10 -4
- package/dist/tools/server-instructions.js.map +1 -1
- package/package.json +1 -1
|
@@ -631,8 +631,9 @@ async function waitForLoad(tabId, timeoutMs = 20_000) {
|
|
|
631
631
|
}
|
|
632
632
|
/**
|
|
633
633
|
* `performFind` / `performClick` / `performType` / `performPress` /
|
|
634
|
-
* `performWaitFor` — the extracted bodies of the
|
|
635
|
-
* `click` / `type` / `press` / `wait_for`
|
|
634
|
+
* `performWaitFor` / `performDrag` — the extracted bodies of the
|
|
635
|
+
* standalone `find` / `click` / `type` / `press` / `wait_for` / `drag`
|
|
636
|
+
* cases in `handleTool` below.
|
|
636
637
|
*
|
|
637
638
|
* Both the standalone `case` handlers AND `browser.flow`'s step executor
|
|
638
639
|
* (`runFlow` in `./flow.js`) call these exact functions — there is one
|
|
@@ -864,6 +865,279 @@ async function performWaitFor(tabId, state, params) {
|
|
|
864
865
|
return { ok: false, error: err instanceof Error ? err.message : String(err) };
|
|
865
866
|
}
|
|
866
867
|
}
|
|
868
|
+
async function performDrag(tabId, params) {
|
|
869
|
+
try {
|
|
870
|
+
// Same numeric guard the wire boundary used before this body was
|
|
871
|
+
// extracted out of `handleDrag`: anything non-finite or negative is
|
|
872
|
+
// treated as absent, so flow drag steps get identical protection.
|
|
873
|
+
const num = (v) => typeof v === 'number' && Number.isFinite(v) && v >= 0 ? v : undefined;
|
|
874
|
+
const clamp = (v, max) => Math.min(v, max);
|
|
875
|
+
const fromSelector = params.from_selector;
|
|
876
|
+
const toSelector = params.to_selector;
|
|
877
|
+
const fromXRaw = num(params.from_x);
|
|
878
|
+
const fromYRaw = num(params.from_y);
|
|
879
|
+
const toXRaw = num(params.to_x);
|
|
880
|
+
const toYRaw = num(params.to_y);
|
|
881
|
+
// Cap every duration that ends up in a setTimeout to keep CodeQL's
|
|
882
|
+
// "user-controlled timer duration" check happy and to prevent a
|
|
883
|
+
// misbehaving agent from parking the bridge for hours on a typo.
|
|
884
|
+
const durationMs = clamp(num(params.duration_ms) ?? 1500, MAX_DRAG_DURATION_MS);
|
|
885
|
+
const holdBeforeMoveMs = clamp(num(params.hold_before_move_ms) ?? 0, MAX_DRAG_HOLD_MS);
|
|
886
|
+
const holdBeforeReleaseMs = clamp(num(params.hold_before_release_ms) ?? 0, MAX_DRAG_HOLD_MS);
|
|
887
|
+
const hasFromCoords = fromXRaw !== undefined && fromYRaw !== undefined;
|
|
888
|
+
const hasToCoords = toXRaw !== undefined && toYRaw !== undefined;
|
|
889
|
+
if (!fromSelector && !hasFromCoords) {
|
|
890
|
+
return { ok: false, error: 'drag: provide from_selector or both from_x and from_y' };
|
|
891
|
+
}
|
|
892
|
+
if (!toSelector && !hasToCoords) {
|
|
893
|
+
return { ok: false, error: 'drag: provide to_selector or both to_x and to_y' };
|
|
894
|
+
}
|
|
895
|
+
const probeExpr = buildDragProbeJs({
|
|
896
|
+
from_selector: fromSelector,
|
|
897
|
+
to_selector: toSelector,
|
|
898
|
+
from_x: fromXRaw,
|
|
899
|
+
from_y: fromYRaw,
|
|
900
|
+
to_x: toXRaw,
|
|
901
|
+
to_y: toYRaw,
|
|
902
|
+
});
|
|
903
|
+
const probe = await evaluateInTab(tabId, probeExpr);
|
|
904
|
+
if (probe.err) {
|
|
905
|
+
return { ok: false, error: `drag: ${probe.err}` };
|
|
906
|
+
}
|
|
907
|
+
if (!probe.from || !probe.to) {
|
|
908
|
+
return { ok: false, error: 'drag: could not resolve coordinates' };
|
|
909
|
+
}
|
|
910
|
+
if (!probe.from.in_viewport) {
|
|
911
|
+
return {
|
|
912
|
+
ok: false,
|
|
913
|
+
error: 'drag: source point is offscreen — scroll first or pass viewport coords',
|
|
914
|
+
};
|
|
915
|
+
}
|
|
916
|
+
if (!probe.to.in_viewport) {
|
|
917
|
+
return {
|
|
918
|
+
ok: false,
|
|
919
|
+
error: 'drag: destination point is offscreen — scroll first or pass viewport coords',
|
|
920
|
+
};
|
|
921
|
+
}
|
|
922
|
+
const fromX = probe.from.x;
|
|
923
|
+
const fromY = probe.from.y;
|
|
924
|
+
const toX = probe.to.x;
|
|
925
|
+
const toY = probe.to.y;
|
|
926
|
+
const isDraggable = !!probe.draggable;
|
|
927
|
+
// ~30fps interpolation; minimum 2 steps so the path actually has a midpoint.
|
|
928
|
+
const steps = durationMs > 0 ? Math.max(2, Math.round(durationMs / 33)) : 1;
|
|
929
|
+
const stepDelayMs = steps > 0 ? durationMs / steps : 0;
|
|
930
|
+
const eventsFired = [];
|
|
931
|
+
// Every branch below reassigns `dragMode` before the final `return`
|
|
932
|
+
// reads it. (The old `case`-block extraction needed a
|
|
933
|
+
// no-useless-assignment disable here; inside this try/catch scope the
|
|
934
|
+
// linter no longer flags the initializer, so the directive is gone.)
|
|
935
|
+
let dragMode = 'pointer';
|
|
936
|
+
let interceptReceived = false;
|
|
937
|
+
const dragStart = Date.now();
|
|
938
|
+
let interceptionEnabled = false;
|
|
939
|
+
const interpolate = (t) => ({
|
|
940
|
+
x: fromX + (toX - fromX) * t,
|
|
941
|
+
y: fromY + (toY - fromY) * t,
|
|
942
|
+
});
|
|
943
|
+
// Mouse move WITH the left button held down. Chrome's HTML5 drag
|
|
944
|
+
// system only treats movement as a drag when the button state is
|
|
945
|
+
// signalled on every move after mousePressed; omitting it makes
|
|
946
|
+
// Blink ignore the move for drag-detection purposes.
|
|
947
|
+
const moveHeld = (x, y) => cdp(tabId, 'Input.dispatchMouseEvent', {
|
|
948
|
+
type: 'mouseMoved',
|
|
949
|
+
x,
|
|
950
|
+
y,
|
|
951
|
+
button: 'left',
|
|
952
|
+
buttons: 1,
|
|
953
|
+
});
|
|
954
|
+
// Wiggle distance has to comfortably clear `kDragThreshold` (~5px on
|
|
955
|
+
// Mac, 4px on Linux/Windows). 5px landed *right* at the boundary and
|
|
956
|
+
// Chrome did not start the drag — 20px crosses it on every platform.
|
|
957
|
+
const WIGGLE_PX = 20;
|
|
958
|
+
// Time to wait for Input.dragIntercepted after the wiggle. 120ms was
|
|
959
|
+
// enough on local-only synthetic tests but flaky in real Chrome —
|
|
960
|
+
// 250ms is comfortable without dragging out the overall latency.
|
|
961
|
+
const INTERCEPT_TIMEOUT_MS = 250;
|
|
962
|
+
try {
|
|
963
|
+
if (isDraggable) {
|
|
964
|
+
try {
|
|
965
|
+
await cdp(tabId, 'Input.setInterceptDrags', { enabled: true });
|
|
966
|
+
interceptionEnabled = true;
|
|
967
|
+
}
|
|
968
|
+
catch {
|
|
969
|
+
// setInterceptDrags is experimental — fall back to pointer mode silently.
|
|
970
|
+
}
|
|
971
|
+
}
|
|
972
|
+
if (interceptionEnabled) {
|
|
973
|
+
// Arm the listener BEFORE the press+wiggle that may trigger it.
|
|
974
|
+
const interceptPromise = waitForCdpEvent(tabId, 'Input.dragIntercepted', INTERCEPT_TIMEOUT_MS);
|
|
975
|
+
await cdp(tabId, 'Input.dispatchMouseEvent', {
|
|
976
|
+
type: 'mouseMoved',
|
|
977
|
+
x: fromX,
|
|
978
|
+
y: fromY,
|
|
979
|
+
});
|
|
980
|
+
await cdp(tabId, 'Input.dispatchMouseEvent', {
|
|
981
|
+
type: 'mousePressed',
|
|
982
|
+
x: fromX,
|
|
983
|
+
y: fromY,
|
|
984
|
+
button: 'left',
|
|
985
|
+
clickCount: 1,
|
|
986
|
+
});
|
|
987
|
+
if (holdBeforeMoveMs > 0)
|
|
988
|
+
await sleep(holdBeforeMoveMs);
|
|
989
|
+
// Wiggle toward the destination so Chrome's native drag system
|
|
990
|
+
// crosses its activation threshold. Direction matters for libs
|
|
991
|
+
// that have direction-sensitive activation constraints.
|
|
992
|
+
const dx = toX - fromX;
|
|
993
|
+
const dy = toY - fromY;
|
|
994
|
+
const len = Math.hypot(dx, dy) || 1;
|
|
995
|
+
const wx = fromX + (dx / len) * WIGGLE_PX;
|
|
996
|
+
const wy = fromY + (dy / len) * WIGGLE_PX;
|
|
997
|
+
await moveHeld(wx, wy);
|
|
998
|
+
const intercepted = await interceptPromise;
|
|
999
|
+
if (intercepted) {
|
|
1000
|
+
dragMode = 'html5';
|
|
1001
|
+
interceptReceived = true;
|
|
1002
|
+
eventsFired.push('Input.dragIntercepted');
|
|
1003
|
+
const dragData = intercepted.data;
|
|
1004
|
+
for (let i = 1; i <= steps; i++) {
|
|
1005
|
+
const t = i / steps;
|
|
1006
|
+
const { x, y } = interpolate(t);
|
|
1007
|
+
await moveHeld(x, y);
|
|
1008
|
+
await cdp(tabId, 'Input.dispatchDragEvent', {
|
|
1009
|
+
type: 'dragOver',
|
|
1010
|
+
x,
|
|
1011
|
+
y,
|
|
1012
|
+
data: dragData,
|
|
1013
|
+
});
|
|
1014
|
+
if (i === 1)
|
|
1015
|
+
eventsFired.push('dragOver');
|
|
1016
|
+
if (stepDelayMs > 0)
|
|
1017
|
+
await sleep(stepDelayMs);
|
|
1018
|
+
}
|
|
1019
|
+
await cdp(tabId, 'Input.dispatchDragEvent', {
|
|
1020
|
+
type: 'dragEnter',
|
|
1021
|
+
x: toX,
|
|
1022
|
+
y: toY,
|
|
1023
|
+
data: dragData,
|
|
1024
|
+
});
|
|
1025
|
+
eventsFired.push('dragEnter');
|
|
1026
|
+
await cdp(tabId, 'Input.dispatchDragEvent', {
|
|
1027
|
+
type: 'dragOver',
|
|
1028
|
+
x: toX,
|
|
1029
|
+
y: toY,
|
|
1030
|
+
data: dragData,
|
|
1031
|
+
});
|
|
1032
|
+
if (holdBeforeReleaseMs > 0)
|
|
1033
|
+
await sleep(holdBeforeReleaseMs);
|
|
1034
|
+
await cdp(tabId, 'Input.dispatchDragEvent', {
|
|
1035
|
+
type: 'drop',
|
|
1036
|
+
x: toX,
|
|
1037
|
+
y: toY,
|
|
1038
|
+
data: dragData,
|
|
1039
|
+
});
|
|
1040
|
+
eventsFired.push('drop');
|
|
1041
|
+
await cdp(tabId, 'Input.dispatchMouseEvent', {
|
|
1042
|
+
type: 'mouseReleased',
|
|
1043
|
+
x: toX,
|
|
1044
|
+
y: toY,
|
|
1045
|
+
button: 'left',
|
|
1046
|
+
clickCount: 1,
|
|
1047
|
+
});
|
|
1048
|
+
}
|
|
1049
|
+
else {
|
|
1050
|
+
// Element was tagged draggable but no native drag fired — the page
|
|
1051
|
+
// either preventDefault'd dragstart or wires its own pointer logic.
|
|
1052
|
+
// Continue with pointer-only events from where we already pressed.
|
|
1053
|
+
dragMode = 'pointer';
|
|
1054
|
+
for (let i = 1; i <= steps; i++) {
|
|
1055
|
+
const t = i / steps;
|
|
1056
|
+
const { x, y } = interpolate(t);
|
|
1057
|
+
await moveHeld(x, y);
|
|
1058
|
+
if (stepDelayMs > 0)
|
|
1059
|
+
await sleep(stepDelayMs);
|
|
1060
|
+
}
|
|
1061
|
+
if (holdBeforeReleaseMs > 0)
|
|
1062
|
+
await sleep(holdBeforeReleaseMs);
|
|
1063
|
+
await cdp(tabId, 'Input.dispatchMouseEvent', {
|
|
1064
|
+
type: 'mouseReleased',
|
|
1065
|
+
x: toX,
|
|
1066
|
+
y: toY,
|
|
1067
|
+
button: 'left',
|
|
1068
|
+
clickCount: 1,
|
|
1069
|
+
});
|
|
1070
|
+
}
|
|
1071
|
+
}
|
|
1072
|
+
else {
|
|
1073
|
+
// Pointer-only branch: no native drag involvement at all.
|
|
1074
|
+
dragMode = 'pointer';
|
|
1075
|
+
await cdp(tabId, 'Input.dispatchMouseEvent', {
|
|
1076
|
+
type: 'mouseMoved',
|
|
1077
|
+
x: fromX,
|
|
1078
|
+
y: fromY,
|
|
1079
|
+
});
|
|
1080
|
+
await cdp(tabId, 'Input.dispatchMouseEvent', {
|
|
1081
|
+
type: 'mousePressed',
|
|
1082
|
+
x: fromX,
|
|
1083
|
+
y: fromY,
|
|
1084
|
+
button: 'left',
|
|
1085
|
+
clickCount: 1,
|
|
1086
|
+
});
|
|
1087
|
+
if (holdBeforeMoveMs > 0)
|
|
1088
|
+
await sleep(holdBeforeMoveMs);
|
|
1089
|
+
for (let i = 1; i <= steps; i++) {
|
|
1090
|
+
const t = i / steps;
|
|
1091
|
+
const { x, y } = interpolate(t);
|
|
1092
|
+
await moveHeld(x, y);
|
|
1093
|
+
if (stepDelayMs > 0)
|
|
1094
|
+
await sleep(stepDelayMs);
|
|
1095
|
+
}
|
|
1096
|
+
if (holdBeforeReleaseMs > 0)
|
|
1097
|
+
await sleep(holdBeforeReleaseMs);
|
|
1098
|
+
await cdp(tabId, 'Input.dispatchMouseEvent', {
|
|
1099
|
+
type: 'mouseReleased',
|
|
1100
|
+
x: toX,
|
|
1101
|
+
y: toY,
|
|
1102
|
+
button: 'left',
|
|
1103
|
+
clickCount: 1,
|
|
1104
|
+
});
|
|
1105
|
+
}
|
|
1106
|
+
}
|
|
1107
|
+
finally {
|
|
1108
|
+
// Critical: leaving interception on would block the human user from
|
|
1109
|
+
// dragging anything in this tab. Best-effort cleanup, swallow errors.
|
|
1110
|
+
if (interceptionEnabled) {
|
|
1111
|
+
try {
|
|
1112
|
+
await cdp(tabId, 'Input.setInterceptDrags', { enabled: false });
|
|
1113
|
+
}
|
|
1114
|
+
catch {
|
|
1115
|
+
// ignore
|
|
1116
|
+
}
|
|
1117
|
+
}
|
|
1118
|
+
}
|
|
1119
|
+
return {
|
|
1120
|
+
ok: true,
|
|
1121
|
+
result: {
|
|
1122
|
+
from: { x: fromX, y: fromY, selector: fromSelector ?? null },
|
|
1123
|
+
to: { x: toX, y: toY, selector: toSelector ?? null },
|
|
1124
|
+
duration_ms_actual: Date.now() - dragStart,
|
|
1125
|
+
drag_mode: dragMode,
|
|
1126
|
+
interception_attempted: interceptionEnabled,
|
|
1127
|
+
intercept_received: interceptReceived,
|
|
1128
|
+
events_fired: eventsFired,
|
|
1129
|
+
},
|
|
1130
|
+
};
|
|
1131
|
+
}
|
|
1132
|
+
catch (err) {
|
|
1133
|
+
// The pre-extraction `case 'drag'` let thrown errors bubble to
|
|
1134
|
+
// `handleTool`'s outer catch, which produced this exact same
|
|
1135
|
+
// `err.message` string — converting here instead keeps the standalone
|
|
1136
|
+
// wire behavior identical while giving `runFlow` the `ActionOutcome`
|
|
1137
|
+
// shape it expects from every other perform* function.
|
|
1138
|
+
return { ok: false, error: err instanceof Error ? err.message : String(err) };
|
|
1139
|
+
}
|
|
1140
|
+
}
|
|
867
1141
|
/**
|
|
868
1142
|
* `handlePing` through `handleSetPermission` — the extracted bodies of the
|
|
869
1143
|
* remaining standalone tool cases in `handleTool` below that never got the
|
|
@@ -960,6 +1234,7 @@ async function handleFlow(tabId, state, msg, p) {
|
|
|
960
1234
|
performType: (params) => performType(tabId, params),
|
|
961
1235
|
performPress: (params) => performPress(tabId, params),
|
|
962
1236
|
performWaitFor: (params) => performWaitFor(tabId, state, params),
|
|
1237
|
+
performDrag: (params) => performDrag(tabId, params),
|
|
963
1238
|
buildRecoverySnapshot: () => evaluateInTab(tabId, buildSnapshotJs({ only_interactive: true, max_interactive: 40 })),
|
|
964
1239
|
});
|
|
965
1240
|
// The wire-level response is ok:true whenever the flow RAN (even a
|
|
@@ -969,285 +1244,29 @@ async function handleFlow(tabId, state, msg, p) {
|
|
|
969
1244
|
return { kind: 'tool.response', id: msg.id, ok: true, result: flowResult };
|
|
970
1245
|
}
|
|
971
1246
|
async function handleDrag(tabId, msg, p, optStr) {
|
|
1247
|
+
// Thin wire wrapper over performDrag — the same pattern the standalone
|
|
1248
|
+
// click/type/press/wait_for cases use over their perform* functions.
|
|
1249
|
+
// Numbers pass through with a plain typeof check; performDrag applies
|
|
1250
|
+
// the full finite/non-negative guard and the duration/hold clamps.
|
|
972
1251
|
const optNum = (key) => {
|
|
973
1252
|
const v = p[key];
|
|
974
|
-
return typeof v === 'number'
|
|
1253
|
+
return typeof v === 'number' ? v : undefined;
|
|
975
1254
|
};
|
|
976
|
-
const
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
const durationMs = clamp(optNum('duration_ms') ?? 1500, MAX_DRAG_DURATION_MS);
|
|
987
|
-
const holdBeforeMoveMs = clamp(optNum('hold_before_move_ms') ?? 0, MAX_DRAG_HOLD_MS);
|
|
988
|
-
const holdBeforeReleaseMs = clamp(optNum('hold_before_release_ms') ?? 0, MAX_DRAG_HOLD_MS);
|
|
989
|
-
const hasFromCoords = fromXRaw !== undefined && fromYRaw !== undefined;
|
|
990
|
-
const hasToCoords = toXRaw !== undefined && toYRaw !== undefined;
|
|
991
|
-
if (!fromSelector && !hasFromCoords) {
|
|
992
|
-
return {
|
|
993
|
-
kind: 'tool.response',
|
|
994
|
-
id: msg.id,
|
|
995
|
-
ok: false,
|
|
996
|
-
error: 'drag: provide from_selector or both from_x and from_y',
|
|
997
|
-
};
|
|
998
|
-
}
|
|
999
|
-
if (!toSelector && !hasToCoords) {
|
|
1000
|
-
return {
|
|
1001
|
-
kind: 'tool.response',
|
|
1002
|
-
id: msg.id,
|
|
1003
|
-
ok: false,
|
|
1004
|
-
error: 'drag: provide to_selector or both to_x and to_y',
|
|
1005
|
-
};
|
|
1006
|
-
}
|
|
1007
|
-
const probeExpr = buildDragProbeJs({
|
|
1008
|
-
from_selector: fromSelector,
|
|
1009
|
-
to_selector: toSelector,
|
|
1010
|
-
from_x: fromXRaw,
|
|
1011
|
-
from_y: fromYRaw,
|
|
1012
|
-
to_x: toXRaw,
|
|
1013
|
-
to_y: toYRaw,
|
|
1255
|
+
const outcome = await performDrag(tabId, {
|
|
1256
|
+
from_selector: optStr('from_selector'),
|
|
1257
|
+
from_x: optNum('from_x'),
|
|
1258
|
+
from_y: optNum('from_y'),
|
|
1259
|
+
to_selector: optStr('to_selector'),
|
|
1260
|
+
to_x: optNum('to_x'),
|
|
1261
|
+
to_y: optNum('to_y'),
|
|
1262
|
+
duration_ms: optNum('duration_ms'),
|
|
1263
|
+
hold_before_move_ms: optNum('hold_before_move_ms'),
|
|
1264
|
+
hold_before_release_ms: optNum('hold_before_release_ms'),
|
|
1014
1265
|
});
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
return { kind: 'tool.response', id: msg.id, ok: false, error: `drag: ${probe.err}` };
|
|
1266
|
+
if (!outcome.ok) {
|
|
1267
|
+
return { kind: 'tool.response', id: msg.id, ok: false, error: outcome.error };
|
|
1018
1268
|
}
|
|
1019
|
-
|
|
1020
|
-
return {
|
|
1021
|
-
kind: 'tool.response',
|
|
1022
|
-
id: msg.id,
|
|
1023
|
-
ok: false,
|
|
1024
|
-
error: 'drag: could not resolve coordinates',
|
|
1025
|
-
};
|
|
1026
|
-
}
|
|
1027
|
-
if (!probe.from.in_viewport) {
|
|
1028
|
-
return {
|
|
1029
|
-
kind: 'tool.response',
|
|
1030
|
-
id: msg.id,
|
|
1031
|
-
ok: false,
|
|
1032
|
-
error: 'drag: source point is offscreen — scroll first or pass viewport coords',
|
|
1033
|
-
};
|
|
1034
|
-
}
|
|
1035
|
-
if (!probe.to.in_viewport) {
|
|
1036
|
-
return {
|
|
1037
|
-
kind: 'tool.response',
|
|
1038
|
-
id: msg.id,
|
|
1039
|
-
ok: false,
|
|
1040
|
-
error: 'drag: destination point is offscreen — scroll first or pass viewport coords',
|
|
1041
|
-
};
|
|
1042
|
-
}
|
|
1043
|
-
const fromX = probe.from.x;
|
|
1044
|
-
const fromY = probe.from.y;
|
|
1045
|
-
const toX = probe.to.x;
|
|
1046
|
-
const toY = probe.to.y;
|
|
1047
|
-
const isDraggable = !!probe.draggable;
|
|
1048
|
-
// ~30fps interpolation; minimum 2 steps so the path actually has a midpoint.
|
|
1049
|
-
const steps = durationMs > 0 ? Math.max(2, Math.round(durationMs / 33)) : 1;
|
|
1050
|
-
const stepDelayMs = steps > 0 ? durationMs / steps : 0;
|
|
1051
|
-
const eventsFired = [];
|
|
1052
|
-
// Every branch below reassigns `dragMode` before the final `return` reads it.
|
|
1053
|
-
let dragMode = 'pointer'; // eslint-disable-line no-useless-assignment -- see comment above
|
|
1054
|
-
let interceptReceived = false;
|
|
1055
|
-
const dragStart = Date.now();
|
|
1056
|
-
let interceptionEnabled = false;
|
|
1057
|
-
const interpolate = (t) => ({
|
|
1058
|
-
x: fromX + (toX - fromX) * t,
|
|
1059
|
-
y: fromY + (toY - fromY) * t,
|
|
1060
|
-
});
|
|
1061
|
-
// Mouse move WITH the left button held down. Chrome's HTML5 drag
|
|
1062
|
-
// system only treats movement as a drag when the button state is
|
|
1063
|
-
// signalled on every move after mousePressed; omitting it makes
|
|
1064
|
-
// Blink ignore the move for drag-detection purposes.
|
|
1065
|
-
const moveHeld = (x, y) => cdp(tabId, 'Input.dispatchMouseEvent', {
|
|
1066
|
-
type: 'mouseMoved',
|
|
1067
|
-
x,
|
|
1068
|
-
y,
|
|
1069
|
-
button: 'left',
|
|
1070
|
-
buttons: 1,
|
|
1071
|
-
});
|
|
1072
|
-
// Wiggle distance has to comfortably clear `kDragThreshold` (~5px on
|
|
1073
|
-
// Mac, 4px on Linux/Windows). 5px landed *right* at the boundary and
|
|
1074
|
-
// Chrome did not start the drag — 20px crosses it on every platform.
|
|
1075
|
-
const WIGGLE_PX = 20;
|
|
1076
|
-
// Time to wait for Input.dragIntercepted after the wiggle. 120ms was
|
|
1077
|
-
// enough on local-only synthetic tests but flaky in real Chrome —
|
|
1078
|
-
// 250ms is comfortable without dragging out the overall latency.
|
|
1079
|
-
const INTERCEPT_TIMEOUT_MS = 250;
|
|
1080
|
-
try {
|
|
1081
|
-
if (isDraggable) {
|
|
1082
|
-
try {
|
|
1083
|
-
await cdp(tabId, 'Input.setInterceptDrags', { enabled: true });
|
|
1084
|
-
interceptionEnabled = true;
|
|
1085
|
-
}
|
|
1086
|
-
catch {
|
|
1087
|
-
// setInterceptDrags is experimental — fall back to pointer mode silently.
|
|
1088
|
-
}
|
|
1089
|
-
}
|
|
1090
|
-
if (interceptionEnabled) {
|
|
1091
|
-
// Arm the listener BEFORE the press+wiggle that may trigger it.
|
|
1092
|
-
const interceptPromise = waitForCdpEvent(tabId, 'Input.dragIntercepted', INTERCEPT_TIMEOUT_MS);
|
|
1093
|
-
await cdp(tabId, 'Input.dispatchMouseEvent', {
|
|
1094
|
-
type: 'mouseMoved',
|
|
1095
|
-
x: fromX,
|
|
1096
|
-
y: fromY,
|
|
1097
|
-
});
|
|
1098
|
-
await cdp(tabId, 'Input.dispatchMouseEvent', {
|
|
1099
|
-
type: 'mousePressed',
|
|
1100
|
-
x: fromX,
|
|
1101
|
-
y: fromY,
|
|
1102
|
-
button: 'left',
|
|
1103
|
-
clickCount: 1,
|
|
1104
|
-
});
|
|
1105
|
-
if (holdBeforeMoveMs > 0)
|
|
1106
|
-
await sleep(holdBeforeMoveMs);
|
|
1107
|
-
// Wiggle toward the destination so Chrome's native drag system
|
|
1108
|
-
// crosses its activation threshold. Direction matters for libs
|
|
1109
|
-
// that have direction-sensitive activation constraints.
|
|
1110
|
-
const dx = toX - fromX;
|
|
1111
|
-
const dy = toY - fromY;
|
|
1112
|
-
const len = Math.hypot(dx, dy) || 1;
|
|
1113
|
-
const wx = fromX + (dx / len) * WIGGLE_PX;
|
|
1114
|
-
const wy = fromY + (dy / len) * WIGGLE_PX;
|
|
1115
|
-
await moveHeld(wx, wy);
|
|
1116
|
-
const intercepted = await interceptPromise;
|
|
1117
|
-
if (intercepted) {
|
|
1118
|
-
dragMode = 'html5';
|
|
1119
|
-
interceptReceived = true;
|
|
1120
|
-
eventsFired.push('Input.dragIntercepted');
|
|
1121
|
-
const dragData = intercepted.data;
|
|
1122
|
-
for (let i = 1; i <= steps; i++) {
|
|
1123
|
-
const t = i / steps;
|
|
1124
|
-
const { x, y } = interpolate(t);
|
|
1125
|
-
await moveHeld(x, y);
|
|
1126
|
-
await cdp(tabId, 'Input.dispatchDragEvent', {
|
|
1127
|
-
type: 'dragOver',
|
|
1128
|
-
x,
|
|
1129
|
-
y,
|
|
1130
|
-
data: dragData,
|
|
1131
|
-
});
|
|
1132
|
-
if (i === 1)
|
|
1133
|
-
eventsFired.push('dragOver');
|
|
1134
|
-
if (stepDelayMs > 0)
|
|
1135
|
-
await sleep(stepDelayMs);
|
|
1136
|
-
}
|
|
1137
|
-
await cdp(tabId, 'Input.dispatchDragEvent', {
|
|
1138
|
-
type: 'dragEnter',
|
|
1139
|
-
x: toX,
|
|
1140
|
-
y: toY,
|
|
1141
|
-
data: dragData,
|
|
1142
|
-
});
|
|
1143
|
-
eventsFired.push('dragEnter');
|
|
1144
|
-
await cdp(tabId, 'Input.dispatchDragEvent', {
|
|
1145
|
-
type: 'dragOver',
|
|
1146
|
-
x: toX,
|
|
1147
|
-
y: toY,
|
|
1148
|
-
data: dragData,
|
|
1149
|
-
});
|
|
1150
|
-
if (holdBeforeReleaseMs > 0)
|
|
1151
|
-
await sleep(holdBeforeReleaseMs);
|
|
1152
|
-
await cdp(tabId, 'Input.dispatchDragEvent', {
|
|
1153
|
-
type: 'drop',
|
|
1154
|
-
x: toX,
|
|
1155
|
-
y: toY,
|
|
1156
|
-
data: dragData,
|
|
1157
|
-
});
|
|
1158
|
-
eventsFired.push('drop');
|
|
1159
|
-
await cdp(tabId, 'Input.dispatchMouseEvent', {
|
|
1160
|
-
type: 'mouseReleased',
|
|
1161
|
-
x: toX,
|
|
1162
|
-
y: toY,
|
|
1163
|
-
button: 'left',
|
|
1164
|
-
clickCount: 1,
|
|
1165
|
-
});
|
|
1166
|
-
}
|
|
1167
|
-
else {
|
|
1168
|
-
// Element was tagged draggable but no native drag fired — the page
|
|
1169
|
-
// either preventDefault'd dragstart or wires its own pointer logic.
|
|
1170
|
-
// Continue with pointer-only events from where we already pressed.
|
|
1171
|
-
dragMode = 'pointer';
|
|
1172
|
-
for (let i = 1; i <= steps; i++) {
|
|
1173
|
-
const t = i / steps;
|
|
1174
|
-
const { x, y } = interpolate(t);
|
|
1175
|
-
await moveHeld(x, y);
|
|
1176
|
-
if (stepDelayMs > 0)
|
|
1177
|
-
await sleep(stepDelayMs);
|
|
1178
|
-
}
|
|
1179
|
-
if (holdBeforeReleaseMs > 0)
|
|
1180
|
-
await sleep(holdBeforeReleaseMs);
|
|
1181
|
-
await cdp(tabId, 'Input.dispatchMouseEvent', {
|
|
1182
|
-
type: 'mouseReleased',
|
|
1183
|
-
x: toX,
|
|
1184
|
-
y: toY,
|
|
1185
|
-
button: 'left',
|
|
1186
|
-
clickCount: 1,
|
|
1187
|
-
});
|
|
1188
|
-
}
|
|
1189
|
-
}
|
|
1190
|
-
else {
|
|
1191
|
-
// Pointer-only branch: no native drag involvement at all.
|
|
1192
|
-
dragMode = 'pointer';
|
|
1193
|
-
await cdp(tabId, 'Input.dispatchMouseEvent', {
|
|
1194
|
-
type: 'mouseMoved',
|
|
1195
|
-
x: fromX,
|
|
1196
|
-
y: fromY,
|
|
1197
|
-
});
|
|
1198
|
-
await cdp(tabId, 'Input.dispatchMouseEvent', {
|
|
1199
|
-
type: 'mousePressed',
|
|
1200
|
-
x: fromX,
|
|
1201
|
-
y: fromY,
|
|
1202
|
-
button: 'left',
|
|
1203
|
-
clickCount: 1,
|
|
1204
|
-
});
|
|
1205
|
-
if (holdBeforeMoveMs > 0)
|
|
1206
|
-
await sleep(holdBeforeMoveMs);
|
|
1207
|
-
for (let i = 1; i <= steps; i++) {
|
|
1208
|
-
const t = i / steps;
|
|
1209
|
-
const { x, y } = interpolate(t);
|
|
1210
|
-
await moveHeld(x, y);
|
|
1211
|
-
if (stepDelayMs > 0)
|
|
1212
|
-
await sleep(stepDelayMs);
|
|
1213
|
-
}
|
|
1214
|
-
if (holdBeforeReleaseMs > 0)
|
|
1215
|
-
await sleep(holdBeforeReleaseMs);
|
|
1216
|
-
await cdp(tabId, 'Input.dispatchMouseEvent', {
|
|
1217
|
-
type: 'mouseReleased',
|
|
1218
|
-
x: toX,
|
|
1219
|
-
y: toY,
|
|
1220
|
-
button: 'left',
|
|
1221
|
-
clickCount: 1,
|
|
1222
|
-
});
|
|
1223
|
-
}
|
|
1224
|
-
}
|
|
1225
|
-
finally {
|
|
1226
|
-
// Critical: leaving interception on would block the human user from
|
|
1227
|
-
// dragging anything in this tab. Best-effort cleanup, swallow errors.
|
|
1228
|
-
if (interceptionEnabled) {
|
|
1229
|
-
try {
|
|
1230
|
-
await cdp(tabId, 'Input.setInterceptDrags', { enabled: false });
|
|
1231
|
-
}
|
|
1232
|
-
catch {
|
|
1233
|
-
// ignore
|
|
1234
|
-
}
|
|
1235
|
-
}
|
|
1236
|
-
}
|
|
1237
|
-
return {
|
|
1238
|
-
kind: 'tool.response',
|
|
1239
|
-
id: msg.id,
|
|
1240
|
-
ok: true,
|
|
1241
|
-
result: {
|
|
1242
|
-
from: { x: fromX, y: fromY, selector: fromSelector ?? null },
|
|
1243
|
-
to: { x: toX, y: toY, selector: toSelector ?? null },
|
|
1244
|
-
duration_ms_actual: Date.now() - dragStart,
|
|
1245
|
-
drag_mode: dragMode,
|
|
1246
|
-
interception_attempted: interceptionEnabled,
|
|
1247
|
-
intercept_received: interceptReceived,
|
|
1248
|
-
events_fired: eventsFired,
|
|
1249
|
-
},
|
|
1250
|
-
};
|
|
1269
|
+
return { kind: 'tool.response', id: msg.id, ok: true, result: outcome.result };
|
|
1251
1270
|
}
|
|
1252
1271
|
async function handleDialogRespond(tabId, msg, p, optStr) {
|
|
1253
1272
|
const accept = p.accept === true;
|