ant0-browser 0.0.0-stage → 0.2.0-beta.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/LICENSE +35 -0
- package/README.md +208 -2
- package/dist/canvas.d.ts +55 -0
- package/dist/canvas.d.ts.map +1 -0
- package/dist/canvas.js +82 -0
- package/dist/canvas.js.map +1 -0
- package/dist/chromium-version.d.ts +22 -0
- package/dist/chromium-version.d.ts.map +1 -0
- package/dist/chromium-version.js +22 -0
- package/dist/chromium-version.js.map +1 -0
- package/dist/claim-hints.d.ts +36 -0
- package/dist/claim-hints.d.ts.map +1 -0
- package/dist/claim-hints.js +9 -0
- package/dist/claim-hints.js.map +1 -0
- package/dist/cleanup.d.ts +35 -0
- package/dist/cleanup.d.ts.map +1 -0
- package/dist/cleanup.js +106 -0
- package/dist/cleanup.js.map +1 -0
- package/dist/content.d.ts +11 -0
- package/dist/content.d.ts.map +1 -0
- package/dist/content.js +52 -0
- package/dist/content.js.map +1 -0
- package/dist/download.d.ts +24 -0
- package/dist/download.d.ts.map +1 -0
- package/dist/download.js +227 -0
- package/dist/download.js.map +1 -0
- package/dist/errors.d.ts +49 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +53 -0
- package/dist/errors.js.map +1 -0
- package/dist/fingerprint.d.ts +218 -0
- package/dist/fingerprint.d.ts.map +1 -0
- package/dist/fingerprint.js +475 -0
- package/dist/fingerprint.js.map +1 -0
- package/dist/fontpack.d.ts +13 -0
- package/dist/fontpack.d.ts.map +1 -0
- package/dist/fontpack.js +150 -0
- package/dist/fontpack.js.map +1 -0
- package/dist/fonts.d.ts +15 -0
- package/dist/fonts.d.ts.map +1 -0
- package/dist/fonts.js +86 -0
- package/dist/fonts.js.map +1 -0
- package/dist/geoip.d.ts +45 -0
- package/dist/geoip.d.ts.map +1 -0
- package/dist/geoip.js +290 -0
- package/dist/geoip.js.map +1 -0
- package/dist/geometry.d.ts +32 -0
- package/dist/geometry.d.ts.map +1 -0
- package/dist/geometry.js +85 -0
- package/dist/geometry.js.map +1 -0
- package/dist/humanize.d.ts +20 -0
- package/dist/humanize.d.ts.map +1 -0
- package/dist/humanize.js +921 -0
- package/dist/humanize.js.map +1 -0
- package/dist/identity-persistence.d.ts +88 -0
- package/dist/identity-persistence.d.ts.map +1 -0
- package/dist/identity-persistence.js +381 -0
- package/dist/identity-persistence.js.map +1 -0
- package/dist/identity.d.ts +81 -0
- package/dist/identity.d.ts.map +1 -0
- package/dist/identity.js +370 -0
- package/dist/identity.js.map +1 -0
- package/dist/index.d.ts +281 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +790 -0
- package/dist/index.js.map +1 -0
- package/dist/launch-ticket.d.ts +191 -0
- package/dist/launch-ticket.d.ts.map +1 -0
- package/dist/launch-ticket.js +549 -0
- package/dist/launch-ticket.js.map +1 -0
- package/dist/launchopts.d.ts +266 -0
- package/dist/launchopts.d.ts.map +1 -0
- package/dist/launchopts.js +623 -0
- package/dist/launchopts.js.map +1 -0
- package/dist/motion.d.ts +106 -0
- package/dist/motion.d.ts.map +1 -0
- package/dist/motion.js +311 -0
- package/dist/motion.js.map +1 -0
- package/dist/motionscore.d.ts +57 -0
- package/dist/motionscore.d.ts.map +1 -0
- package/dist/motionscore.js +215 -0
- package/dist/motionscore.js.map +1 -0
- package/dist/native/linux-x64/PRODUCT-LICENSE.txt +28 -0
- package/dist/native/linux-x64/ant0_runtime.node +0 -0
- package/dist/native/windows-x64/PRODUCT-LICENSE.txt +28 -0
- package/dist/native/windows-x64/ant0_runtime.node +0 -0
- package/dist/persona-migration.d.ts +54 -0
- package/dist/persona-migration.d.ts.map +1 -0
- package/dist/persona-migration.js +97 -0
- package/dist/persona-migration.js.map +1 -0
- package/dist/persona-profile-v2.d.ts +169 -0
- package/dist/persona-profile-v2.d.ts.map +1 -0
- package/dist/persona-profile-v2.js +454 -0
- package/dist/persona-profile-v2.js.map +1 -0
- package/dist/persona-profile-v4.d.ts +61 -0
- package/dist/persona-profile-v4.d.ts.map +1 -0
- package/dist/persona-profile-v4.js +79 -0
- package/dist/persona-profile-v4.js.map +1 -0
- package/dist/persona-rejected.d.ts +36 -0
- package/dist/persona-rejected.d.ts.map +1 -0
- package/dist/persona-rejected.js +40 -0
- package/dist/persona-rejected.js.map +1 -0
- package/dist/population-model.d.ts +45 -0
- package/dist/population-model.d.ts.map +1 -0
- package/dist/population-model.js +245 -0
- package/dist/population-model.js.map +1 -0
- package/dist/profile-file.d.ts +201 -0
- package/dist/profile-file.d.ts.map +1 -0
- package/dist/profile-file.js +736 -0
- package/dist/profile-file.js.map +1 -0
- package/dist/release-fetch.d.ts +74 -0
- package/dist/release-fetch.d.ts.map +1 -0
- package/dist/release-fetch.js +395 -0
- package/dist/release-fetch.js.map +1 -0
- package/dist/release.d.ts +16 -0
- package/dist/release.d.ts.map +1 -0
- package/dist/release.js +26 -0
- package/dist/release.js.map +1 -0
- package/dist/render.d.ts +38 -0
- package/dist/render.d.ts.map +1 -0
- package/dist/render.js +104 -0
- package/dist/render.js.map +1 -0
- package/dist/runtime-client.d.ts +47 -0
- package/dist/runtime-client.d.ts.map +1 -0
- package/dist/runtime-client.js +327 -0
- package/dist/runtime-client.js.map +1 -0
- package/dist/runtime-launch.d.ts +22 -0
- package/dist/runtime-launch.d.ts.map +1 -0
- package/dist/runtime-launch.js +92 -0
- package/dist/runtime-launch.js.map +1 -0
- package/dist/scale.d.ts +29 -0
- package/dist/scale.d.ts.map +1 -0
- package/dist/scale.js +105 -0
- package/dist/scale.js.map +1 -0
- package/dist/shaderdialect.d.ts +33 -0
- package/dist/shaderdialect.d.ts.map +1 -0
- package/dist/shaderdialect.js +47 -0
- package/dist/shaderdialect.js.map +1 -0
- package/dist/speech-coherence.d.ts +52 -0
- package/dist/speech-coherence.d.ts.map +1 -0
- package/dist/speech-coherence.js +254 -0
- package/dist/speech-coherence.js.map +1 -0
- package/dist/template-selection.d.ts +14 -0
- package/dist/template-selection.d.ts.map +1 -0
- package/dist/template-selection.js +66 -0
- package/dist/template-selection.js.map +1 -0
- package/dist/warnings.d.ts +50 -0
- package/dist/warnings.d.ts.map +1 -0
- package/dist/warnings.js +273 -0
- package/dist/warnings.js.map +1 -0
- package/dist/webgl-coherence.d.ts +30 -0
- package/dist/webgl-coherence.d.ts.map +1 -0
- package/dist/webgl-coherence.js +98 -0
- package/dist/webgl-coherence.js.map +1 -0
- package/dist/webgpu-override.d.ts +14 -0
- package/dist/webgpu-override.d.ts.map +1 -0
- package/dist/webgpu-override.js +82 -0
- package/dist/webgpu-override.js.map +1 -0
- package/dist/widevine.d.ts +44 -0
- package/dist/widevine.d.ts.map +1 -0
- package/dist/widevine.js +203 -0
- package/dist/widevine.js.map +1 -0
- package/package.json +56 -4
package/dist/humanize.js
ADDED
|
@@ -0,0 +1,921 @@
|
|
|
1
|
+
// Humanized input + cursor visualization.
|
|
2
|
+
//
|
|
3
|
+
// `humanize: true` installs ONE consistent human-input standard covering moving, clicking,
|
|
4
|
+
// dragging, scrolling and typing — dispatched as native trusted input (isTrusted===true,
|
|
5
|
+
// navigator.webdriver stays false). It works whether you drive the page directly or via
|
|
6
|
+
// locators:
|
|
7
|
+
// page level — page.click / hover / dblclick / type / fill / press / selectOption,
|
|
8
|
+
// page.mouse.move / click / wheel and held-button drags (down -> move -> up),
|
|
9
|
+
// page.keyboard.type / press
|
|
10
|
+
// locator level — locator.click / type / fill / hover / dblclick / press /
|
|
11
|
+
// pressSequentially / clear / check / uncheck / tap / selectOption / dragTo
|
|
12
|
+
// (routed through the humanized page methods; main-frame locators only,
|
|
13
|
+
// native fallback)
|
|
14
|
+
// `showCursor: true` — injects a red cursor dot that follows the real mousemove events.
|
|
15
|
+
//
|
|
16
|
+
// Mouse moves build an eased, slightly bowed cubic-bezier path SDK-side from the tracked cursor
|
|
17
|
+
// position (continuous — no snap back to 0,0) using Playwright's NATIVE mouse.move. Because it is
|
|
18
|
+
// native input, the button state is carried: down() -> move() -> up() is a real held-button drag
|
|
19
|
+
// (slider captchas work) with no separate CDP channel to desync. Typing goes key-by-key through
|
|
20
|
+
// the native keyboard (trusted; shift handled by the engine) with human inter-key timing. No
|
|
21
|
+
// special binary command required.
|
|
22
|
+
import { makePersona, planMove, dragDwell, clickHold, keyDwell, clickPoint, planAmbient, gaussFrom } from "./motion.js";
|
|
23
|
+
/** Overlay: a red dot that follows real mousemove events. Idempotent IIFE so it works both as
|
|
24
|
+
* an addInitScript source string AND when passed to page.evaluate. */
|
|
25
|
+
export const CURSOR_OVERLAY = `(() => {
|
|
26
|
+
if (window.__ant0BrowserCursor) return; window.__ant0BrowserCursor = 1;
|
|
27
|
+
const make = () => {
|
|
28
|
+
if (document.getElementById('__ant0-browser_cursor')) return;
|
|
29
|
+
const d = document.createElement('div'); d.id = '__ant0-browser_cursor';
|
|
30
|
+
d.style.cssText = 'position:fixed;left:0;top:0;width:20px;height:20px;margin:-10px 0 0 -10px;' +
|
|
31
|
+
'border-radius:50%;border:2px solid #ff3b3b;background:rgba(255,59,59,.22);' +
|
|
32
|
+
'box-shadow:0 0 10px rgba(255,59,59,.6);pointer-events:none;z-index:2147483647';
|
|
33
|
+
(document.body || document.documentElement).appendChild(d);
|
|
34
|
+
};
|
|
35
|
+
if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', make); else make();
|
|
36
|
+
document.addEventListener('mousemove', (e) => {
|
|
37
|
+
const d = document.getElementById('__ant0-browser_cursor');
|
|
38
|
+
if (d) { d.style.left = e.clientX + 'px'; d.style.top = e.clientY + 'px'; }
|
|
39
|
+
}, true);
|
|
40
|
+
})();`;
|
|
41
|
+
const rand = (a, b) => a + Math.random() * (b - a);
|
|
42
|
+
const clamp = (v, lo, hi) => Math.max(lo, Math.min(hi, v));
|
|
43
|
+
// Same unseeded live source as rand() — the seeded persona rng stays inside motion.ts, this is only
|
|
44
|
+
// for placing a point in a plausible spread instead of on an exact landmark.
|
|
45
|
+
const gauss = () => gaussFrom(Math.random);
|
|
46
|
+
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
|
|
47
|
+
// Compact keyboard-adjacency map for realistic fat-finger typos.
|
|
48
|
+
const NEARBY = {
|
|
49
|
+
a: "sqwz", b: "vghn", c: "xdfv", d: "sfecx", e: "wrsdf", f: "dgrtcv",
|
|
50
|
+
g: "fhtyb", h: "gjybn", i: "ujko", j: "hkunm", k: "jloi", l: "kop",
|
|
51
|
+
m: "njk", n: "bhjm", o: "iklp", p: "ol", q: "wa", r: "edft",
|
|
52
|
+
s: "awedxz", t: "rfgy", u: "yhji", v: "cfgb", w: "qase", x: "zsdc",
|
|
53
|
+
y: "tghu", z: "asx",
|
|
54
|
+
};
|
|
55
|
+
function nearbyKey(ch) {
|
|
56
|
+
const lo = ch.toLowerCase();
|
|
57
|
+
const n = NEARBY[lo];
|
|
58
|
+
if (!n)
|
|
59
|
+
return ch;
|
|
60
|
+
const w = n[Math.floor(Math.random() * n.length)];
|
|
61
|
+
return ch === ch.toUpperCase() && ch !== ch.toLowerCase() ? w.toUpperCase() : w;
|
|
62
|
+
}
|
|
63
|
+
const SELECT_ALL = "Control+a"; // Ant0 Browser ships a Windows binary; Control is correct.
|
|
64
|
+
/** Wrap a single page's input methods + (optionally) inject the cursor overlay. */
|
|
65
|
+
export async function attachHumanize(browser, page, opts) {
|
|
66
|
+
// IDEMPOTENCY GUARD — do not remove.
|
|
67
|
+
//
|
|
68
|
+
// installHumanize wraps BOTH browser.newPage and browser.newContext. Playwright's newPage()
|
|
69
|
+
// creates a context internally, so the wrapped newContext runs installHumanizeOnContext (which
|
|
70
|
+
// attaches on the "page" event) and then the outer newPage wrapper attaches a second time.
|
|
71
|
+
//
|
|
72
|
+
// Without this guard the mouse is wrapped twice, and the second
|
|
73
|
+
// const nativeMove = mouse.move.bind(mouse)
|
|
74
|
+
// captures the FIRST WRAPPER rather than the real native move — so every step of the outer
|
|
75
|
+
// planned path triggers an entire inner planned path. Measured on the same ~980px move:
|
|
76
|
+
// browser.newPage() 51338 ms 2946 samples
|
|
77
|
+
// context.newPage() 2833 ms 161 samples <- correct (planner caps at ~180)
|
|
78
|
+
// i.e. an 18x blow-up on the most common entry point in the API.
|
|
79
|
+
if (page.__ant0BrowserHumanized)
|
|
80
|
+
return;
|
|
81
|
+
page.__ant0BrowserHumanized = true;
|
|
82
|
+
if (opts.showCursor) {
|
|
83
|
+
const inject = () => page.evaluate(CURSOR_OVERLAY).catch(() => { });
|
|
84
|
+
inject();
|
|
85
|
+
page.on("load", () => { inject(); });
|
|
86
|
+
page.on("framenavigated", (f) => { if (f === page.mainFrame())
|
|
87
|
+
inject(); });
|
|
88
|
+
}
|
|
89
|
+
if (!opts.humanize)
|
|
90
|
+
return;
|
|
91
|
+
// Per-identity motor persona (cadence, tremor, overshoot, handedness) — stable across the session,
|
|
92
|
+
// seeded from the fingerprint so behavior is consistent within an identity and unlinkable across
|
|
93
|
+
// seeds. Stored on the page so the module-level Locator.dragTo patch can reach it too.
|
|
94
|
+
const persona = makePersona(opts.seed);
|
|
95
|
+
page._ant0BrowserPersona = persona;
|
|
96
|
+
// Tracked cursor position so each move continues from where the last one ended.
|
|
97
|
+
// `held` tracks whether a mouse button is currently down — see mouse.move below.
|
|
98
|
+
// `placed` records whether the engine's cursor has actually been moved yet. A page starts with the
|
|
99
|
+
// pointer at the default origin, so anything that delivers input WITHOUT a target to move to —
|
|
100
|
+
// a wheel, a mouse.down that opens a drag, a bare keyboard.type — otherwise happens from a cursor
|
|
101
|
+
// that has never been anywhere. The guarantees below key off this flag.
|
|
102
|
+
const st = { x: rand(140, 380), y: rand(90, 240), held: false, placed: false };
|
|
103
|
+
const mouse = page.mouse;
|
|
104
|
+
const nativeMove = mouse.move.bind(mouse);
|
|
105
|
+
const nativeClick = mouse.click.bind(mouse);
|
|
106
|
+
const nativeWheel = mouse.wheel.bind(mouse);
|
|
107
|
+
const nativeDown = mouse.down.bind(mouse);
|
|
108
|
+
const nativeUp = mouse.up.bind(mouse);
|
|
109
|
+
// Walk a planned trajectory (motion.ts) with native (trusted) mouse.move + off-protocol sleeps.
|
|
110
|
+
// Native input carries the button state, so a button pressed via mouse.down() stays held across
|
|
111
|
+
// the whole path — the same code powers a free move AND a held-button drag.
|
|
112
|
+
const dispatch = async (steps) => {
|
|
113
|
+
for (const s of steps) {
|
|
114
|
+
try {
|
|
115
|
+
await nativeMove(s.x, s.y);
|
|
116
|
+
}
|
|
117
|
+
catch {
|
|
118
|
+
break;
|
|
119
|
+
}
|
|
120
|
+
st.placed = true;
|
|
121
|
+
await sleep(s.sleepMs);
|
|
122
|
+
}
|
|
123
|
+
};
|
|
124
|
+
// Move to (x, y) as a minimum-jerk sum-of-submovements path (primary ballistic + corrective homing)
|
|
125
|
+
// with colored sub-pixel noise and Fitts-scaled duration. `settle` adds a seating jiggle (drag).
|
|
126
|
+
const glide = async (x, y, o = {}) => {
|
|
127
|
+
await dispatch(planMove(st, { x, y }, persona, { settle: o.settle, targetW: o.targetW }));
|
|
128
|
+
st.x = x;
|
|
129
|
+
st.y = y;
|
|
130
|
+
};
|
|
131
|
+
// Engine-assisted trajectory routing: send point-to-point moves/clicks through
|
|
132
|
+
// Browser.humanizedClick. Held-button DRAGS stay native; falls back to the
|
|
133
|
+
// SDK bezier if the method is unavailable (older engine / no browser session).
|
|
134
|
+
const eng = { session: null, tid: null, ok: true };
|
|
135
|
+
const engineGlide = async (x, y, noClick) => {
|
|
136
|
+
if (!eng.ok)
|
|
137
|
+
return false;
|
|
138
|
+
try {
|
|
139
|
+
if (eng.session === null) {
|
|
140
|
+
const ctx = page.context();
|
|
141
|
+
const tsession = await ctx.newCDPSession(page);
|
|
142
|
+
const info = await tsession.send("Target.getTargetInfo");
|
|
143
|
+
eng.tid = info.targetInfo.targetId;
|
|
144
|
+
const br = ctx.browser ? ctx.browser() : null;
|
|
145
|
+
eng.session = br ? await br.newBrowserCDPSession() : tsession;
|
|
146
|
+
}
|
|
147
|
+
const dx = x - st.x, dy = y - st.y;
|
|
148
|
+
const dist = Math.sqrt(dx * dx + dy * dy);
|
|
149
|
+
const dur = Math.min(1.10, Math.max(0.28, 0.30 + dist / 1700)) * (0.85 + 0.30 * Math.random());
|
|
150
|
+
await eng.session.send("Browser.humanizedClick", { targetId: eng.tid, x, y, duration: dur, noClick });
|
|
151
|
+
// The engine walks the trajectory in the BROWSER process and answers this command before it
|
|
152
|
+
// has finished. Without waiting, a following mouse.down() fires at the cursor's STALE
|
|
153
|
+
// position — measured: a move to (110,58) then a press landed at (981,629), on <body>
|
|
154
|
+
// instead of the target, so every move-then-press sequence (drag, press-and-hold,
|
|
155
|
+
// drag-and-drop) acted on the wrong element.
|
|
156
|
+
await sleep(dur * 1000);
|
|
157
|
+
try {
|
|
158
|
+
await nativeMove(x, y);
|
|
159
|
+
}
|
|
160
|
+
catch { /* keep playwright cursor pos in sync for drags */ }
|
|
161
|
+
st.x = x;
|
|
162
|
+
st.y = y;
|
|
163
|
+
st.placed = true;
|
|
164
|
+
return true;
|
|
165
|
+
}
|
|
166
|
+
catch {
|
|
167
|
+
eng.ok = false;
|
|
168
|
+
return false;
|
|
169
|
+
}
|
|
170
|
+
};
|
|
171
|
+
// The REAL viewport, in CSS px — NOT page.viewportSize().
|
|
172
|
+
//
|
|
173
|
+
// installHeadedViewport (index.ts) forces `viewport: null` on every headed newPage/newContext,
|
|
174
|
+
// and launchPersistentContext does the same, so that innerWidth tracks the real OS window
|
|
175
|
+
// instead of Playwright's emulated 1280x720 (an emulated viewport on a headed window is itself
|
|
176
|
+
// a tell). With viewport:null, viewportSize() returns null for the LIFE of the page — so the
|
|
177
|
+
// `|| {1280, 800}` fallback was taken on every headed launch, and the scroll anchor's
|
|
178
|
+
// "is the pointer inside the viewport" gate was evaluated against a box unrelated to the
|
|
179
|
+
// window. On a maximized display a cursor legitimately at x=1400 read as out-of-bounds and got
|
|
180
|
+
// re-homed on EVERY scroll — precisely the behaviour that gate exists to prevent.
|
|
181
|
+
// innerWidth/innerHeight are correct in both modes; the constant is a last resort only.
|
|
182
|
+
let vpCache = null;
|
|
183
|
+
const viewport = async () => {
|
|
184
|
+
try {
|
|
185
|
+
const wh = (await page.evaluate("() => [innerWidth, innerHeight]"));
|
|
186
|
+
if (wh && wh[0] && wh[1]) {
|
|
187
|
+
vpCache = { width: wh[0], height: wh[1] };
|
|
188
|
+
return vpCache;
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
catch { /* detached/navigating — fall through */ }
|
|
192
|
+
return (page.viewportSize && page.viewportSize()) || vpCache || { width: 1280, height: 800 };
|
|
193
|
+
};
|
|
194
|
+
// Non-goal cursor activity (idle drift + a few excursions). It never presses a button and never
|
|
195
|
+
// approaches a caller-named target, so it is also the safe way to give the cursor a position when
|
|
196
|
+
// an action needs one but names no coordinates.
|
|
197
|
+
const ambient = async (ms) => {
|
|
198
|
+
try {
|
|
199
|
+
const steps = planAmbient(st, await viewport(), persona, ms);
|
|
200
|
+
await dispatch(steps);
|
|
201
|
+
if (steps.length) {
|
|
202
|
+
st.x = steps[steps.length - 1].x;
|
|
203
|
+
st.y = steps[steps.length - 1].y;
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
catch { /* best-effort ambient */ }
|
|
207
|
+
};
|
|
208
|
+
// Give the cursor a position for input that names no coordinates (a bare keyboard.type). It is
|
|
209
|
+
// ambient only: it never presses, so it cannot activate anything it happens to pass over.
|
|
210
|
+
//
|
|
211
|
+
// NOT used before mouse.down — see that wrapper. Before any real move st.x/st.y is the RANDOM
|
|
212
|
+
// SEED assigned at attach, so pressing at the end of this walk would put a TRUSTED click at an
|
|
213
|
+
// arbitrary page coordinate the caller never named, which can land on a link and navigate.
|
|
214
|
+
const ensurePlaced = async () => {
|
|
215
|
+
if (st.placed)
|
|
216
|
+
return;
|
|
217
|
+
await ambient(rand(260, 560));
|
|
218
|
+
if (!st.placed) {
|
|
219
|
+
try {
|
|
220
|
+
await nativeMove(st.x, st.y);
|
|
221
|
+
st.placed = true;
|
|
222
|
+
}
|
|
223
|
+
catch { /* best-effort placement */ }
|
|
224
|
+
}
|
|
225
|
+
};
|
|
226
|
+
// A HELD BUTTON MUST STAY ON THE NATIVE PATH. The engine trajectory builds its own
|
|
227
|
+
// WebMouseEvents and cannot hold a button across them, so routing a drag leg through it emits
|
|
228
|
+
// moves reporting MouseEvent.buttons 0 in the middle of a press. That is both a contradiction no
|
|
229
|
+
// real mouse produces (buttons is derived from physical state) and a functional break — a range
|
|
230
|
+
// thumb, a map or a drop target ignores a move that says nothing is down. Measured before this
|
|
231
|
+
// guard: 672 of 829 held moves reported buttons 0, and a slider dragged 10%->90% never left 0.
|
|
232
|
+
// A BARE MOVE ALWAYS USES THE NATIVE PATH — never the engine trajectory. Two independent
|
|
233
|
+
// reasons, both measured on the Python sync file which shares this design:
|
|
234
|
+
// * a held button cannot be carried across the engine's own WebMouseEvents, so a drag leg
|
|
235
|
+
// routed through it reports MouseEvent.buttons 0 mid-press and a range thumb ignores it
|
|
236
|
+
// (349 of 354 held moves reported buttons 0; the slider moved 0 -> 1 and stopped);
|
|
237
|
+
// * the engine walks its path ASYNCHRONOUSLY in the browser process and answers before it has
|
|
238
|
+
// finished, so it keeps moving the cursor AFTER the pre-press pin in mouse.down — a move to
|
|
239
|
+
// (56,120) then down() pressed on HTML at (856,213) instead of the INPUT, and the slider
|
|
240
|
+
// stayed at 0. Re-pinning right before the press does not help; the overrun outlives it.
|
|
241
|
+
// mouse.click keeps engine routing: it performs move+click entirely engine-side, where nothing
|
|
242
|
+
// races the trajectory. glide() is still fully humanized (minimum-jerk, colored noise, Fitts).
|
|
243
|
+
mouse.move = async (x, y) => {
|
|
244
|
+
await glide(x, y);
|
|
245
|
+
};
|
|
246
|
+
// down/up are wrapped to TRACK the held state mouse.move needs, and — critically — to make the
|
|
247
|
+
// press position DETERMINISTIC. The engine answers humanizedClick before its trajectory has
|
|
248
|
+
// finished walking, and it overruns the `duration` it reports, so sleeping that duration is not
|
|
249
|
+
// enough: measured on Node, a move to (110,58) was still mid-flight at (416,504) when the press
|
|
250
|
+
// fired, landing on <body>. Re-issuing a native move to the tracked target immediately before
|
|
251
|
+
// the press pins the cursor to where the caller asked, whatever the trajectory is doing.
|
|
252
|
+
mouse.down = async (...a) => {
|
|
253
|
+
// DELIBERATELY NO PLACEMENT WHEN THE POINTER HAS NEVER MOVED.
|
|
254
|
+
//
|
|
255
|
+
// An earlier revision called ensurePlaced() here and pressed wherever the ambient walk ended.
|
|
256
|
+
// But before any real move st.x/st.y is the RANDOM SEED assigned at attach — not a coordinate
|
|
257
|
+
// the caller named — so that converted a harmless no-op press at the origin into a TRUSTED
|
|
258
|
+
// click at an arbitrary point on the page, which can hit a link and navigate. That is a worse
|
|
259
|
+
// failure than the one it was meant to fix. A bare down() with no preceding move is a caller
|
|
260
|
+
// mistake, and humanize must not invent a target to paper over it; callers who want a
|
|
261
|
+
// humanized drag move first, and that move glides and sets `placed`.
|
|
262
|
+
if (st.placed) {
|
|
263
|
+
// Re-pin only once a real move has happened — that is what makes st.x/st.y correspond to
|
|
264
|
+
// where the engine's cursor actually is. The engine answers humanizedClick before its
|
|
265
|
+
// trajectory has finished, so without this the press can fire at a stale position.
|
|
266
|
+
try {
|
|
267
|
+
await nativeMove(st.x, st.y);
|
|
268
|
+
}
|
|
269
|
+
catch { /* best effort: keep the press on target */ }
|
|
270
|
+
}
|
|
271
|
+
st.held = true;
|
|
272
|
+
try {
|
|
273
|
+
return await nativeDown(...a);
|
|
274
|
+
}
|
|
275
|
+
catch (e) {
|
|
276
|
+
st.held = false; // a raising press must not strand held=true and make every later move a drag leg
|
|
277
|
+
throw e;
|
|
278
|
+
}
|
|
279
|
+
};
|
|
280
|
+
mouse.up = async (...a) => { try {
|
|
281
|
+
return await nativeUp(...a);
|
|
282
|
+
}
|
|
283
|
+
finally {
|
|
284
|
+
st.held = false;
|
|
285
|
+
} };
|
|
286
|
+
mouse.click = async (x, y) => {
|
|
287
|
+
if (await engineGlide(x, y, false))
|
|
288
|
+
return;
|
|
289
|
+
await glide(x, y);
|
|
290
|
+
await sleep(rand(40, 130)); // brief dwell before pressing, like a human
|
|
291
|
+
// delay = the mousedown→mouseup HOLD (human ~60–150 ms); without it Playwright releases in ~2 ms.
|
|
292
|
+
try {
|
|
293
|
+
await nativeClick(x, y, { delay: clickHold(persona) });
|
|
294
|
+
}
|
|
295
|
+
catch { /* best-effort */ }
|
|
296
|
+
};
|
|
297
|
+
// Opt-in ambient / pre-challenge cursor activity: idle drift + a few non-goal moves so a behavioral
|
|
298
|
+
// collector sees non-zero pointer entropy BEFORE the first goal action (the biggest reason a
|
|
299
|
+
// challenge/slider is shown to headless automation). `await page.ambientMotion(ms)`.
|
|
300
|
+
page.ambientMotion = async (ms = 1200) => { await ambient(ms); };
|
|
301
|
+
// Held-button drag leg with the seating jiggle (settle), reachable from the module-level
|
|
302
|
+
// Locator.dragTo patch (which can't see this closure) via the page object.
|
|
303
|
+
page._ant0BrowserHeldGlide = async (x, y) => { await glide(x, y, { settle: true }); };
|
|
304
|
+
// OPT-IN: no ambient burst on load by default (it interleaves with caller-driven input,
|
|
305
|
+
// e.g. a slider drag). Call page.ambientMotion(ms) explicitly, or set
|
|
306
|
+
// page._ant0BrowserAutoAmbient = true to restore the per-load burst.
|
|
307
|
+
page._ant0BrowserAutoAmbient = false;
|
|
308
|
+
page.on("load", () => {
|
|
309
|
+
if (page._ant0BrowserAutoAmbient) {
|
|
310
|
+
Promise.resolve(page.ambientMotion(rand(450, 950))).catch(() => { });
|
|
311
|
+
}
|
|
312
|
+
});
|
|
313
|
+
// A wheel event carries the pointer position, and until something moves the cursor that position
|
|
314
|
+
// is the default origin — so a scroll right after a page load reported the mouse parked in the
|
|
315
|
+
// corner, over nothing it was scrolling, while the deltas rolled down the middle of the document.
|
|
316
|
+
// Move to where someone reading this page would have left the cursor first.
|
|
317
|
+
//
|
|
318
|
+
// Only when the current position is unusable (never placed, or outside the viewport). A hand does
|
|
319
|
+
// not lift off the mouse between two scrolls of the same page, so re-homing on every wheel call
|
|
320
|
+
// would replace one tell with another.
|
|
321
|
+
const scrollAnchor = async () => {
|
|
322
|
+
try {
|
|
323
|
+
const vp = await viewport();
|
|
324
|
+
if (st.placed && st.x > 6 && st.y > 6 && st.x < vp.width - 6 && st.y < vp.height - 6)
|
|
325
|
+
return;
|
|
326
|
+
// Upper-middle of the reading column, gaussian-jittered: an exact viewport centre is as
|
|
327
|
+
// machine-made as the origin is, so the anchor has to be a distribution, not a landmark.
|
|
328
|
+
const x = clamp(vp.width * 0.48 + gauss() * vp.width * 0.10, vp.width * 0.18, vp.width * 0.82);
|
|
329
|
+
const y = clamp(vp.height * 0.36 + gauss() * vp.height * 0.09, vp.height * 0.14, vp.height * 0.62);
|
|
330
|
+
await glide(x, y);
|
|
331
|
+
await sleep(rand(60, 180)); // eyes land before the finger rolls the wheel
|
|
332
|
+
}
|
|
333
|
+
catch { /* best-effort: scroll from wherever the cursor is */ }
|
|
334
|
+
};
|
|
335
|
+
// scroll easing: break into eased chunks of native wheel deltas
|
|
336
|
+
mouse.wheel = async (dx, dy) => {
|
|
337
|
+
await scrollAnchor();
|
|
338
|
+
const steps = Math.max(5, Math.min(24, Math.round((Math.abs(dx) + Math.abs(dy)) / 60)));
|
|
339
|
+
const ease = (u) => 1 - Math.pow(1 - u, 2.2); // ease-OUT: fast flick -> slow inertial settle
|
|
340
|
+
let px = 0, py = 0;
|
|
341
|
+
for (let i = 1; i <= steps; i++) {
|
|
342
|
+
const f = ease(i / steps);
|
|
343
|
+
const nxw = Math.round(dx * f), nyw = Math.round(dy * f);
|
|
344
|
+
await nativeWheel(nxw - px, nyw - py);
|
|
345
|
+
px = nxw;
|
|
346
|
+
py = nyw;
|
|
347
|
+
// local sleep, NOT page.waitForTimeout: the latter is a CDP round-trip per call, so it
|
|
348
|
+
// emits protocol traffic bot-detectors (e.g. reCAPTCHA) can score — a self-inflicted tell
|
|
349
|
+
// inside the humanize path. setTimeout is off-protocol.
|
|
350
|
+
await sleep(rand(10, 38));
|
|
351
|
+
if (Math.random() < 0.07)
|
|
352
|
+
await sleep(rand(40, 120)); // occasional mid-scroll pause (reading)
|
|
353
|
+
// A hand resting on the mouse does not hold it perfectly still through a long scroll. Without
|
|
354
|
+
// this a wheel burst is the one input where the pointer coordinate never changes at all.
|
|
355
|
+
if (steps >= 10 && i > 2 && i < steps && Math.random() < 0.06) {
|
|
356
|
+
try {
|
|
357
|
+
await glide(st.x + gauss() * 3.5, st.y + gauss() * 2.5);
|
|
358
|
+
}
|
|
359
|
+
catch { /* drift is cosmetic */ }
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
if (px !== dx || py !== dy)
|
|
363
|
+
await nativeWheel(dx - px, dy - py);
|
|
364
|
+
};
|
|
365
|
+
// ----------------------------------------------------------------- keyboard
|
|
366
|
+
const keyboard = page.keyboard;
|
|
367
|
+
const nativeKbType = keyboard.type.bind(keyboard);
|
|
368
|
+
const nativeKbPress = keyboard.press.bind(keyboard);
|
|
369
|
+
// Emit one character with a human keydown→keyup DWELL. keyboard.press's `delay` IS the hold
|
|
370
|
+
// (keyboard.type's delay is only inter-key flight), so per-char press gives the missing dwell.
|
|
371
|
+
// Fall back to type() for anything press can't map (some symbols / composed chars).
|
|
372
|
+
const emitKey = async (ch) => {
|
|
373
|
+
try {
|
|
374
|
+
await nativeKbPress(ch, { delay: keyDwell(persona) });
|
|
375
|
+
}
|
|
376
|
+
catch {
|
|
377
|
+
try {
|
|
378
|
+
await nativeKbType(ch);
|
|
379
|
+
}
|
|
380
|
+
catch { /* best-effort */ }
|
|
381
|
+
}
|
|
382
|
+
};
|
|
383
|
+
// Type text key-by-key with human timing. Each char goes through the native keyboard (trusted;
|
|
384
|
+
// shift/symbols handled by the engine), so this stays isTrusted===true.
|
|
385
|
+
const humanTypeText = async (text) => {
|
|
386
|
+
const n = text.length;
|
|
387
|
+
for (let i = 0; i < n; i++) {
|
|
388
|
+
const ch = text[i];
|
|
389
|
+
if (/[a-zA-Z0-9]/.test(ch) && Math.random() < 0.02) {
|
|
390
|
+
try {
|
|
391
|
+
await emitKey(nearbyKey(ch));
|
|
392
|
+
await sleep(rand(120, 300));
|
|
393
|
+
await nativeKbPress("Backspace", { delay: keyDwell(persona) });
|
|
394
|
+
await sleep(rand(80, 200));
|
|
395
|
+
}
|
|
396
|
+
catch { /* typo path best-effort */ }
|
|
397
|
+
}
|
|
398
|
+
try {
|
|
399
|
+
await emitKey(ch);
|
|
400
|
+
}
|
|
401
|
+
catch {
|
|
402
|
+
break;
|
|
403
|
+
}
|
|
404
|
+
if (i < n - 1) {
|
|
405
|
+
if (Math.random() < 0.06)
|
|
406
|
+
await sleep(rand(180, 450)); // brief thinking pause
|
|
407
|
+
else
|
|
408
|
+
await sleep(rand(45, 150));
|
|
409
|
+
if (/\s/.test(ch))
|
|
410
|
+
await sleep(rand(20, 100)); // slight extra pause at word boundaries
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
};
|
|
414
|
+
// Keystrokes from a session in which the mouse never existed. Selector-based typing glides to the
|
|
415
|
+
// field first (focusClick), but page.keyboard.type/press names no target, and on a page focused by
|
|
416
|
+
// autofocus or by Tab it can be the first input of any kind — a keydown stream with zero preceding
|
|
417
|
+
// pointer events is one of the cheapest checks a collector can run.
|
|
418
|
+
//
|
|
419
|
+
// AMBIENT ONLY. Deliberately NOT a move toward the focused field: the caller has already chosen
|
|
420
|
+
// where the text goes, and approaching that element would hover it (and, on a control that reacts
|
|
421
|
+
// to hover, move it) for no behavioral gain. Once per page — after that the cursor has a history,
|
|
422
|
+
// and warming up before every keystroke would itself be the anomaly.
|
|
423
|
+
let kbWarmed = false;
|
|
424
|
+
const keyboardWarmup = async () => {
|
|
425
|
+
if (kbWarmed || st.placed)
|
|
426
|
+
return;
|
|
427
|
+
kbWarmed = true;
|
|
428
|
+
await ambient(rand(280, 620));
|
|
429
|
+
};
|
|
430
|
+
keyboard.type = async (text) => { await keyboardWarmup(); await humanTypeText(text); };
|
|
431
|
+
// keyboard.press with the persona's hold, unless the caller specified their own.
|
|
432
|
+
//
|
|
433
|
+
// WHY THIS EXISTS. keyboard.type was humanised from the start and keyboard.press was not, which
|
|
434
|
+
// left the most common single-key call in any script — Enter, Tab, Escape, arrow keys — emitting
|
|
435
|
+
// a keydown and keyUp in the same instant. Measured with humanize ON, before this wrapper:
|
|
436
|
+
// keyboard.type held keys 58–107ms while keyboard.press held them 1.4–3.8ms. A finger cannot
|
|
437
|
+
// press and release a key in under a millisecond, and a detector reading the SHORTEST dwell in a
|
|
438
|
+
// session sees the press path — so one un-humanised call undid the whole keyboard persona.
|
|
439
|
+
//
|
|
440
|
+
// The caller's own `delay` wins: press(key, {delay}) is the documented way to hold a key for a
|
|
441
|
+
// specific time, and silently overriding it would break that. Only the DEFAULT changes.
|
|
442
|
+
keyboard.press = async (key, options = {}) => {
|
|
443
|
+
await keyboardWarmup();
|
|
444
|
+
const opts = options && typeof options === "object" ? options : {};
|
|
445
|
+
if (opts.delay === undefined)
|
|
446
|
+
opts.delay = keyDwell(persona);
|
|
447
|
+
return nativeKbPress(key, opts);
|
|
448
|
+
};
|
|
449
|
+
// ------------------------------------------------------- page-level targeted helpers
|
|
450
|
+
const isFocused = async (selector) => {
|
|
451
|
+
try {
|
|
452
|
+
return await page.evaluate((s) => {
|
|
453
|
+
const e = globalThis.document.querySelector(s);
|
|
454
|
+
return !!e && e === globalThis.document.activeElement;
|
|
455
|
+
}, selector);
|
|
456
|
+
}
|
|
457
|
+
catch {
|
|
458
|
+
return false;
|
|
459
|
+
}
|
|
460
|
+
};
|
|
461
|
+
const pointFor = async (selector, timeout) => {
|
|
462
|
+
const loc = page.locator(selector).first();
|
|
463
|
+
await loc.waitFor({ state: "visible", timeout });
|
|
464
|
+
await loc.scrollIntoViewIfNeeded({ timeout });
|
|
465
|
+
if (!(await loc.isEnabled()))
|
|
466
|
+
return null;
|
|
467
|
+
let box = await loc.boundingBox();
|
|
468
|
+
if (!box)
|
|
469
|
+
return null;
|
|
470
|
+
await sleep(50); // stability: a box that's still moving = an animation in flight
|
|
471
|
+
const box2 = await loc.boundingBox();
|
|
472
|
+
if (!box2 || Math.abs(box2.x - box.x) > 1 || Math.abs(box2.y - box.y) > 1)
|
|
473
|
+
return null;
|
|
474
|
+
box = box2;
|
|
475
|
+
return clickPoint(box, st, persona); // 2D gaussian toward center, nudged to the approach side
|
|
476
|
+
};
|
|
477
|
+
const focusClick = async (selector, timeout) => {
|
|
478
|
+
const pt = await pointFor(selector, timeout);
|
|
479
|
+
if (!pt)
|
|
480
|
+
return false;
|
|
481
|
+
await glide(pt.x, pt.y);
|
|
482
|
+
await sleep(rand(40, 130));
|
|
483
|
+
await nativeClick(pt.x, pt.y, { delay: clickHold(persona) });
|
|
484
|
+
return true;
|
|
485
|
+
};
|
|
486
|
+
const nativePageType = page.type ? page.type.bind(page) : null;
|
|
487
|
+
const nativePageFill = page.fill.bind(page);
|
|
488
|
+
const nativePagePress = page.press.bind(page);
|
|
489
|
+
const nativePageDblclick = page.dblclick.bind(page);
|
|
490
|
+
const nativePageSelect = page.selectOption ? page.selectOption.bind(page) : null;
|
|
491
|
+
page.type = async (selector, text, options = {}) => {
|
|
492
|
+
try {
|
|
493
|
+
const timeout = options.timeout ?? 30000;
|
|
494
|
+
if (!(await isFocused(selector))) {
|
|
495
|
+
if (!(await focusClick(selector, timeout))) {
|
|
496
|
+
return nativePageType ? nativePageType(selector, text, options) : nativePageFill(selector, text, options);
|
|
497
|
+
}
|
|
498
|
+
await sleep(rand(40, 120));
|
|
499
|
+
}
|
|
500
|
+
await humanTypeText(text);
|
|
501
|
+
}
|
|
502
|
+
catch {
|
|
503
|
+
return nativePageType ? nativePageType(selector, text, options) : nativePageFill(selector, text, options);
|
|
504
|
+
}
|
|
505
|
+
};
|
|
506
|
+
page.fill = async (selector, value, options = {}) => {
|
|
507
|
+
try {
|
|
508
|
+
const timeout = options.timeout ?? 30000;
|
|
509
|
+
if (value.length > 200)
|
|
510
|
+
return nativePageFill(selector, value, options); // bulk -> atomic
|
|
511
|
+
if (!(await focusClick(selector, timeout)))
|
|
512
|
+
return nativePageFill(selector, value, options);
|
|
513
|
+
await sleep(rand(40, 120));
|
|
514
|
+
try {
|
|
515
|
+
await nativeKbPress(SELECT_ALL);
|
|
516
|
+
await sleep(rand(30, 80));
|
|
517
|
+
await nativeKbPress("Backspace");
|
|
518
|
+
await sleep(rand(40, 120));
|
|
519
|
+
}
|
|
520
|
+
catch { /* clear best-effort */ }
|
|
521
|
+
await humanTypeText(value);
|
|
522
|
+
}
|
|
523
|
+
catch {
|
|
524
|
+
return nativePageFill(selector, value, options);
|
|
525
|
+
}
|
|
526
|
+
};
|
|
527
|
+
page.dblclick = async (selector, options = {}) => {
|
|
528
|
+
try {
|
|
529
|
+
const timeout = options.timeout ?? 30000;
|
|
530
|
+
const pt = await pointFor(selector, timeout);
|
|
531
|
+
if (!pt)
|
|
532
|
+
return nativePageDblclick(selector, options);
|
|
533
|
+
await glide(pt.x, pt.y);
|
|
534
|
+
await sleep(rand(40, 130));
|
|
535
|
+
await nativeClick(pt.x, pt.y, { clickCount: 2, delay: rand(40, 90) });
|
|
536
|
+
}
|
|
537
|
+
catch {
|
|
538
|
+
return nativePageDblclick(selector, options);
|
|
539
|
+
}
|
|
540
|
+
};
|
|
541
|
+
page.press = async (selector, key, options = {}) => {
|
|
542
|
+
try {
|
|
543
|
+
const timeout = options.timeout ?? 30000;
|
|
544
|
+
if (!(await isFocused(selector))) {
|
|
545
|
+
if (!(await focusClick(selector, timeout)))
|
|
546
|
+
return nativePagePress(selector, key, options);
|
|
547
|
+
await sleep(rand(40, 120));
|
|
548
|
+
}
|
|
549
|
+
// The persona's hold, for the same reason keyboard.press above carries one: this is the
|
|
550
|
+
// path locator.press() delegates to, so without it every locator.press in a script emitted
|
|
551
|
+
// a zero-length keypress while the typed text around it looked human.
|
|
552
|
+
await nativeKbPress(key, { delay: keyDwell(persona) });
|
|
553
|
+
}
|
|
554
|
+
catch {
|
|
555
|
+
return nativePagePress(selector, key, options);
|
|
556
|
+
}
|
|
557
|
+
};
|
|
558
|
+
// page.selectOption was the last input method humanize did not touch, so a script that used it —
|
|
559
|
+
// rather than locator.selectOption, which has been patched for a while — went straight to
|
|
560
|
+
// Playwright's implementation: assign the value, dispatch input+change from page script. That is
|
|
561
|
+
// measurably scored: it fails the historical interaction trust check,
|
|
562
|
+
// because no user gesture can produce a change event with isTrusted false.
|
|
563
|
+
//
|
|
564
|
+
// The move is a HOVER, not a click: clicking a <select> opens the native popup, and the popup
|
|
565
|
+
// swallows the arrow keys the trusted path steps the selection with. A person's pointer is on the
|
|
566
|
+
// dropdown they are choosing from either way.
|
|
567
|
+
if (nativePageSelect) {
|
|
568
|
+
page.selectOption = async (selector, values, options = {}) => {
|
|
569
|
+
const timeout = options.timeout ?? 30000;
|
|
570
|
+
try {
|
|
571
|
+
const pt = await pointFor(selector, timeout);
|
|
572
|
+
if (pt) {
|
|
573
|
+
await glide(pt.x, pt.y);
|
|
574
|
+
await sleep(rand(50, 150));
|
|
575
|
+
}
|
|
576
|
+
}
|
|
577
|
+
catch { /* the pre-move is best-effort; a trusted selection still beats a native one */ }
|
|
578
|
+
try {
|
|
579
|
+
const got = await trustedSelect(page, selector, values, timeout);
|
|
580
|
+
if (got)
|
|
581
|
+
return got;
|
|
582
|
+
}
|
|
583
|
+
catch { /* fall back */ }
|
|
584
|
+
return nativePageSelect(selector, values, options);
|
|
585
|
+
};
|
|
586
|
+
}
|
|
587
|
+
const wrapTargeted = (name, noClick) => {
|
|
588
|
+
const orig = page[name].bind(page);
|
|
589
|
+
page[name] = async (selector, options = {}) => {
|
|
590
|
+
// Actionability pre-flight before a TRUSTED humanized click: visible+enabled+stable+
|
|
591
|
+
// not-covered, else fall back to the native click (which has its own waits) — only improves.
|
|
592
|
+
try {
|
|
593
|
+
const timeout = options.timeout ?? 30000;
|
|
594
|
+
const loc = page.locator(selector).first();
|
|
595
|
+
await loc.waitFor({ state: "visible", timeout });
|
|
596
|
+
await loc.scrollIntoViewIfNeeded({ timeout });
|
|
597
|
+
if (!(await loc.isEnabled()))
|
|
598
|
+
return orig(selector, options);
|
|
599
|
+
let box = await loc.boundingBox();
|
|
600
|
+
if (!box)
|
|
601
|
+
return orig(selector, options);
|
|
602
|
+
await sleep(50);
|
|
603
|
+
const box2 = await loc.boundingBox();
|
|
604
|
+
if (!box2 || Math.abs(box2.x - box.x) > 1 || Math.abs(box2.y - box.y) > 1) {
|
|
605
|
+
return orig(selector, options);
|
|
606
|
+
}
|
|
607
|
+
box = box2;
|
|
608
|
+
const cp = clickPoint(box, st, persona);
|
|
609
|
+
const x = cp.x, y = cp.y;
|
|
610
|
+
try {
|
|
611
|
+
const handle = await loc.elementHandle();
|
|
612
|
+
const covered = handle &&
|
|
613
|
+
(await page.evaluate(([px, py, el]) => {
|
|
614
|
+
const t = globalThis.document.elementFromPoint(px, py);
|
|
615
|
+
return !(t && (t === el || el.contains(t) || t.contains(el)));
|
|
616
|
+
}, [x, y, handle]));
|
|
617
|
+
if (covered)
|
|
618
|
+
return orig(selector, options); // covered -> let PW wait for it on top
|
|
619
|
+
}
|
|
620
|
+
catch { /* covered-by check best-effort */ }
|
|
621
|
+
await glide(x, y);
|
|
622
|
+
if (!noClick) {
|
|
623
|
+
await sleep(rand(40, 130));
|
|
624
|
+
await nativeClick(x, y, { delay: clickHold(persona) });
|
|
625
|
+
}
|
|
626
|
+
return;
|
|
627
|
+
}
|
|
628
|
+
catch {
|
|
629
|
+
return orig(selector, options);
|
|
630
|
+
}
|
|
631
|
+
};
|
|
632
|
+
};
|
|
633
|
+
wrapTargeted("click", false);
|
|
634
|
+
wrapTargeted("hover", true);
|
|
635
|
+
// Marker the Locator-class patch keys off: humanize is active for THIS page.
|
|
636
|
+
page._ant0BrowserHumanized = true;
|
|
637
|
+
patchLocatorClass(page.locator("html"));
|
|
638
|
+
}
|
|
639
|
+
/** Choose a <select> option with the keyboard, so the ENGINE fires the events. Returns Playwright's
|
|
640
|
+
* `[value]` result once the selection is VERIFIED, or null when the keyboard route cannot be shown
|
|
641
|
+
* to have worked and the caller should fall back to native.
|
|
642
|
+
*
|
|
643
|
+
* Playwright's selectOption assigns the value and dispatches input+change from script. Those arrive
|
|
644
|
+
* with isTrusted false, which is the single most reliable dropdown tell there is — the engine cannot
|
|
645
|
+
* produce an untrusted change, so a page reading the flag knows the selection was not made by a
|
|
646
|
+
* person. A <select> that has focus and is CLOSED steps on ArrowUp/ArrowDown and the browser emits
|
|
647
|
+
* input then change itself, trusted, exactly as it does for a mouse. The fix is not to forge better
|
|
648
|
+
* events, it is to stop forging them.
|
|
649
|
+
*
|
|
650
|
+
* Returns null whenever that cannot be guaranteed: a multi- or disabled select, an option that
|
|
651
|
+
* cannot be resolved, an ElementHandle target, or a platform where arrows open the popup instead of
|
|
652
|
+
* stepping (macOS). selectedIndex is verified afterwards rather than assumed — a silently wrong
|
|
653
|
+
* selection is worse than an untrusted one. */
|
|
654
|
+
async function trustedSelect(page, selector, values, timeout) {
|
|
655
|
+
try {
|
|
656
|
+
const one = (v) => (Array.isArray(v) && v.length === 1 ? v[0] : v);
|
|
657
|
+
const v = one(values);
|
|
658
|
+
let by = null, want = null;
|
|
659
|
+
if (v && typeof v === "object" && !v.constructor?.name?.includes("ElementHandle")) {
|
|
660
|
+
if (v.index != null) {
|
|
661
|
+
by = "index";
|
|
662
|
+
want = v.index;
|
|
663
|
+
}
|
|
664
|
+
else if (v.label != null) {
|
|
665
|
+
by = "label";
|
|
666
|
+
want = v.label;
|
|
667
|
+
}
|
|
668
|
+
else if (v.value != null) {
|
|
669
|
+
by = "value";
|
|
670
|
+
want = v.value;
|
|
671
|
+
}
|
|
672
|
+
}
|
|
673
|
+
else if (typeof v === "string") {
|
|
674
|
+
by = "value";
|
|
675
|
+
want = v;
|
|
676
|
+
}
|
|
677
|
+
if (by === null)
|
|
678
|
+
return null;
|
|
679
|
+
const plan = await page.evaluate((a) => {
|
|
680
|
+
const el = globalThis.document.querySelector(a.sel);
|
|
681
|
+
if (!el || el.multiple || el.disabled)
|
|
682
|
+
return null;
|
|
683
|
+
const os = [...el.options];
|
|
684
|
+
let i = -1;
|
|
685
|
+
if (a.by === "index")
|
|
686
|
+
i = a.want >= 0 && a.want < os.length ? a.want : -1;
|
|
687
|
+
else if (a.by === "label")
|
|
688
|
+
i = os.findIndex((o) => (o.label || o.textContent || "").trim() === String(a.want).trim());
|
|
689
|
+
else
|
|
690
|
+
i = os.findIndex((o) => o.value === a.want);
|
|
691
|
+
if (i < 0 || os[i].disabled)
|
|
692
|
+
return null;
|
|
693
|
+
return { to: i, from: el.selectedIndex, ret: os[i].value };
|
|
694
|
+
}, { sel: selector, by, want });
|
|
695
|
+
if (!plan)
|
|
696
|
+
return null;
|
|
697
|
+
if (plan.to === plan.from)
|
|
698
|
+
return [plan.ret]; // already selected; forge nothing
|
|
699
|
+
await page.focus(selector, { timeout });
|
|
700
|
+
await sleep(rand(60, 160));
|
|
701
|
+
const step = plan.to > plan.from ? "ArrowDown" : "ArrowUp";
|
|
702
|
+
for (let i = 0; i < Math.abs(plan.to - plan.from); i++) {
|
|
703
|
+
// page.keyboard.press — humanized by attachHumanize, so the hold comes with it.
|
|
704
|
+
await page.keyboard.press(step);
|
|
705
|
+
await sleep(rand(45, 120));
|
|
706
|
+
}
|
|
707
|
+
const got = await page.evaluate((q) => {
|
|
708
|
+
const e = globalThis.document.querySelector(q);
|
|
709
|
+
return e ? e.selectedIndex : -1;
|
|
710
|
+
}, selector);
|
|
711
|
+
if (got === plan.to)
|
|
712
|
+
return [plan.ret];
|
|
713
|
+
}
|
|
714
|
+
catch { /* fall back */ }
|
|
715
|
+
return null;
|
|
716
|
+
}
|
|
717
|
+
// --------------------------------------------------------------------------- locator patch
|
|
718
|
+
let locatorPatched = false;
|
|
719
|
+
/** Patch the Locator prototype once so locator.* interactions route through the humanized page.*
|
|
720
|
+
* methods. Each is a no-op unless the locator's page has humanize active AND the locator targets
|
|
721
|
+
* the main frame; anything else (frame locators, exotic selectors, errors) falls through to the
|
|
722
|
+
* original Playwright behaviour. Composes if another library already patched the prototype. */
|
|
723
|
+
function patchLocatorClass(sample) {
|
|
724
|
+
if (locatorPatched)
|
|
725
|
+
return;
|
|
726
|
+
locatorPatched = true;
|
|
727
|
+
const proto = Object.getPrototypeOf(sample);
|
|
728
|
+
if (!proto)
|
|
729
|
+
return;
|
|
730
|
+
const oFill = proto.fill, oClick = proto.click, oType = proto.type, oDbl = proto.dblclick, oHover = proto.hover, oPress = proto.press, oPressSeq = proto.pressSequentially, oClear = proto.clear, oTap = proto.tap, oCheck = proto.check, oUncheck = proto.uncheck, oDragTo = proto.dragTo, oSelectOption = proto.selectOption;
|
|
731
|
+
const on = (self) => {
|
|
732
|
+
const pg = self.page();
|
|
733
|
+
if (!pg || !pg._ant0BrowserHumanized)
|
|
734
|
+
return false;
|
|
735
|
+
try {
|
|
736
|
+
return self._frame === pg.mainFrame();
|
|
737
|
+
}
|
|
738
|
+
catch {
|
|
739
|
+
return false;
|
|
740
|
+
}
|
|
741
|
+
};
|
|
742
|
+
const sel = (self) => self._selector;
|
|
743
|
+
const fwd = (kw) => (kw && kw.timeout != null ? { timeout: kw.timeout } : {});
|
|
744
|
+
proto.fill = async function (value, kw = {}) {
|
|
745
|
+
if (on(this)) {
|
|
746
|
+
try {
|
|
747
|
+
return await this.page().fill(sel(this), value, fwd(kw));
|
|
748
|
+
}
|
|
749
|
+
catch { /* fall back */ }
|
|
750
|
+
}
|
|
751
|
+
return oFill.call(this, value, kw);
|
|
752
|
+
};
|
|
753
|
+
proto.click = async function (kw = {}) {
|
|
754
|
+
if (on(this)) {
|
|
755
|
+
try {
|
|
756
|
+
return await this.page().click(sel(this), fwd(kw));
|
|
757
|
+
}
|
|
758
|
+
catch { /* fall back */ }
|
|
759
|
+
}
|
|
760
|
+
return oClick.call(this, kw);
|
|
761
|
+
};
|
|
762
|
+
proto.type = async function (text, kw = {}) {
|
|
763
|
+
if (on(this)) {
|
|
764
|
+
try {
|
|
765
|
+
return await this.page().type(sel(this), text, fwd(kw));
|
|
766
|
+
}
|
|
767
|
+
catch { /* fall back */ }
|
|
768
|
+
}
|
|
769
|
+
return oType.call(this, text, kw);
|
|
770
|
+
};
|
|
771
|
+
proto.dblclick = async function (kw = {}) {
|
|
772
|
+
if (on(this)) {
|
|
773
|
+
try {
|
|
774
|
+
return await this.page().dblclick(sel(this), fwd(kw));
|
|
775
|
+
}
|
|
776
|
+
catch { /* fall back */ }
|
|
777
|
+
}
|
|
778
|
+
return oDbl.call(this, kw);
|
|
779
|
+
};
|
|
780
|
+
proto.hover = async function (kw = {}) {
|
|
781
|
+
if (on(this)) {
|
|
782
|
+
try {
|
|
783
|
+
return await this.page().hover(sel(this), fwd(kw));
|
|
784
|
+
}
|
|
785
|
+
catch { /* fall back */ }
|
|
786
|
+
}
|
|
787
|
+
return oHover.call(this, kw);
|
|
788
|
+
};
|
|
789
|
+
proto.press = async function (key, kw = {}) {
|
|
790
|
+
if (on(this)) {
|
|
791
|
+
try {
|
|
792
|
+
return await this.page().press(sel(this), key, fwd(kw));
|
|
793
|
+
}
|
|
794
|
+
catch { /* fall back */ }
|
|
795
|
+
}
|
|
796
|
+
return oPress.call(this, key, kw);
|
|
797
|
+
};
|
|
798
|
+
proto.pressSequentially = async function (text, kw = {}) {
|
|
799
|
+
if (on(this)) {
|
|
800
|
+
try {
|
|
801
|
+
return await this.page().type(sel(this), text, fwd(kw));
|
|
802
|
+
}
|
|
803
|
+
catch { /* fall back */ }
|
|
804
|
+
}
|
|
805
|
+
return oPressSeq.call(this, text, kw);
|
|
806
|
+
};
|
|
807
|
+
proto.clear = async function (kw = {}) {
|
|
808
|
+
if (on(this)) {
|
|
809
|
+
try {
|
|
810
|
+
return await this.page().fill(sel(this), "", fwd(kw));
|
|
811
|
+
}
|
|
812
|
+
catch { /* fall back */ }
|
|
813
|
+
}
|
|
814
|
+
return oClear.call(this, kw);
|
|
815
|
+
};
|
|
816
|
+
proto.tap = async function (kw = {}) {
|
|
817
|
+
if (on(this)) {
|
|
818
|
+
try {
|
|
819
|
+
return await this.page().click(sel(this), fwd(kw));
|
|
820
|
+
}
|
|
821
|
+
catch { /* fall back */ }
|
|
822
|
+
}
|
|
823
|
+
return oTap.call(this, kw);
|
|
824
|
+
};
|
|
825
|
+
proto.check = async function (kw = {}) {
|
|
826
|
+
if (on(this)) {
|
|
827
|
+
try {
|
|
828
|
+
if (!(await this.isChecked()))
|
|
829
|
+
return await this.page().click(sel(this), fwd(kw));
|
|
830
|
+
return;
|
|
831
|
+
}
|
|
832
|
+
catch { /* fall back */ }
|
|
833
|
+
}
|
|
834
|
+
return oCheck.call(this, kw);
|
|
835
|
+
};
|
|
836
|
+
proto.uncheck = async function (kw = {}) {
|
|
837
|
+
if (on(this)) {
|
|
838
|
+
try {
|
|
839
|
+
if (await this.isChecked())
|
|
840
|
+
return await this.page().click(sel(this), fwd(kw));
|
|
841
|
+
return;
|
|
842
|
+
}
|
|
843
|
+
catch { /* fall back */ }
|
|
844
|
+
}
|
|
845
|
+
return oUncheck.call(this, kw);
|
|
846
|
+
};
|
|
847
|
+
proto.selectOption = async function (values, kw = {}) {
|
|
848
|
+
// Routed through the page method (which glides to the control first) rather than calling
|
|
849
|
+
// trustedSelect straight, so a locator selection gets the same pre-move as a locator click.
|
|
850
|
+
if (on(this)) {
|
|
851
|
+
try {
|
|
852
|
+
return await this.page().selectOption(sel(this), values, fwd(kw));
|
|
853
|
+
}
|
|
854
|
+
catch { /* fall back */ }
|
|
855
|
+
}
|
|
856
|
+
return oSelectOption.call(this, values, kw);
|
|
857
|
+
};
|
|
858
|
+
proto.dragTo = async function (target, kw = {}) {
|
|
859
|
+
if (on(this)) {
|
|
860
|
+
try {
|
|
861
|
+
const pg = this.page();
|
|
862
|
+
const sb = await this.boundingBox();
|
|
863
|
+
const tb = await target.boundingBox();
|
|
864
|
+
if (sb && tb) {
|
|
865
|
+
const sx = sb.x + sb.width / 2, sy = sb.y + sb.height / 2;
|
|
866
|
+
const tx = tb.x + tb.width / 2, ty = tb.y + tb.height / 2;
|
|
867
|
+
// Human endpoint dynamics (the two worst slider tells): a grab hesitation AFTER pressing and
|
|
868
|
+
// a settle dwell BEFORE releasing, drawn from the page's motor persona.
|
|
869
|
+
const persona = pg._ant0BrowserPersona;
|
|
870
|
+
const { grabMs, releaseMs } = persona ? dragDwell(persona) : { grabMs: rand(130, 360), releaseMs: rand(90, 230) };
|
|
871
|
+
await pg.mouse.move(sx, sy);
|
|
872
|
+
await sleep(rand(100, 200));
|
|
873
|
+
await pg.mouse.down();
|
|
874
|
+
await sleep(grabMs); // grab hesitation (native -> button held across glide)
|
|
875
|
+
const heldGlide = pg._ant0BrowserHeldGlide; // humanized held-button drag + seating jiggle (settle)
|
|
876
|
+
if (heldGlide)
|
|
877
|
+
await heldGlide(tx, ty);
|
|
878
|
+
else
|
|
879
|
+
await pg.mouse.move(tx, ty);
|
|
880
|
+
await sleep(releaseMs); // pre-release settle before letting go
|
|
881
|
+
await pg.mouse.up();
|
|
882
|
+
return;
|
|
883
|
+
}
|
|
884
|
+
}
|
|
885
|
+
catch { /* fall back */ }
|
|
886
|
+
}
|
|
887
|
+
return oDragTo.call(this, target, kw);
|
|
888
|
+
};
|
|
889
|
+
}
|
|
890
|
+
/** Install humanize/showCursor on a browser: wraps newPage/newContext so every page is covered. */
|
|
891
|
+
export function installHumanize(browser, opts) {
|
|
892
|
+
if (!opts.humanize && !opts.showCursor)
|
|
893
|
+
return;
|
|
894
|
+
const origNewPage = browser.newPage.bind(browser);
|
|
895
|
+
browser.newPage = async (options) => {
|
|
896
|
+
const page = await origNewPage(options);
|
|
897
|
+
await attachHumanize(browser, page, opts);
|
|
898
|
+
return page;
|
|
899
|
+
};
|
|
900
|
+
const origNewContext = browser.newContext.bind(browser);
|
|
901
|
+
browser.newContext = async (options) => {
|
|
902
|
+
const ctx = await origNewContext(options);
|
|
903
|
+
installHumanizeOnContext(ctx, opts, browser);
|
|
904
|
+
return ctx;
|
|
905
|
+
};
|
|
906
|
+
}
|
|
907
|
+
/** Install humanize/showCursor on a single context (used for launchPersistentContext). */
|
|
908
|
+
export function installHumanizeOnContext(context, opts, browser) {
|
|
909
|
+
if (!opts.humanize && !opts.showCursor)
|
|
910
|
+
return;
|
|
911
|
+
const b = browser ?? context.browser();
|
|
912
|
+
if (opts.showCursor) {
|
|
913
|
+
context.addInitScript(CURSOR_OVERLAY).catch(() => { });
|
|
914
|
+
}
|
|
915
|
+
if (b) {
|
|
916
|
+
context.on("page", (p) => { attachHumanize(b, p, opts).catch(() => { }); });
|
|
917
|
+
for (const p of context.pages())
|
|
918
|
+
attachHumanize(b, p, opts).catch(() => { });
|
|
919
|
+
}
|
|
920
|
+
}
|
|
921
|
+
//# sourceMappingURL=humanize.js.map
|