@humanjs/playwright 0.4.0 → 0.5.0
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 +109 -2
- package/dist/index.cjs +554 -207
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +164 -2
- package/dist/index.d.ts +164 -2
- package/dist/index.js +554 -207
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
package/dist/index.cjs
CHANGED
|
@@ -68,6 +68,73 @@ async function dispatchKey(page, key) {
|
|
|
68
68
|
await page.keyboard.insertText(key);
|
|
69
69
|
}
|
|
70
70
|
}
|
|
71
|
+
async function executePaste(target, value, ctx) {
|
|
72
|
+
if (value.length === 0) return { characters: 0 };
|
|
73
|
+
const locator = typeof target === "string" ? ctx.page.locator(target) : target;
|
|
74
|
+
await locator.focus();
|
|
75
|
+
await ctx.page.keyboard.insertText(value);
|
|
76
|
+
return { characters: value.length };
|
|
77
|
+
}
|
|
78
|
+
async function executePress(key, ctx) {
|
|
79
|
+
const dispatched = resolveChord(key);
|
|
80
|
+
await ctx.page.keyboard.press(dispatched);
|
|
81
|
+
return { dispatched };
|
|
82
|
+
}
|
|
83
|
+
function resolveChord(key) {
|
|
84
|
+
const parts = key.split("+").map((p) => p.trim()).filter((p) => p.length > 0);
|
|
85
|
+
if (parts.length === 0) {
|
|
86
|
+
throw new Error(`Invalid key: ${JSON.stringify(key)} \u2014 empty or only separators`);
|
|
87
|
+
}
|
|
88
|
+
const keyToken = parts[parts.length - 1];
|
|
89
|
+
if (keyToken === void 0) {
|
|
90
|
+
throw new Error(`Invalid key: ${JSON.stringify(key)} \u2014 missing key`);
|
|
91
|
+
}
|
|
92
|
+
const modifierTokens = parts.slice(0, -1);
|
|
93
|
+
const modifiers = [];
|
|
94
|
+
for (const token of modifierTokens) {
|
|
95
|
+
const resolved = resolveModifier(token);
|
|
96
|
+
if (resolved === null) {
|
|
97
|
+
throw new Error(
|
|
98
|
+
`Invalid key modifier: ${JSON.stringify(token)} in ${JSON.stringify(key)}. Use one of: Mod/CmdOrCtrl/CommandOrControl, Cmd/Command/Meta/Win/Super, Ctrl/Control, Alt/Option/Opt, Shift.`
|
|
99
|
+
);
|
|
100
|
+
}
|
|
101
|
+
modifiers.push(resolved);
|
|
102
|
+
}
|
|
103
|
+
return [...modifiers, normalizeKey(keyToken)].join("+");
|
|
104
|
+
}
|
|
105
|
+
function resolveModifier(token) {
|
|
106
|
+
const lower = token.toLowerCase();
|
|
107
|
+
switch (lower) {
|
|
108
|
+
case "mod":
|
|
109
|
+
case "cmdorctrl":
|
|
110
|
+
case "commandorcontrol":
|
|
111
|
+
return isMac() ? "Meta" : "Control";
|
|
112
|
+
case "cmd":
|
|
113
|
+
case "command":
|
|
114
|
+
case "meta":
|
|
115
|
+
case "win":
|
|
116
|
+
case "super":
|
|
117
|
+
return "Meta";
|
|
118
|
+
case "ctrl":
|
|
119
|
+
case "control":
|
|
120
|
+
return "Control";
|
|
121
|
+
case "alt":
|
|
122
|
+
case "option":
|
|
123
|
+
case "opt":
|
|
124
|
+
return "Alt";
|
|
125
|
+
case "shift":
|
|
126
|
+
return "Shift";
|
|
127
|
+
default:
|
|
128
|
+
return null;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
function normalizeKey(key) {
|
|
132
|
+
if (key.length === 1) return key.toUpperCase();
|
|
133
|
+
return key.charAt(0).toUpperCase() + key.slice(1);
|
|
134
|
+
}
|
|
135
|
+
function isMac() {
|
|
136
|
+
return process.platform === "darwin";
|
|
137
|
+
}
|
|
71
138
|
|
|
72
139
|
// src/internal/mouse-walk.ts
|
|
73
140
|
async function walkMouseAlongPath(page, path, durationMs) {
|
|
@@ -82,31 +149,191 @@ async function walkMouseAlongPath(page, path, durationMs) {
|
|
|
82
149
|
}
|
|
83
150
|
}
|
|
84
151
|
}
|
|
152
|
+
var RESERVED_TARGETS = /* @__PURE__ */ new Set(["natural", "end", "top"]);
|
|
153
|
+
async function executeScroll(target, ctx, options = {}) {
|
|
154
|
+
const { page, personality, rng, speed } = ctx;
|
|
155
|
+
const speedFactor = speedModeFactor(speed);
|
|
156
|
+
const axis = options.axis ?? "y";
|
|
157
|
+
const container = resolveWithin(options.within, ctx);
|
|
158
|
+
const geom = container ? await readContainerGeometry(container, axis) : await readWindowGeometry(page, axis);
|
|
159
|
+
if (!geom) {
|
|
160
|
+
return { from: 0, to: 0, distance: 0, durationMs: 0 };
|
|
161
|
+
}
|
|
162
|
+
const from = geom.current;
|
|
163
|
+
const targetPos = await resolveTarget(target, ctx, geom, container, axis, options.block);
|
|
164
|
+
const to = clamp(targetPos, 0, Math.max(0, geom.total - geom.viewport));
|
|
165
|
+
const distance = to - from;
|
|
166
|
+
if (distance === 0) {
|
|
167
|
+
return { from, to, distance: 0, durationMs: 0 };
|
|
168
|
+
}
|
|
169
|
+
if (speed === "instant") {
|
|
170
|
+
if (container) {
|
|
171
|
+
await container.evaluate(
|
|
172
|
+
(el, args) => {
|
|
173
|
+
const a = args;
|
|
174
|
+
if (a.axis === "x") el.scrollTo(a.pos, el.scrollTop);
|
|
175
|
+
else el.scrollTo(el.scrollLeft, a.pos);
|
|
176
|
+
},
|
|
177
|
+
{ axis, pos: to }
|
|
178
|
+
);
|
|
179
|
+
} else {
|
|
180
|
+
await page.evaluate(
|
|
181
|
+
(args) => {
|
|
182
|
+
if (args.axis === "x") window.scrollTo(args.pos, window.scrollY);
|
|
183
|
+
else window.scrollTo(window.scrollX, args.pos);
|
|
184
|
+
},
|
|
185
|
+
{ axis, pos: to }
|
|
186
|
+
);
|
|
187
|
+
}
|
|
188
|
+
return { from, to, distance, durationMs: 0 };
|
|
189
|
+
}
|
|
190
|
+
const segments = core.planScroll(from, to, personality.scroll, rng, {
|
|
191
|
+
forceOvershoot: options.overshoot,
|
|
192
|
+
withPauses: options.withPauses,
|
|
193
|
+
personalitySpeed: personality.speed,
|
|
194
|
+
speedFactor
|
|
195
|
+
});
|
|
196
|
+
if (container && geom.hover) {
|
|
197
|
+
await page.mouse.move(geom.hover.x, geom.hover.y);
|
|
198
|
+
}
|
|
199
|
+
const startedAt = Date.now();
|
|
200
|
+
await walkSegments(page, segments, axis, container);
|
|
201
|
+
const durationMs = Date.now() - startedAt;
|
|
202
|
+
return { from, to, distance, durationMs };
|
|
203
|
+
}
|
|
204
|
+
function resolveWithin(within, ctx) {
|
|
205
|
+
if (!within) return null;
|
|
206
|
+
return typeof within === "string" ? ctx.page.locator(within) : within;
|
|
207
|
+
}
|
|
208
|
+
async function readWindowGeometry(page, axis) {
|
|
209
|
+
const g = await page.evaluate((a) => {
|
|
210
|
+
if (a === "x") {
|
|
211
|
+
return {
|
|
212
|
+
current: window.scrollX,
|
|
213
|
+
viewport: window.innerWidth,
|
|
214
|
+
total: Math.max(document.documentElement.scrollWidth, document.body?.scrollWidth ?? 0)
|
|
215
|
+
};
|
|
216
|
+
}
|
|
217
|
+
return {
|
|
218
|
+
current: window.scrollY,
|
|
219
|
+
viewport: window.innerHeight,
|
|
220
|
+
total: Math.max(document.documentElement.scrollHeight, document.body?.scrollHeight ?? 0)
|
|
221
|
+
};
|
|
222
|
+
}, axis);
|
|
223
|
+
return { current: g.current, viewport: g.viewport, total: g.total };
|
|
224
|
+
}
|
|
225
|
+
async function readContainerGeometry(container, axis) {
|
|
226
|
+
return container.evaluate((el, a) => {
|
|
227
|
+
const rect = el.getBoundingClientRect();
|
|
228
|
+
const isX = a === "x";
|
|
229
|
+
return {
|
|
230
|
+
current: isX ? el.scrollLeft : el.scrollTop,
|
|
231
|
+
viewport: isX ? el.clientWidth : el.clientHeight,
|
|
232
|
+
total: isX ? el.scrollWidth : el.scrollHeight,
|
|
233
|
+
hover: {
|
|
234
|
+
x: rect.left + rect.width / 2,
|
|
235
|
+
y: rect.top + rect.height / 2
|
|
236
|
+
}
|
|
237
|
+
};
|
|
238
|
+
}, axis).catch(() => null);
|
|
239
|
+
}
|
|
240
|
+
async function resolveTarget(target, ctx, geom, container, axis, block = "start") {
|
|
241
|
+
if (target === void 0 || target === "natural") return geom.current + geom.viewport;
|
|
242
|
+
if (target === "end") return geom.total;
|
|
243
|
+
if (target === "top") return 0;
|
|
244
|
+
if (typeof target === "object" && "by" in target) return geom.current + target.by;
|
|
245
|
+
if (typeof target === "object" && "to" in target) return target.to;
|
|
246
|
+
const elementLocator = typeof target === "string" && !RESERVED_TARGETS.has(target) ? ctx.page.locator(target) : typeof target === "string" ? null : target;
|
|
247
|
+
if (!elementLocator) return geom.current + geom.viewport;
|
|
248
|
+
return container ? resolveElementWithinContainer(elementLocator, container, geom, axis, block) : resolveElementInWindow(elementLocator, geom, axis, block);
|
|
249
|
+
}
|
|
250
|
+
async function resolveElementInWindow(elementLocator, geom, axis, block) {
|
|
251
|
+
const rect = await elementLocator.boundingBox().catch(() => null);
|
|
252
|
+
if (!rect) return geom.current;
|
|
253
|
+
const relStart = axis === "x" ? rect.x : rect.y;
|
|
254
|
+
const length = axis === "x" ? rect.width : rect.height;
|
|
255
|
+
const absoluteStart = geom.current + relStart;
|
|
256
|
+
const absoluteEnd = absoluteStart + length;
|
|
257
|
+
if (block === "start") return absoluteStart;
|
|
258
|
+
if (block === "end") return absoluteEnd - geom.viewport;
|
|
259
|
+
if (block === "nearest") {
|
|
260
|
+
if (relStart >= 0 && relStart + length <= geom.viewport) return geom.current;
|
|
261
|
+
if (relStart < 0) return absoluteStart;
|
|
262
|
+
return absoluteEnd - geom.viewport;
|
|
263
|
+
}
|
|
264
|
+
return absoluteStart - (geom.viewport - length) / 2;
|
|
265
|
+
}
|
|
266
|
+
async function resolveElementWithinContainer(elementLocator, container, geom, axis, block) {
|
|
267
|
+
const rects = await container.evaluate(
|
|
268
|
+
(containerEl, args) => {
|
|
269
|
+
const elementEl = args.sel ? document.querySelector(args.sel) : null;
|
|
270
|
+
const targetEl = elementEl ?? containerEl.querySelector(":scope > *");
|
|
271
|
+
if (!targetEl) return null;
|
|
272
|
+
const cRect = containerEl.getBoundingClientRect();
|
|
273
|
+
const eRect = targetEl.getBoundingClientRect();
|
|
274
|
+
return args.axis === "x" ? { relStart: eRect.left - cRect.left, length: eRect.width } : { relStart: eRect.top - cRect.top, length: eRect.height };
|
|
275
|
+
},
|
|
276
|
+
{ sel: await locatorSelector(elementLocator), axis }
|
|
277
|
+
).catch(() => null);
|
|
278
|
+
if (!rects) return geom.current;
|
|
279
|
+
const offsetStart = rects.relStart + geom.current;
|
|
280
|
+
const offsetEnd = offsetStart + rects.length;
|
|
281
|
+
if (block === "start") return offsetStart;
|
|
282
|
+
if (block === "end") return offsetEnd - geom.viewport;
|
|
283
|
+
if (block === "nearest") {
|
|
284
|
+
if (rects.relStart >= 0 && rects.relStart + rects.length <= geom.viewport) {
|
|
285
|
+
return geom.current;
|
|
286
|
+
}
|
|
287
|
+
if (rects.relStart < 0) return offsetStart;
|
|
288
|
+
return offsetEnd - geom.viewport;
|
|
289
|
+
}
|
|
290
|
+
return offsetStart - (geom.viewport - rects.length) / 2;
|
|
291
|
+
}
|
|
292
|
+
async function locatorSelector(locator) {
|
|
293
|
+
const s = locator.toString?.();
|
|
294
|
+
if (typeof s !== "string") return null;
|
|
295
|
+
const match = /locator\(['"](.+?)['"]/.exec(s);
|
|
296
|
+
if (!match) return null;
|
|
297
|
+
const raw = match[1] ?? "";
|
|
298
|
+
const eq = raw.indexOf("=");
|
|
299
|
+
return eq > 0 && /^[a-z]+$/.test(raw.slice(0, eq)) ? raw.slice(eq + 1) : raw;
|
|
300
|
+
}
|
|
301
|
+
async function walkSegments(page, segments, axis, container) {
|
|
302
|
+
for (const segment of segments) {
|
|
303
|
+
if (segment.delayBeforeMs > 0) await sleep(segment.delayBeforeMs);
|
|
304
|
+
if (segment.delta === 0) continue;
|
|
305
|
+
if (container) {
|
|
306
|
+
await container.evaluate(
|
|
307
|
+
(el, args) => {
|
|
308
|
+
const a = args;
|
|
309
|
+
if (a.axis === "x") el.scrollLeft += a.delta;
|
|
310
|
+
else el.scrollTop += a.delta;
|
|
311
|
+
},
|
|
312
|
+
{ axis, delta: segment.delta }
|
|
313
|
+
);
|
|
314
|
+
} else if (axis === "x") {
|
|
315
|
+
await page.mouse.wheel(segment.delta, 0);
|
|
316
|
+
} else {
|
|
317
|
+
await page.mouse.wheel(0, segment.delta);
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
function clamp(value, min, max) {
|
|
322
|
+
return value < min ? min : value > max ? max : value;
|
|
323
|
+
}
|
|
85
324
|
|
|
86
325
|
// src/mouse/index.ts
|
|
87
|
-
async function executeClick(target, ctx) {
|
|
326
|
+
async function executeClick(target, ctx, options = {}) {
|
|
327
|
+
const button = options.button ?? "left";
|
|
88
328
|
const locator = typeof target === "string" ? ctx.page.locator(target) : target;
|
|
89
329
|
if (ctx.speed === "instant") {
|
|
90
|
-
const
|
|
91
|
-
await locator.click();
|
|
92
|
-
const center =
|
|
330
|
+
const box = await locator.boundingBox();
|
|
331
|
+
await locator.click({ button });
|
|
332
|
+
const center = box ? { x: box.x + box.width / 2, y: box.y + box.height / 2 } : ctx.getMousePosition();
|
|
93
333
|
ctx.setMousePosition(center);
|
|
94
334
|
return { target: center };
|
|
95
335
|
}
|
|
96
|
-
const
|
|
97
|
-
if (!box) {
|
|
98
|
-
throw new Error(
|
|
99
|
-
`Cannot click: element not found or has no bounding box (target: ${describeTarget(target)})`
|
|
100
|
-
);
|
|
101
|
-
}
|
|
102
|
-
const targetPoint = pickClickPoint(box, ctx.rng);
|
|
103
|
-
const startPoint = ctx.getMousePosition();
|
|
104
|
-
const rawPath = core.bezierPath(startPoint, targetPoint, ctx.rng, {
|
|
105
|
-
curvature: ctx.personality.mouse.curvature
|
|
106
|
-
});
|
|
107
|
-
const path = core.humanizePath(rawPath, ctx.rng);
|
|
108
|
-
const travelMs = computeTravelTime(path, ctx.personality, ctx.speed, ctx.rng);
|
|
109
|
-
await walkMouseAlongPath(ctx.page, path, travelMs);
|
|
336
|
+
const targetPoint = await moveToTarget(target, ctx, "click");
|
|
110
337
|
const preClickMs = computeDwellTime(
|
|
111
338
|
ctx.personality.dwell.preClickMs,
|
|
112
339
|
ctx.personality.dwell.preClickJitter,
|
|
@@ -116,7 +343,7 @@ async function executeClick(target, ctx) {
|
|
|
116
343
|
);
|
|
117
344
|
if (preClickMs > 0) await sleep(preClickMs);
|
|
118
345
|
ctx.setMousePosition(targetPoint);
|
|
119
|
-
await ctx.page.mouse.click(targetPoint.x, targetPoint.y);
|
|
346
|
+
await ctx.page.mouse.click(targetPoint.x, targetPoint.y, { button });
|
|
120
347
|
const postActionMs = computeDwellTime(
|
|
121
348
|
ctx.personality.dwell.postActionMs,
|
|
122
349
|
ctx.personality.dwell.postActionJitter,
|
|
@@ -127,11 +354,249 @@ async function executeClick(target, ctx) {
|
|
|
127
354
|
if (postActionMs > 0) await sleep(postActionMs);
|
|
128
355
|
return { target: targetPoint };
|
|
129
356
|
}
|
|
130
|
-
function
|
|
357
|
+
async function executeHover(target, ctx) {
|
|
358
|
+
if (ctx.speed === "instant") {
|
|
359
|
+
const box = await readBoxWithAutoScroll(target, ctx, "hover");
|
|
360
|
+
const center = { x: box.x + box.width / 2, y: box.y + box.height / 2 };
|
|
361
|
+
await ctx.page.mouse.move(center.x, center.y);
|
|
362
|
+
ctx.setMousePosition(center);
|
|
363
|
+
return { target: center };
|
|
364
|
+
}
|
|
365
|
+
const targetPoint = await moveToTarget(target, ctx, "hover");
|
|
366
|
+
const dwellMs = computeDwellTime(
|
|
367
|
+
ctx.personality.dwell.preClickMs,
|
|
368
|
+
ctx.personality.dwell.preClickJitter,
|
|
369
|
+
ctx.personality,
|
|
370
|
+
ctx.speed,
|
|
371
|
+
ctx.rng
|
|
372
|
+
);
|
|
373
|
+
if (dwellMs > 0) await sleep(dwellMs);
|
|
374
|
+
ctx.setMousePosition(targetPoint);
|
|
375
|
+
return { target: targetPoint };
|
|
376
|
+
}
|
|
377
|
+
async function executeDrag(from, to, ctx) {
|
|
378
|
+
const scrollYBeforeResolve = await readScrollY(ctx.page);
|
|
379
|
+
let { point: fromPoint, box: fromBox } = await resolveTargetPointAndBox(from, ctx, "drag");
|
|
380
|
+
let { point: toPoint, box: toBox } = await resolveTargetPointAndBox(to, ctx, "drag");
|
|
381
|
+
const resolveScrollDelta = await readScrollY(ctx.page) - scrollYBeforeResolve;
|
|
382
|
+
if (resolveScrollDelta !== 0) {
|
|
383
|
+
if (isPoint(from)) fromPoint = { x: fromPoint.x, y: fromPoint.y - resolveScrollDelta };
|
|
384
|
+
if (isPoint(to)) toPoint = { x: toPoint.x, y: toPoint.y - resolveScrollDelta };
|
|
385
|
+
}
|
|
386
|
+
if (ctx.speed === "instant") {
|
|
387
|
+
await ctx.page.mouse.move(fromPoint.x, fromPoint.y);
|
|
388
|
+
await ctx.page.mouse.down();
|
|
389
|
+
await ctx.page.mouse.move(toPoint.x, toPoint.y);
|
|
390
|
+
await ctx.page.mouse.up();
|
|
391
|
+
ctx.setMousePosition(toPoint);
|
|
392
|
+
return { from: fromPoint, to: toPoint };
|
|
393
|
+
}
|
|
394
|
+
if (fromBox && toBox) {
|
|
395
|
+
const scrollDelta = computeCurveScrollDelta(
|
|
396
|
+
fromPoint,
|
|
397
|
+
toPoint,
|
|
398
|
+
ctx.page.viewportSize(),
|
|
399
|
+
ctx.personality.mouse.curvature
|
|
400
|
+
);
|
|
401
|
+
if (scrollDelta !== 0) {
|
|
402
|
+
await executeScroll({ by: scrollDelta }, ctx, {});
|
|
403
|
+
const refreshedFrom = await resolveTargetPointAndBox(from, ctx, "drag");
|
|
404
|
+
fromPoint = refreshedFrom.point;
|
|
405
|
+
fromBox = refreshedFrom.box;
|
|
406
|
+
const refreshedTo = await resolveTargetPointAndBox(to, ctx, "drag");
|
|
407
|
+
toPoint = refreshedTo.point;
|
|
408
|
+
toBox = refreshedTo.box;
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
await maybeMisclickBeat(ctx, fromBox, fromPoint);
|
|
412
|
+
await walkBezierTo(fromPoint, ctx);
|
|
413
|
+
ctx.setMousePosition(fromPoint);
|
|
414
|
+
const preDragMs = computeDwellTime(
|
|
415
|
+
ctx.personality.dwell.preClickMs,
|
|
416
|
+
ctx.personality.dwell.preClickJitter,
|
|
417
|
+
ctx.personality,
|
|
418
|
+
ctx.speed,
|
|
419
|
+
ctx.rng
|
|
420
|
+
);
|
|
421
|
+
if (preDragMs > 0) await sleep(preDragMs);
|
|
422
|
+
await ctx.page.mouse.down();
|
|
423
|
+
await maybeMisclickBeat(ctx, toBox, toPoint);
|
|
424
|
+
await walkBezierTo(toPoint, ctx);
|
|
425
|
+
await ctx.page.mouse.up();
|
|
426
|
+
ctx.setMousePosition(toPoint);
|
|
427
|
+
const postActionMs = computeDwellTime(
|
|
428
|
+
ctx.personality.dwell.postActionMs,
|
|
429
|
+
ctx.personality.dwell.postActionJitter,
|
|
430
|
+
ctx.personality,
|
|
431
|
+
ctx.speed,
|
|
432
|
+
ctx.rng
|
|
433
|
+
);
|
|
434
|
+
if (postActionMs > 0) await sleep(postActionMs);
|
|
435
|
+
return { from: fromPoint, to: toPoint };
|
|
436
|
+
}
|
|
437
|
+
async function executeMove(target, ctx) {
|
|
438
|
+
const point = await resolveTargetPoint(target, ctx, "move");
|
|
439
|
+
if (ctx.speed === "instant") {
|
|
440
|
+
await ctx.page.mouse.move(point.x, point.y);
|
|
441
|
+
ctx.setMousePosition(point);
|
|
442
|
+
return { target: point };
|
|
443
|
+
}
|
|
444
|
+
await walkBezierTo(point, ctx);
|
|
445
|
+
ctx.setMousePosition(point);
|
|
446
|
+
return { target: point };
|
|
447
|
+
}
|
|
448
|
+
async function moveToTarget(target, ctx, action) {
|
|
449
|
+
const box = await readBoxWithAutoScroll(target, ctx, action);
|
|
450
|
+
const targetPoint = pickClickPoint(box, ctx.rng, ctx.personality.mouse.clickSpread);
|
|
451
|
+
if (action === "click") await maybeMisclickBeat(ctx, box, targetPoint);
|
|
452
|
+
await walkBezierTo(targetPoint, ctx);
|
|
453
|
+
return targetPoint;
|
|
454
|
+
}
|
|
455
|
+
async function walkBezierTo(to, ctx) {
|
|
456
|
+
const startPoint = ctx.getMousePosition();
|
|
457
|
+
const rawPath = core.bezierPath(startPoint, to, ctx.rng, {
|
|
458
|
+
curvature: ctx.personality.mouse.curvature
|
|
459
|
+
});
|
|
460
|
+
const path = core.humanizePath(rawPath, ctx.rng);
|
|
461
|
+
const travelMs = computeTravelTime(path, ctx.personality, ctx.speed, ctx.rng);
|
|
462
|
+
await walkMouseAlongPath(ctx.page, path, travelMs);
|
|
463
|
+
}
|
|
464
|
+
async function resolveTargetPoint(target, ctx, action) {
|
|
465
|
+
if (isPoint(target)) return target;
|
|
466
|
+
return resolveLocatorPoint(target, ctx, action);
|
|
467
|
+
}
|
|
468
|
+
async function resolveTargetPointAndBox(target, ctx, action) {
|
|
469
|
+
if (isPoint(target)) return { point: target, box: null };
|
|
470
|
+
const box = await readBoxWithAutoScroll(target, ctx, action);
|
|
471
|
+
const point = pickClickPoint(box, ctx.rng, ctx.personality.mouse.clickSpread);
|
|
472
|
+
return { point, box };
|
|
473
|
+
}
|
|
474
|
+
async function resolveLocatorPoint(target, ctx, action) {
|
|
475
|
+
const box = await readBoxWithAutoScroll(target, ctx, action);
|
|
476
|
+
return pickClickPoint(box, ctx.rng, ctx.personality.mouse.clickSpread);
|
|
477
|
+
}
|
|
478
|
+
async function readBoxWithAutoScroll(target, ctx, action) {
|
|
479
|
+
const locator = typeof target === "string" ? ctx.page.locator(target) : target;
|
|
480
|
+
let box = await locator.boundingBox();
|
|
481
|
+
if (!box) {
|
|
482
|
+
throw new Error(
|
|
483
|
+
`Cannot ${action}: element not found or has no bounding box (target: ${describeTarget(target)})`
|
|
484
|
+
);
|
|
485
|
+
}
|
|
486
|
+
const viewport = ctx.page.viewportSize();
|
|
487
|
+
if (viewport && !isBoxCenterInViewport(box, viewport)) {
|
|
488
|
+
if (ctx.speed === "instant") {
|
|
489
|
+
await locator.scrollIntoViewIfNeeded();
|
|
490
|
+
} else {
|
|
491
|
+
await executeScroll(locator, ctx, { block: "center" });
|
|
492
|
+
}
|
|
493
|
+
box = await locator.boundingBox();
|
|
494
|
+
if (!box) {
|
|
495
|
+
throw new Error(
|
|
496
|
+
`Cannot ${action}: element disappeared after scrolling into view (target: ${describeTarget(target)})`
|
|
497
|
+
);
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
return box;
|
|
501
|
+
}
|
|
502
|
+
function isBoxCenterInViewport(box, viewport) {
|
|
503
|
+
const cx = box.x + box.width / 2;
|
|
504
|
+
const cy = box.y + box.height / 2;
|
|
505
|
+
return cx >= 0 && cx <= viewport.width && cy >= 0 && cy <= viewport.height;
|
|
506
|
+
}
|
|
507
|
+
async function readScrollY(page) {
|
|
508
|
+
if (typeof page.evaluate !== "function") return 0;
|
|
509
|
+
return page.evaluate(() => window.scrollY);
|
|
510
|
+
}
|
|
511
|
+
var CURVE_VIEWPORT_MARGIN = 20;
|
|
512
|
+
function computeCurveScrollDelta(from, to, viewport, curvature) {
|
|
513
|
+
if (!viewport) return 0;
|
|
514
|
+
const distance = Math.hypot(to.x - from.x, to.y - from.y);
|
|
515
|
+
const perpendicularExtent = distance * curvature;
|
|
516
|
+
const minY = Math.min(from.y, to.y) - perpendicularExtent;
|
|
517
|
+
const maxY = Math.max(from.y, to.y) + perpendicularExtent;
|
|
518
|
+
const topOverflow = -minY + CURVE_VIEWPORT_MARGIN;
|
|
519
|
+
const bottomOverflow = maxY + CURVE_VIEWPORT_MARGIN - viewport.height;
|
|
520
|
+
if (bottomOverflow > 0 && bottomOverflow >= topOverflow) return bottomOverflow;
|
|
521
|
+
if (topOverflow > 0) return -topOverflow;
|
|
522
|
+
return 0;
|
|
523
|
+
}
|
|
524
|
+
function isPoint(target) {
|
|
525
|
+
return typeof target === "object" && target !== null && !("boundingBox" in target) && typeof target.x === "number" && typeof target.y === "number";
|
|
526
|
+
}
|
|
527
|
+
var MISCLICK_OFFSET_MIN = 5;
|
|
528
|
+
var MISCLICK_OFFSET_MAX = 15;
|
|
529
|
+
async function maybeMisclickBeat(ctx, box, targetPoint) {
|
|
530
|
+
if (!ctx.rng.chance(ctx.personality.mouse.misclickProbability)) return;
|
|
531
|
+
if (cursorAlreadyOnTarget(ctx.getMousePosition(), box, targetPoint)) return;
|
|
532
|
+
const viewport = ctx.page.viewportSize();
|
|
533
|
+
const misclickPoint = box ? pickMisclickOutsideBox(box, ctx.rng, viewport) : pickMisclickAroundPoint(targetPoint, ctx.rng, viewport);
|
|
534
|
+
if (misclickPoint === null) return;
|
|
535
|
+
await walkBezierTo(misclickPoint, ctx);
|
|
536
|
+
ctx.setMousePosition(misclickPoint);
|
|
537
|
+
const realizeMs = computeDwellTime(
|
|
538
|
+
ctx.personality.dwell.preClickMs,
|
|
539
|
+
ctx.personality.dwell.preClickJitter,
|
|
540
|
+
ctx.personality,
|
|
541
|
+
ctx.speed,
|
|
542
|
+
ctx.rng
|
|
543
|
+
);
|
|
544
|
+
if (realizeMs > 0) await sleep(realizeMs);
|
|
545
|
+
}
|
|
546
|
+
function cursorAlreadyOnTarget(current, box, targetPoint) {
|
|
547
|
+
if (box) {
|
|
548
|
+
return current.x >= box.x && current.x <= box.x + box.width && current.y >= box.y && current.y <= box.y + box.height;
|
|
549
|
+
}
|
|
550
|
+
const dx = current.x - targetPoint.x;
|
|
551
|
+
const dy = current.y - targetPoint.y;
|
|
552
|
+
return Math.hypot(dx, dy) < MISCLICK_OFFSET_MIN;
|
|
553
|
+
}
|
|
554
|
+
function pickMisclickOutsideBox(box, rng, viewport) {
|
|
555
|
+
const edge = rng.nextInt(0, 4);
|
|
556
|
+
const offset = rng.nextFloat(MISCLICK_OFFSET_MIN, MISCLICK_OFFSET_MAX);
|
|
557
|
+
const along = rng.nextFloat(0.2, 0.8);
|
|
558
|
+
let x;
|
|
559
|
+
let y;
|
|
560
|
+
if (edge === 0) {
|
|
561
|
+
x = box.x + box.width * along;
|
|
562
|
+
y = box.y - offset;
|
|
563
|
+
} else if (edge === 1) {
|
|
564
|
+
x = box.x + box.width + offset;
|
|
565
|
+
y = box.y + box.height * along;
|
|
566
|
+
} else if (edge === 2) {
|
|
567
|
+
x = box.x + box.width * along;
|
|
568
|
+
y = box.y + box.height + offset;
|
|
569
|
+
} else {
|
|
570
|
+
x = box.x - offset;
|
|
571
|
+
y = box.y + box.height * along;
|
|
572
|
+
}
|
|
573
|
+
if (viewport) {
|
|
574
|
+
x = clamp2(x, 0, viewport.width - 1);
|
|
575
|
+
y = clamp2(y, 0, viewport.height - 1);
|
|
576
|
+
}
|
|
577
|
+
const insideBox = x >= box.x && x <= box.x + box.width && y >= box.y && y <= box.y + box.height;
|
|
578
|
+
if (insideBox) return null;
|
|
579
|
+
return { x, y };
|
|
580
|
+
}
|
|
581
|
+
function pickMisclickAroundPoint(target, rng, viewport) {
|
|
582
|
+
const angle = rng.nextFloat(0, Math.PI * 2);
|
|
583
|
+
const distance = rng.nextFloat(MISCLICK_OFFSET_MIN, MISCLICK_OFFSET_MAX);
|
|
584
|
+
let x = target.x + Math.cos(angle) * distance;
|
|
585
|
+
let y = target.y + Math.sin(angle) * distance;
|
|
586
|
+
if (viewport) {
|
|
587
|
+
x = clamp2(x, 0, viewport.width - 1);
|
|
588
|
+
y = clamp2(y, 0, viewport.height - 1);
|
|
589
|
+
}
|
|
590
|
+
if (x === target.x && y === target.y) return null;
|
|
591
|
+
return { x, y };
|
|
592
|
+
}
|
|
593
|
+
function pickClickPoint(box, rng, clickSpread) {
|
|
131
594
|
const cx = box.x + box.width / 2;
|
|
132
595
|
const cy = box.y + box.height / 2;
|
|
133
|
-
const
|
|
134
|
-
const
|
|
596
|
+
const sigmaX = box.width * clickSpread;
|
|
597
|
+
const sigmaY = box.height * clickSpread;
|
|
598
|
+
const x = clamp2(cx + rng.nextGaussian(0, sigmaX), box.x, box.x + box.width);
|
|
599
|
+
const y = clamp2(cy + rng.nextGaussian(0, sigmaY), box.y, box.y + box.height);
|
|
135
600
|
return { x, y };
|
|
136
601
|
}
|
|
137
602
|
function computeTravelTime(path, personality, speed, rng) {
|
|
@@ -149,9 +614,10 @@ function computeTravelTime(path, personality, speed, rng) {
|
|
|
149
614
|
return Math.max(0, total);
|
|
150
615
|
}
|
|
151
616
|
function describeTarget(target) {
|
|
617
|
+
if (isPoint(target)) return `point(${target.x}, ${target.y})`;
|
|
152
618
|
return typeof target === "string" ? target : target.toString?.() ?? "locator";
|
|
153
619
|
}
|
|
154
|
-
function
|
|
620
|
+
function clamp2(value, min, max) {
|
|
155
621
|
return value < min ? min : value > max ? max : value;
|
|
156
622
|
}
|
|
157
623
|
async function executeRead(target, ctx, options = {}) {
|
|
@@ -635,178 +1101,6 @@ async function startCapture(page, options = {}) {
|
|
|
635
1101
|
}
|
|
636
1102
|
};
|
|
637
1103
|
}
|
|
638
|
-
var RESERVED_TARGETS = /* @__PURE__ */ new Set(["natural", "end", "top"]);
|
|
639
|
-
async function executeScroll(target, ctx, options = {}) {
|
|
640
|
-
const { page, personality, rng, speed } = ctx;
|
|
641
|
-
const speedFactor = speedModeFactor(speed);
|
|
642
|
-
const axis = options.axis ?? "y";
|
|
643
|
-
const container = resolveWithin(options.within, ctx);
|
|
644
|
-
const geom = container ? await readContainerGeometry(container, axis) : await readWindowGeometry(page, axis);
|
|
645
|
-
if (!geom) {
|
|
646
|
-
return { from: 0, to: 0, distance: 0, durationMs: 0 };
|
|
647
|
-
}
|
|
648
|
-
const from = geom.current;
|
|
649
|
-
const targetPos = await resolveTarget(target, ctx, geom, container, axis, options.block);
|
|
650
|
-
const to = clamp2(targetPos, 0, Math.max(0, geom.total - geom.viewport));
|
|
651
|
-
const distance = to - from;
|
|
652
|
-
if (distance === 0) {
|
|
653
|
-
return { from, to, distance: 0, durationMs: 0 };
|
|
654
|
-
}
|
|
655
|
-
if (speed === "instant") {
|
|
656
|
-
if (container) {
|
|
657
|
-
await container.evaluate(
|
|
658
|
-
(el, args) => {
|
|
659
|
-
const a = args;
|
|
660
|
-
if (a.axis === "x") el.scrollTo(a.pos, el.scrollTop);
|
|
661
|
-
else el.scrollTo(el.scrollLeft, a.pos);
|
|
662
|
-
},
|
|
663
|
-
{ axis, pos: to }
|
|
664
|
-
);
|
|
665
|
-
} else {
|
|
666
|
-
await page.evaluate(
|
|
667
|
-
(args) => {
|
|
668
|
-
if (args.axis === "x") window.scrollTo(args.pos, window.scrollY);
|
|
669
|
-
else window.scrollTo(window.scrollX, args.pos);
|
|
670
|
-
},
|
|
671
|
-
{ axis, pos: to }
|
|
672
|
-
);
|
|
673
|
-
}
|
|
674
|
-
return { from, to, distance, durationMs: 0 };
|
|
675
|
-
}
|
|
676
|
-
const segments = core.planScroll(from, to, personality.scroll, rng, {
|
|
677
|
-
forceOvershoot: options.overshoot,
|
|
678
|
-
withPauses: options.withPauses,
|
|
679
|
-
personalitySpeed: personality.speed,
|
|
680
|
-
speedFactor
|
|
681
|
-
});
|
|
682
|
-
if (container && geom.hover) {
|
|
683
|
-
await page.mouse.move(geom.hover.x, geom.hover.y);
|
|
684
|
-
}
|
|
685
|
-
const startedAt = Date.now();
|
|
686
|
-
await walkSegments(page, segments, axis, container);
|
|
687
|
-
const durationMs = Date.now() - startedAt;
|
|
688
|
-
return { from, to, distance, durationMs };
|
|
689
|
-
}
|
|
690
|
-
function resolveWithin(within, ctx) {
|
|
691
|
-
if (!within) return null;
|
|
692
|
-
return typeof within === "string" ? ctx.page.locator(within) : within;
|
|
693
|
-
}
|
|
694
|
-
async function readWindowGeometry(page, axis) {
|
|
695
|
-
const g = await page.evaluate((a) => {
|
|
696
|
-
if (a === "x") {
|
|
697
|
-
return {
|
|
698
|
-
current: window.scrollX,
|
|
699
|
-
viewport: window.innerWidth,
|
|
700
|
-
total: Math.max(document.documentElement.scrollWidth, document.body?.scrollWidth ?? 0)
|
|
701
|
-
};
|
|
702
|
-
}
|
|
703
|
-
return {
|
|
704
|
-
current: window.scrollY,
|
|
705
|
-
viewport: window.innerHeight,
|
|
706
|
-
total: Math.max(document.documentElement.scrollHeight, document.body?.scrollHeight ?? 0)
|
|
707
|
-
};
|
|
708
|
-
}, axis);
|
|
709
|
-
return { current: g.current, viewport: g.viewport, total: g.total };
|
|
710
|
-
}
|
|
711
|
-
async function readContainerGeometry(container, axis) {
|
|
712
|
-
return container.evaluate((el, a) => {
|
|
713
|
-
const rect = el.getBoundingClientRect();
|
|
714
|
-
const isX = a === "x";
|
|
715
|
-
return {
|
|
716
|
-
current: isX ? el.scrollLeft : el.scrollTop,
|
|
717
|
-
viewport: isX ? el.clientWidth : el.clientHeight,
|
|
718
|
-
total: isX ? el.scrollWidth : el.scrollHeight,
|
|
719
|
-
hover: {
|
|
720
|
-
x: rect.left + rect.width / 2,
|
|
721
|
-
y: rect.top + rect.height / 2
|
|
722
|
-
}
|
|
723
|
-
};
|
|
724
|
-
}, axis).catch(() => null);
|
|
725
|
-
}
|
|
726
|
-
async function resolveTarget(target, ctx, geom, container, axis, block = "start") {
|
|
727
|
-
if (target === void 0 || target === "natural") return geom.current + geom.viewport;
|
|
728
|
-
if (target === "end") return geom.total;
|
|
729
|
-
if (target === "top") return 0;
|
|
730
|
-
if (typeof target === "object" && "by" in target) return geom.current + target.by;
|
|
731
|
-
if (typeof target === "object" && "to" in target) return target.to;
|
|
732
|
-
const elementLocator = typeof target === "string" && !RESERVED_TARGETS.has(target) ? ctx.page.locator(target) : typeof target === "string" ? null : target;
|
|
733
|
-
if (!elementLocator) return geom.current + geom.viewport;
|
|
734
|
-
return container ? resolveElementWithinContainer(elementLocator, container, geom, axis, block) : resolveElementInWindow(elementLocator, geom, axis, block);
|
|
735
|
-
}
|
|
736
|
-
async function resolveElementInWindow(elementLocator, geom, axis, block) {
|
|
737
|
-
const rect = await elementLocator.boundingBox().catch(() => null);
|
|
738
|
-
if (!rect) return geom.current;
|
|
739
|
-
const relStart = axis === "x" ? rect.x : rect.y;
|
|
740
|
-
const length = axis === "x" ? rect.width : rect.height;
|
|
741
|
-
const absoluteStart = geom.current + relStart;
|
|
742
|
-
const absoluteEnd = absoluteStart + length;
|
|
743
|
-
if (block === "start") return absoluteStart;
|
|
744
|
-
if (block === "end") return absoluteEnd - geom.viewport;
|
|
745
|
-
if (block === "nearest") {
|
|
746
|
-
if (relStart >= 0 && relStart + length <= geom.viewport) return geom.current;
|
|
747
|
-
if (relStart < 0) return absoluteStart;
|
|
748
|
-
return absoluteEnd - geom.viewport;
|
|
749
|
-
}
|
|
750
|
-
return absoluteStart - (geom.viewport - length) / 2;
|
|
751
|
-
}
|
|
752
|
-
async function resolveElementWithinContainer(elementLocator, container, geom, axis, block) {
|
|
753
|
-
const rects = await container.evaluate(
|
|
754
|
-
(containerEl, args) => {
|
|
755
|
-
const elementEl = args.sel ? document.querySelector(args.sel) : null;
|
|
756
|
-
const targetEl = elementEl ?? containerEl.querySelector(":scope > *");
|
|
757
|
-
if (!targetEl) return null;
|
|
758
|
-
const cRect = containerEl.getBoundingClientRect();
|
|
759
|
-
const eRect = targetEl.getBoundingClientRect();
|
|
760
|
-
return args.axis === "x" ? { relStart: eRect.left - cRect.left, length: eRect.width } : { relStart: eRect.top - cRect.top, length: eRect.height };
|
|
761
|
-
},
|
|
762
|
-
{ sel: await locatorSelector(elementLocator), axis }
|
|
763
|
-
).catch(() => null);
|
|
764
|
-
if (!rects) return geom.current;
|
|
765
|
-
const offsetStart = rects.relStart + geom.current;
|
|
766
|
-
const offsetEnd = offsetStart + rects.length;
|
|
767
|
-
if (block === "start") return offsetStart;
|
|
768
|
-
if (block === "end") return offsetEnd - geom.viewport;
|
|
769
|
-
if (block === "nearest") {
|
|
770
|
-
if (rects.relStart >= 0 && rects.relStart + rects.length <= geom.viewport) {
|
|
771
|
-
return geom.current;
|
|
772
|
-
}
|
|
773
|
-
if (rects.relStart < 0) return offsetStart;
|
|
774
|
-
return offsetEnd - geom.viewport;
|
|
775
|
-
}
|
|
776
|
-
return offsetStart - (geom.viewport - rects.length) / 2;
|
|
777
|
-
}
|
|
778
|
-
async function locatorSelector(locator) {
|
|
779
|
-
const s = locator.toString?.();
|
|
780
|
-
if (typeof s !== "string") return null;
|
|
781
|
-
const match = /locator\(['"](.+?)['"]/.exec(s);
|
|
782
|
-
if (!match) return null;
|
|
783
|
-
const raw = match[1] ?? "";
|
|
784
|
-
const eq = raw.indexOf("=");
|
|
785
|
-
return eq > 0 && /^[a-z]+$/.test(raw.slice(0, eq)) ? raw.slice(eq + 1) : raw;
|
|
786
|
-
}
|
|
787
|
-
async function walkSegments(page, segments, axis, container) {
|
|
788
|
-
for (const segment of segments) {
|
|
789
|
-
if (segment.delayBeforeMs > 0) await sleep(segment.delayBeforeMs);
|
|
790
|
-
if (segment.delta === 0) continue;
|
|
791
|
-
if (container) {
|
|
792
|
-
await container.evaluate(
|
|
793
|
-
(el, args) => {
|
|
794
|
-
const a = args;
|
|
795
|
-
if (a.axis === "x") el.scrollLeft += a.delta;
|
|
796
|
-
else el.scrollTop += a.delta;
|
|
797
|
-
},
|
|
798
|
-
{ axis, delta: segment.delta }
|
|
799
|
-
);
|
|
800
|
-
} else if (axis === "x") {
|
|
801
|
-
await page.mouse.wheel(segment.delta, 0);
|
|
802
|
-
} else {
|
|
803
|
-
await page.mouse.wheel(0, segment.delta);
|
|
804
|
-
}
|
|
805
|
-
}
|
|
806
|
-
}
|
|
807
|
-
function clamp2(value, min, max) {
|
|
808
|
-
return value < min ? min : value > max ? max : value;
|
|
809
|
-
}
|
|
810
1104
|
|
|
811
1105
|
// src/mouse-helper/index.ts
|
|
812
1106
|
var CURSOR_PATH = "M 0 0 L 16 6 L 8 9.5 L 5 19 Z";
|
|
@@ -971,6 +1265,23 @@ async function createHuman(page, options = {}) {
|
|
|
971
1265
|
}
|
|
972
1266
|
}
|
|
973
1267
|
let lastMousePosition = options.initialMousePosition ?? { x: 0, y: 0 };
|
|
1268
|
+
const mouseCtx = () => ({
|
|
1269
|
+
page,
|
|
1270
|
+
personality,
|
|
1271
|
+
rng,
|
|
1272
|
+
speed,
|
|
1273
|
+
getMousePosition: () => lastMousePosition,
|
|
1274
|
+
setMousePosition: (point) => {
|
|
1275
|
+
lastMousePosition = point;
|
|
1276
|
+
}
|
|
1277
|
+
});
|
|
1278
|
+
const describeMouseTarget = (target) => {
|
|
1279
|
+
if (typeof target === "string") return target;
|
|
1280
|
+
if ("x" in target && "y" in target && typeof target.x === "number") {
|
|
1281
|
+
return `point(${target.x}, ${target.y})`;
|
|
1282
|
+
}
|
|
1283
|
+
return target.toString?.() ?? "locator";
|
|
1284
|
+
};
|
|
974
1285
|
return {
|
|
975
1286
|
personality,
|
|
976
1287
|
speed,
|
|
@@ -980,29 +1291,65 @@ async function createHuman(page, options = {}) {
|
|
|
980
1291
|
});
|
|
981
1292
|
},
|
|
982
1293
|
async click(target) {
|
|
983
|
-
const description =
|
|
1294
|
+
const description = describeMouseTarget(target);
|
|
984
1295
|
await performAction({ type: "click", params: { target: description } }, async () => {
|
|
985
|
-
await executeClick(target,
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
1296
|
+
await executeClick(target, mouseCtx());
|
|
1297
|
+
});
|
|
1298
|
+
},
|
|
1299
|
+
async rightClick(target) {
|
|
1300
|
+
const description = describeMouseTarget(target);
|
|
1301
|
+
await performAction({ type: "rightClick", params: { target: description } }, async () => {
|
|
1302
|
+
await executeClick(target, mouseCtx(), { button: "right" });
|
|
1303
|
+
});
|
|
1304
|
+
},
|
|
1305
|
+
async hover(target) {
|
|
1306
|
+
const description = describeMouseTarget(target);
|
|
1307
|
+
await performAction({ type: "hover", params: { target: description } }, async () => {
|
|
1308
|
+
await executeHover(target, mouseCtx());
|
|
1309
|
+
});
|
|
1310
|
+
},
|
|
1311
|
+
async move(target) {
|
|
1312
|
+
const description = describeMouseTarget(target);
|
|
1313
|
+
await performAction({ type: "move", params: { target: description } }, async () => {
|
|
1314
|
+
await executeMove(target, mouseCtx());
|
|
1315
|
+
});
|
|
1316
|
+
},
|
|
1317
|
+
async drag(from, to) {
|
|
1318
|
+
const fromDesc = describeMouseTarget(from);
|
|
1319
|
+
const toDesc = describeMouseTarget(to);
|
|
1320
|
+
await performAction({ type: "drag", params: { from: fromDesc, to: toDesc } }, async () => {
|
|
1321
|
+
await executeDrag(from, to, mouseCtx());
|
|
995
1322
|
});
|
|
996
1323
|
},
|
|
997
1324
|
async type(target, value) {
|
|
998
|
-
const description =
|
|
1325
|
+
const description = describeMouseTarget(target);
|
|
999
1326
|
await performAction(
|
|
1000
1327
|
{ type: "type", params: { target: description, length: value.length } },
|
|
1001
1328
|
async () => {
|
|
1329
|
+
if (speed !== "instant" && value.length > 0) {
|
|
1330
|
+
await executeClick(target, mouseCtx());
|
|
1331
|
+
}
|
|
1002
1332
|
await executeType(target, value, { page, personality, rng, speed });
|
|
1003
1333
|
}
|
|
1004
1334
|
);
|
|
1005
1335
|
},
|
|
1336
|
+
async paste(target, value) {
|
|
1337
|
+
const description = describeMouseTarget(target);
|
|
1338
|
+
await performAction(
|
|
1339
|
+
{ type: "paste", params: { target: description, length: value.length } },
|
|
1340
|
+
async () => {
|
|
1341
|
+
if (speed !== "instant" && value.length > 0) {
|
|
1342
|
+
await executeClick(target, mouseCtx());
|
|
1343
|
+
}
|
|
1344
|
+
await executePaste(target, value, { page, personality, rng, speed });
|
|
1345
|
+
}
|
|
1346
|
+
);
|
|
1347
|
+
},
|
|
1348
|
+
async press(key) {
|
|
1349
|
+
await performAction({ type: "press", params: { key } }, async () => {
|
|
1350
|
+
await executePress(key, { page, personality, rng, speed });
|
|
1351
|
+
});
|
|
1352
|
+
},
|
|
1006
1353
|
async read(target, options2) {
|
|
1007
1354
|
const description = describeReadTarget(target);
|
|
1008
1355
|
return performAction(
|