turno-browser 0.3.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/CHANGELOG.md +122 -0
- package/README.md +95 -123
- package/dist/act-model.d.ts +85 -0
- package/dist/act-model.js +117 -0
- package/dist/act.d.ts +37 -0
- package/dist/act.js +600 -0
- package/dist/api.d.ts +90 -2
- package/dist/api.js +41 -4
- package/dist/argv.d.ts +16 -2
- package/dist/argv.js +100 -3
- package/dist/background-state.d.ts +54 -0
- package/dist/background-state.js +97 -0
- package/dist/background.d.ts +69 -0
- package/dist/background.js +458 -0
- package/dist/batch.d.ts +5 -0
- package/dist/batch.js +3 -3
- package/dist/bin/mcp.js +103 -6
- package/dist/captcha.d.ts +77 -0
- package/dist/captcha.js +334 -0
- package/dist/cdp-relay.d.ts +20 -0
- package/dist/cdp-relay.js +101 -0
- package/dist/chrome-features.d.ts +23 -0
- package/dist/chrome-features.js +44 -0
- package/dist/cli/background.d.ts +11 -0
- package/dist/cli/background.js +132 -0
- package/dist/cli/commands.d.ts +16 -0
- package/dist/cli/commands.js +515 -0
- package/dist/cli/context.d.ts +75 -0
- package/dist/cli/context.js +44 -0
- package/dist/cli/do-background.d.ts +27 -0
- package/dist/cli/do-background.js +212 -0
- package/dist/cli/do.d.ts +85 -0
- package/dist/cli/do.js +558 -0
- package/dist/cli/flows.d.ts +28 -0
- package/dist/cli/flows.js +303 -0
- package/dist/cli/identity.d.ts +37 -0
- package/dist/cli/identity.js +906 -0
- package/dist/cli/live.d.ts +7 -0
- package/dist/cli/live.js +99 -0
- package/dist/cli/main.d.ts +16 -0
- package/dist/cli/main.js +171 -0
- package/dist/cli/secret.d.ts +4 -0
- package/dist/cli/secret.js +151 -0
- package/dist/cli/session.d.ts +6 -0
- package/dist/cli/session.js +106 -0
- package/dist/cli/setup.d.ts +7 -0
- package/dist/cli/setup.js +180 -0
- package/dist/cli/vault.d.ts +5 -0
- package/dist/cli/vault.js +192 -0
- package/dist/client.d.ts +82 -0
- package/dist/client.js +303 -0
- package/dist/diagnose.d.ts +7 -0
- package/dist/diagnose.js +61 -6
- package/dist/doctor.d.ts +37 -3
- package/dist/doctor.js +264 -6
- package/dist/engine-attach.d.ts +27 -0
- package/dist/engine-attach.js +165 -0
- package/dist/engine-camoufox.d.ts +32 -0
- package/dist/engine-camoufox.js +271 -0
- package/dist/engine-lightpanda.d.ts +72 -0
- package/dist/engine-lightpanda.js +293 -0
- package/dist/engine.d.ts +63 -11
- package/dist/engine.js +106 -29
- package/dist/flows.d.ts +127 -0
- package/dist/flows.js +294 -0
- package/dist/governor.d.ts +42 -7
- package/dist/governor.js +82 -32
- package/dist/health.js +2 -1
- package/dist/host.d.ts +38 -2
- package/dist/host.js +689 -52
- package/dist/human-input.d.ts +84 -0
- package/dist/human-input.js +448 -0
- package/dist/humanize.d.ts +123 -0
- package/dist/humanize.js +241 -0
- package/dist/identities.js +4 -1
- package/dist/idle-timer.d.ts +12 -0
- package/dist/idle-timer.js +25 -0
- package/dist/index.d.ts +13 -4
- package/dist/index.js +7 -1
- package/dist/lifecycle.d.ts +11 -1
- package/dist/lifecycle.js +11 -2
- package/dist/live-client.d.ts +22 -0
- package/dist/live-client.js +95 -0
- package/dist/live-page.d.ts +4 -0
- package/dist/live-page.js +144 -0
- package/dist/live-server.d.ts +173 -0
- package/dist/live-server.js +643 -0
- package/dist/live.d.ts +120 -0
- package/dist/live.js +204 -0
- package/dist/login-tool.d.ts +13 -0
- package/dist/login-tool.js +8 -3
- package/dist/login.d.ts +4 -1
- package/dist/login.js +12 -2
- package/dist/names.d.ts +28 -0
- package/dist/names.js +25 -0
- package/dist/network-chrome.d.ts +74 -0
- package/dist/network-chrome.js +178 -0
- package/dist/network.d.ts +125 -0
- package/dist/network.js +264 -0
- package/dist/opt-in-settings.d.ts +69 -0
- package/dist/opt-in-settings.js +152 -0
- package/dist/recorder.d.ts +88 -0
- package/dist/recorder.js +538 -0
- package/dist/recordings.d.ts +116 -0
- package/dist/recordings.js +474 -0
- package/dist/redact.d.ts +7 -0
- package/dist/redact.js +18 -0
- package/dist/replay.d.ts +20 -0
- package/dist/replay.js +383 -0
- package/dist/reputation.d.ts +24 -2
- package/dist/reputation.js +63 -17
- package/dist/runtime-lock.js +3 -2
- package/dist/session-export.js +1 -1
- package/dist/session.d.ts +10 -2
- package/dist/session.js +10 -2
- package/dist/settings.d.ts +104 -2
- package/dist/settings.js +204 -6
- package/dist/sharing.d.ts +96 -0
- package/dist/sharing.js +269 -0
- package/dist/snippet.d.ts +8 -2
- package/dist/snippet.js +10 -4
- package/dist/tab-locks.d.ts +63 -0
- package/dist/tab-locks.js +179 -0
- package/dist/tools.d.ts +38 -0
- package/dist/tools.js +20 -1
- package/dist/tuning.d.ts +14 -0
- package/dist/tuning.js +9 -0
- package/dist/upstream.d.ts +11 -0
- package/dist/vault.js +5 -4
- package/npm-shrinkwrap.json +6 -5
- package/package.json +13 -7
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import type { Locator, Page } from "patchright-core";
|
|
2
|
+
import { type ActionLevel, type HumanizeSettings, type Persona, type Point } from "./humanize.js";
|
|
3
|
+
import type { UpstreamTool } from "./upstream.js";
|
|
4
|
+
/**
|
|
5
|
+
* Human-like input, the playing half: plays the plans of `humanize.ts` on a page through
|
|
6
|
+
* Playwright's own `page.mouse` and `page.keyboard`, so every event still goes through Chrome's
|
|
7
|
+
* input pipeline (CDP `Input.*`, `isTrusted` events) and every action keeps Playwright's checks
|
|
8
|
+
* (visible, stable, enabled, not covered).
|
|
9
|
+
*
|
|
10
|
+
* It works under the stock tools without changing them: `humanizeTools` gives each input tool
|
|
11
|
+
* (click, hover, type, drag, select, fill form, press key and the coordinate tools) a view of
|
|
12
|
+
* the tab whose locators, mouse and keyboard are humanized, and runs upstream's own handler on
|
|
13
|
+
* it. The tool's schema, its result text and its errors stay upstream's, and `browser_batch`,
|
|
14
|
+
* which runs the same tools, gets the same input.
|
|
15
|
+
*
|
|
16
|
+
* Whatever goes wrong before the action itself (an element that is not there yet, a page that
|
|
17
|
+
* navigated) falls back to Playwright's own call, so the error the agent sees is upstream's.
|
|
18
|
+
*/
|
|
19
|
+
/** The stock tools whose input is humanized. */
|
|
20
|
+
export declare const HUMANIZED_TOOLS: ReadonlySet<string>;
|
|
21
|
+
/** The sites the identity met a challenge on in the last 30 days (the reputation log), for `adaptive`. */
|
|
22
|
+
export declare function adaptiveSites(id: string, env: NodeJS.ProcessEnv, now?: number): string[];
|
|
23
|
+
export interface HumanInputOptions {
|
|
24
|
+
/** A stable name for the persona (the identity id); without it the seed or a random one. */
|
|
25
|
+
persona?: string;
|
|
26
|
+
/** Sites (host names) where `adaptive` starts at `full`. */
|
|
27
|
+
challenged?: Iterable<string>;
|
|
28
|
+
sleep?: (ms: number) => Promise<void>;
|
|
29
|
+
now?: () => number;
|
|
30
|
+
}
|
|
31
|
+
export declare class HumanInput {
|
|
32
|
+
readonly settings: HumanizeSettings;
|
|
33
|
+
readonly persona: Persona;
|
|
34
|
+
private readonly rng;
|
|
35
|
+
private readonly sleep;
|
|
36
|
+
private readonly now;
|
|
37
|
+
/** Where the pointer is on each page, so the next move starts there. */
|
|
38
|
+
private readonly pointers;
|
|
39
|
+
/** The URL each page had when it was last acted on, for the reading pause. */
|
|
40
|
+
private readonly seen;
|
|
41
|
+
private readonly challenged;
|
|
42
|
+
/** The real locator behind each humanized one (a drag's target). */
|
|
43
|
+
private readonly originals;
|
|
44
|
+
constructor(settings: HumanizeSettings, options?: HumanInputOptions);
|
|
45
|
+
/** A challenge page on this site: `adaptive` uses `full` there from now on. */
|
|
46
|
+
noteChallenge(site: string): void;
|
|
47
|
+
/** The level of an action on this page. */
|
|
48
|
+
levelFor(page: Page): ActionLevel;
|
|
49
|
+
/** Where the pointer is on this page, if it moved there through this module. */
|
|
50
|
+
pointerOf(page: Page): Point | undefined;
|
|
51
|
+
/** Waits `ms`, or until the plan's clock catches up (each mouse event takes time of its own). */
|
|
52
|
+
private waitUntil;
|
|
53
|
+
private viewport;
|
|
54
|
+
/** Moves the pointer along a planned path to `to`. */
|
|
55
|
+
travel(page: Page, to: Point, targetWidth: number, level: "light" | "full"): Promise<void>;
|
|
56
|
+
/** Scrolls the element into view in wheel notches, with the pointer over the page (`full`). */
|
|
57
|
+
private wheelIntoView;
|
|
58
|
+
private inside;
|
|
59
|
+
/**
|
|
60
|
+
* Brings the element into view and the pointer onto a point inside it. Undefined when that
|
|
61
|
+
* cannot be done now (not there, hidden, zero size): the caller then runs Playwright's own call.
|
|
62
|
+
*/
|
|
63
|
+
private aim;
|
|
64
|
+
private static position;
|
|
65
|
+
/** Types `text` at the focus, key by key. */
|
|
66
|
+
typeText(page: Page, text: string, level: "light" | "full"): Promise<void>;
|
|
67
|
+
/** Clicks into a field unless it has the focus; false when it still does not have it. */
|
|
68
|
+
private focus;
|
|
69
|
+
/** A humanized view of one of the page's locators. */
|
|
70
|
+
wrapLocator(locator: Locator, page: Page): Locator;
|
|
71
|
+
/** A humanized view of the page's mouse (the coordinate tools). */
|
|
72
|
+
private wrapMouse;
|
|
73
|
+
/** A humanized view of the page's keyboard. */
|
|
74
|
+
private wrapKeyboard;
|
|
75
|
+
/** A view of one of upstream's tabs whose locators, mouse and keyboard are humanized. */
|
|
76
|
+
wrapTab<T extends object>(tab: T): T;
|
|
77
|
+
/** A view of upstream's per-session `Context` whose `ensureTab` hands out humanized tabs. */
|
|
78
|
+
wrapContext<T extends object>(context: T): T;
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* The stock tools of one browser, with the input tools running on humanized tabs. Schemas and
|
|
82
|
+
* every other tool are upstream's own objects.
|
|
83
|
+
*/
|
|
84
|
+
export declare function humanizeTools<T extends UpstreamTool>(tools: readonly T[], input: HumanInput): T[];
|
|
@@ -0,0 +1,448 @@
|
|
|
1
|
+
import { randomInt } from "node:crypto";
|
|
2
|
+
import { MAX_TYPED, Rng, clickTiming, hashSeed, keyDelays, keyHold, personaFor, pickPoint, planMove, planWheel, readingPause, } from "./humanize.js";
|
|
3
|
+
import { readChallenges } from "./reputation.js";
|
|
4
|
+
/**
|
|
5
|
+
* Human-like input, the playing half: plays the plans of `humanize.ts` on a page through
|
|
6
|
+
* Playwright's own `page.mouse` and `page.keyboard`, so every event still goes through Chrome's
|
|
7
|
+
* input pipeline (CDP `Input.*`, `isTrusted` events) and every action keeps Playwright's checks
|
|
8
|
+
* (visible, stable, enabled, not covered).
|
|
9
|
+
*
|
|
10
|
+
* It works under the stock tools without changing them: `humanizeTools` gives each input tool
|
|
11
|
+
* (click, hover, type, drag, select, fill form, press key and the coordinate tools) a view of
|
|
12
|
+
* the tab whose locators, mouse and keyboard are humanized, and runs upstream's own handler on
|
|
13
|
+
* it. The tool's schema, its result text and its errors stay upstream's, and `browser_batch`,
|
|
14
|
+
* which runs the same tools, gets the same input.
|
|
15
|
+
*
|
|
16
|
+
* Whatever goes wrong before the action itself (an element that is not there yet, a page that
|
|
17
|
+
* navigated) falls back to Playwright's own call, so the error the agent sees is upstream's.
|
|
18
|
+
*/
|
|
19
|
+
/** The stock tools whose input is humanized. */
|
|
20
|
+
export const HUMANIZED_TOOLS = new Set([
|
|
21
|
+
"browser_click",
|
|
22
|
+
"browser_hover",
|
|
23
|
+
"browser_drag",
|
|
24
|
+
"browser_type",
|
|
25
|
+
"browser_fill_form",
|
|
26
|
+
"browser_select_option",
|
|
27
|
+
"browser_press_key",
|
|
28
|
+
"browser_press_sequentially",
|
|
29
|
+
"browser_mouse_move_xy",
|
|
30
|
+
"browser_mouse_click_xy",
|
|
31
|
+
"browser_mouse_drag_xy",
|
|
32
|
+
"browser_mouse_wheel",
|
|
33
|
+
]);
|
|
34
|
+
/**
|
|
35
|
+
* How long the steps before an action wait for its element: past it, Playwright's own call runs
|
|
36
|
+
* (a plain fill or click, without the human-like path). 1 second was too short on a busy machine,
|
|
37
|
+
* where reading the field or aiming at it can take longer and the action quietly lost its
|
|
38
|
+
* humanize; the wait only runs out when the element is missing, so a longer one costs little.
|
|
39
|
+
*/
|
|
40
|
+
const PREPARE_TIMEOUT_MS = 3000;
|
|
41
|
+
/** A scroll longer than this is a jump (a person drags the scroll bar or presses End). */
|
|
42
|
+
const MAX_WHEEL_PX = 3000;
|
|
43
|
+
/** How far back the reputation log makes `adaptive` use `full` on a site. */
|
|
44
|
+
const ADAPTIVE_WINDOW_MS = 30 * 86_400_000;
|
|
45
|
+
/** The sites the identity met a challenge on in the last 30 days (the reputation log), for `adaptive`. */
|
|
46
|
+
export function adaptiveSites(id, env, now = Date.now()) {
|
|
47
|
+
try {
|
|
48
|
+
const recent = readChallenges(id, env).filter((e) => now - Date.parse(e.ts) <= ADAPTIVE_WINDOW_MS);
|
|
49
|
+
return [...new Set(recent.map((e) => e.site))];
|
|
50
|
+
}
|
|
51
|
+
catch {
|
|
52
|
+
return [];
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* A view of `target` where the names in `overrides` are replaced. Every other method is bound
|
|
57
|
+
* to the target itself, so private fields and `this` inside Playwright's classes keep working.
|
|
58
|
+
*/
|
|
59
|
+
function overlay(target, overrides) {
|
|
60
|
+
return new Proxy(target, {
|
|
61
|
+
get(t, prop) {
|
|
62
|
+
if (typeof prop === "string" && Object.hasOwn(overrides, prop))
|
|
63
|
+
return overrides[prop];
|
|
64
|
+
const value = Reflect.get(t, prop, t);
|
|
65
|
+
return typeof value === "function" ? value.bind(t) : value;
|
|
66
|
+
},
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
const defaultSleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
70
|
+
/** True for a character Playwright's keyboard layout has a key for: printable ASCII. */
|
|
71
|
+
const onKeyboard = (ch) => ch.length === 1 && ch >= " " && ch <= "~";
|
|
72
|
+
/** What a type action needs to know about its element. Runs in the page. */
|
|
73
|
+
function fieldState(el) {
|
|
74
|
+
const TEXT_TYPES = ["", "text", "search", "email", "password", "tel", "url", "number"];
|
|
75
|
+
const active = document.activeElement;
|
|
76
|
+
const focused = active === el || (active !== null && el.contains(active));
|
|
77
|
+
if (el instanceof HTMLTextAreaElement)
|
|
78
|
+
return { text: true, focused, empty: el.value === "" };
|
|
79
|
+
if (el instanceof HTMLInputElement)
|
|
80
|
+
return { text: TEXT_TYPES.includes(el.type.toLowerCase()), focused, empty: el.value === "" };
|
|
81
|
+
if (el instanceof HTMLElement && el.isContentEditable)
|
|
82
|
+
return { text: true, focused, empty: (el.textContent ?? "") === "" };
|
|
83
|
+
return { text: false, focused, empty: true };
|
|
84
|
+
}
|
|
85
|
+
function isFocused(el) {
|
|
86
|
+
const active = document.activeElement;
|
|
87
|
+
return active === el || (active !== null && el.contains(active));
|
|
88
|
+
}
|
|
89
|
+
export class HumanInput {
|
|
90
|
+
settings;
|
|
91
|
+
persona;
|
|
92
|
+
rng;
|
|
93
|
+
sleep;
|
|
94
|
+
now;
|
|
95
|
+
/** Where the pointer is on each page, so the next move starts there. */
|
|
96
|
+
pointers = new WeakMap();
|
|
97
|
+
/** The URL each page had when it was last acted on, for the reading pause. */
|
|
98
|
+
seen = new WeakMap();
|
|
99
|
+
challenged;
|
|
100
|
+
/** The real locator behind each humanized one (a drag's target). */
|
|
101
|
+
originals = new WeakMap();
|
|
102
|
+
constructor(settings, options = {}) {
|
|
103
|
+
this.settings = settings;
|
|
104
|
+
const seed = settings.seed ?? randomInt(0, 2 ** 32);
|
|
105
|
+
this.rng = new Rng(seed);
|
|
106
|
+
this.persona = personaFor(settings.seed ?? (options.persona !== undefined ? hashSeed(options.persona) : seed));
|
|
107
|
+
this.sleep = options.sleep ?? defaultSleep;
|
|
108
|
+
this.now = options.now ?? Date.now;
|
|
109
|
+
this.challenged = new Set(options.challenged ?? []);
|
|
110
|
+
}
|
|
111
|
+
/** A challenge page on this site: `adaptive` uses `full` there from now on. */
|
|
112
|
+
noteChallenge(site) {
|
|
113
|
+
this.challenged.add(site);
|
|
114
|
+
}
|
|
115
|
+
/** The level of an action on this page. */
|
|
116
|
+
levelFor(page) {
|
|
117
|
+
const { level } = this.settings;
|
|
118
|
+
if (level !== "adaptive")
|
|
119
|
+
return level;
|
|
120
|
+
try {
|
|
121
|
+
return this.challenged.has(new URL(page.url()).hostname) ? "full" : "light";
|
|
122
|
+
}
|
|
123
|
+
catch {
|
|
124
|
+
return "light";
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
/** Where the pointer is on this page, if it moved there through this module. */
|
|
128
|
+
pointerOf(page) {
|
|
129
|
+
return this.pointers.get(page);
|
|
130
|
+
}
|
|
131
|
+
/** Waits `ms`, or until the plan's clock catches up (each mouse event takes time of its own). */
|
|
132
|
+
async waitUntil(start, at) {
|
|
133
|
+
const lag = at - (this.now() - start);
|
|
134
|
+
if (lag > 0.5)
|
|
135
|
+
await this.sleep(lag);
|
|
136
|
+
}
|
|
137
|
+
async viewport(page) {
|
|
138
|
+
const size = page.viewportSize();
|
|
139
|
+
if (size)
|
|
140
|
+
return size;
|
|
141
|
+
return await page.evaluate(() => ({ width: window.innerWidth, height: window.innerHeight }));
|
|
142
|
+
}
|
|
143
|
+
/** Moves the pointer along a planned path to `to`. */
|
|
144
|
+
async travel(page, to, targetWidth, level) {
|
|
145
|
+
if (level === "full" && this.seen.get(page) !== page.url())
|
|
146
|
+
await this.sleep(readingPause(this.rng));
|
|
147
|
+
this.seen.set(page, page.url());
|
|
148
|
+
let from = this.pointers.get(page);
|
|
149
|
+
if (!from) {
|
|
150
|
+
// A page the pointer has not been on: it comes in from somewhere in the middle, never (0, 0).
|
|
151
|
+
const vp = await this.viewport(page);
|
|
152
|
+
from = { x: vp.width * this.rng.range(0.3, 0.7), y: vp.height * this.rng.range(0.3, 0.7) };
|
|
153
|
+
}
|
|
154
|
+
const steps = planMove(from, to, targetWidth, level, this.rng, this.persona);
|
|
155
|
+
const start = this.now();
|
|
156
|
+
let at = 0;
|
|
157
|
+
for (const step of steps) {
|
|
158
|
+
at += step.wait;
|
|
159
|
+
await this.waitUntil(start, at);
|
|
160
|
+
await page.mouse.move(step.x, step.y);
|
|
161
|
+
this.pointers.set(page, { x: step.x, y: step.y });
|
|
162
|
+
}
|
|
163
|
+
this.pointers.set(page, to);
|
|
164
|
+
}
|
|
165
|
+
/** Scrolls the element into view in wheel notches, with the pointer over the page (`full`). */
|
|
166
|
+
async wheelIntoView(page, locator) {
|
|
167
|
+
const vp = await this.viewport(page);
|
|
168
|
+
for (let round = 0; round < 3; round++) {
|
|
169
|
+
const box = await locator.boundingBox({ timeout: PREPARE_TIMEOUT_MS });
|
|
170
|
+
if (!box)
|
|
171
|
+
return;
|
|
172
|
+
const center = box.y + box.height / 2;
|
|
173
|
+
if (center >= vp.height * 0.1 && center <= vp.height * 0.9)
|
|
174
|
+
return;
|
|
175
|
+
const dy = Math.round(center - vp.height * this.rng.range(0.35, 0.55));
|
|
176
|
+
if (Math.abs(dy) > MAX_WHEEL_PX)
|
|
177
|
+
return;
|
|
178
|
+
if (!this.pointers.has(page))
|
|
179
|
+
await this.travel(page, this.inside(vp), 200, "full");
|
|
180
|
+
const start = this.now();
|
|
181
|
+
let at = 0;
|
|
182
|
+
for (const step of planWheel(0, dy, this.rng)) {
|
|
183
|
+
at += step.wait;
|
|
184
|
+
await this.waitUntil(start, at);
|
|
185
|
+
await page.mouse.wheel(step.dx, step.dy);
|
|
186
|
+
}
|
|
187
|
+
// Chrome animates a wheel scroll for about 150 ms.
|
|
188
|
+
await this.sleep(200);
|
|
189
|
+
const after = await locator.boundingBox({ timeout: PREPARE_TIMEOUT_MS });
|
|
190
|
+
if (!after || Math.abs(after.y - box.y) < 1)
|
|
191
|
+
return; // the wheel does not scroll it: Playwright will
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
inside(vp) {
|
|
195
|
+
return { x: vp.width * this.rng.range(0.35, 0.65), y: vp.height * this.rng.range(0.35, 0.65) };
|
|
196
|
+
}
|
|
197
|
+
/**
|
|
198
|
+
* Brings the element into view and the pointer onto a point inside it. Undefined when that
|
|
199
|
+
* cannot be done now (not there, hidden, zero size): the caller then runs Playwright's own call.
|
|
200
|
+
*/
|
|
201
|
+
async aim(page, locator, level) {
|
|
202
|
+
try {
|
|
203
|
+
if (level === "full")
|
|
204
|
+
await this.wheelIntoView(page, locator);
|
|
205
|
+
await locator.scrollIntoViewIfNeeded({ timeout: PREPARE_TIMEOUT_MS });
|
|
206
|
+
const box = await locator.boundingBox({ timeout: PREPARE_TIMEOUT_MS });
|
|
207
|
+
if (!box || box.width < 1 || box.height < 1)
|
|
208
|
+
return undefined;
|
|
209
|
+
const point = pickPoint(box, this.rng);
|
|
210
|
+
await this.travel(page, point, Math.min(box.width, box.height), level);
|
|
211
|
+
return { point, box };
|
|
212
|
+
}
|
|
213
|
+
catch {
|
|
214
|
+
return undefined;
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
static position(at) {
|
|
218
|
+
return { x: at.point.x - at.box.x, y: at.point.y - at.box.y };
|
|
219
|
+
}
|
|
220
|
+
/** Types `text` at the focus, key by key. */
|
|
221
|
+
async typeText(page, text, level) {
|
|
222
|
+
const chars = [...text];
|
|
223
|
+
const delays = keyDelays(text, level, this.rng, this.persona);
|
|
224
|
+
let held = 0;
|
|
225
|
+
for (let i = 0; i < chars.length; i++) {
|
|
226
|
+
const ch = chars[i];
|
|
227
|
+
const hold = keyHold(level, this.rng);
|
|
228
|
+
// The previous key's hold already took part of the gap.
|
|
229
|
+
await this.sleep(Math.max(0, delays[i] - held));
|
|
230
|
+
held = hold;
|
|
231
|
+
if (ch === "\n")
|
|
232
|
+
await page.keyboard.press("Enter", { delay: hold });
|
|
233
|
+
else if (onKeyboard(ch))
|
|
234
|
+
await page.keyboard.press(ch, { delay: hold });
|
|
235
|
+
else
|
|
236
|
+
await page.keyboard.insertText(ch); // no key for it on the layout: like an IME commit
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
/** Clicks into a field unless it has the focus; false when it still does not have it. */
|
|
240
|
+
async focus(page, locator, level, focused) {
|
|
241
|
+
if (focused)
|
|
242
|
+
return true;
|
|
243
|
+
const at = await this.aim(page, locator, level);
|
|
244
|
+
if (!at)
|
|
245
|
+
return false;
|
|
246
|
+
const timing = clickTiming(level, this.rng);
|
|
247
|
+
await this.sleep(timing.beforePress);
|
|
248
|
+
try {
|
|
249
|
+
await locator.click({ position: HumanInput.position(at), delay: timing.press, timeout: PREPARE_TIMEOUT_MS });
|
|
250
|
+
return await locator.evaluate(isFocused, undefined, { timeout: PREPARE_TIMEOUT_MS });
|
|
251
|
+
}
|
|
252
|
+
catch {
|
|
253
|
+
return false;
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
/** A humanized view of one of the page's locators. */
|
|
257
|
+
wrapLocator(locator, page) {
|
|
258
|
+
const original = locator;
|
|
259
|
+
const level = () => this.levelFor(page);
|
|
260
|
+
const wrapped = overlay(locator, {
|
|
261
|
+
click: async (options) => {
|
|
262
|
+
const l = level();
|
|
263
|
+
if (l === "off")
|
|
264
|
+
return await original.click(options);
|
|
265
|
+
const at = await this.aim(page, original, l);
|
|
266
|
+
if (!at)
|
|
267
|
+
return await original.click(options);
|
|
268
|
+
const timing = clickTiming(l, this.rng);
|
|
269
|
+
await this.sleep(timing.beforePress);
|
|
270
|
+
await original.click({ ...options, position: HumanInput.position(at), delay: timing.press });
|
|
271
|
+
},
|
|
272
|
+
dblclick: async (options) => {
|
|
273
|
+
const l = level();
|
|
274
|
+
if (l === "off")
|
|
275
|
+
return await original.dblclick(options);
|
|
276
|
+
const at = await this.aim(page, original, l);
|
|
277
|
+
if (!at)
|
|
278
|
+
return await original.dblclick(options);
|
|
279
|
+
const timing = clickTiming(l, this.rng);
|
|
280
|
+
await this.sleep(timing.beforePress);
|
|
281
|
+
await original.dblclick({ ...options, position: HumanInput.position(at), delay: timing.press });
|
|
282
|
+
},
|
|
283
|
+
hover: async (options) => {
|
|
284
|
+
const l = level();
|
|
285
|
+
const at = l === "off" ? undefined : await this.aim(page, original, l);
|
|
286
|
+
await original.hover(at ? { ...options, position: HumanInput.position(at) } : options);
|
|
287
|
+
},
|
|
288
|
+
setChecked: async (checked, options) => {
|
|
289
|
+
const l = level();
|
|
290
|
+
const at = l === "off" ? undefined : await this.aim(page, original, l);
|
|
291
|
+
await original.setChecked(checked, at ? { ...options, position: HumanInput.position(at) } : options);
|
|
292
|
+
},
|
|
293
|
+
selectOption: async (values, options) => {
|
|
294
|
+
const l = level();
|
|
295
|
+
if (l !== "off")
|
|
296
|
+
await this.aim(page, original, l);
|
|
297
|
+
return await original.selectOption(values, options);
|
|
298
|
+
},
|
|
299
|
+
fill: async (value, options) => {
|
|
300
|
+
const l = level();
|
|
301
|
+
if (l === "off" || [...value].length > MAX_TYPED)
|
|
302
|
+
return await original.fill(value, options);
|
|
303
|
+
const state = await original
|
|
304
|
+
.evaluate(fieldState, undefined, { timeout: PREPARE_TIMEOUT_MS })
|
|
305
|
+
.catch(() => undefined);
|
|
306
|
+
if (!state?.text || !(await this.focus(page, original, l, state.focused)))
|
|
307
|
+
return await original.fill(value, options);
|
|
308
|
+
if (!state.empty) {
|
|
309
|
+
// Typing over a selection replaces it, as fill does.
|
|
310
|
+
await page.keyboard.press("ControlOrMeta+a", { delay: keyHold(l, this.rng) });
|
|
311
|
+
if (value === "")
|
|
312
|
+
return await page.keyboard.press("Backspace", { delay: keyHold(l, this.rng) });
|
|
313
|
+
}
|
|
314
|
+
await this.typeText(page, value, l);
|
|
315
|
+
},
|
|
316
|
+
pressSequentially: async (text, options) => {
|
|
317
|
+
const l = level();
|
|
318
|
+
if (l === "off" || [...text].length > MAX_TYPED)
|
|
319
|
+
return await original.pressSequentially(text, options);
|
|
320
|
+
const focused = await original
|
|
321
|
+
.evaluate(isFocused, undefined, { timeout: PREPARE_TIMEOUT_MS })
|
|
322
|
+
.catch(() => undefined);
|
|
323
|
+
if (focused === undefined || !(await this.focus(page, original, l, focused)))
|
|
324
|
+
return await original.pressSequentially(text, options);
|
|
325
|
+
await this.typeText(page, text, l);
|
|
326
|
+
},
|
|
327
|
+
press: async (key, options) => {
|
|
328
|
+
const l = level();
|
|
329
|
+
if (l === "off")
|
|
330
|
+
return await original.press(key, options);
|
|
331
|
+
await this.sleep(clickTiming(l, this.rng).beforePress);
|
|
332
|
+
await original.press(key, { ...options, delay: keyHold(l, this.rng) });
|
|
333
|
+
},
|
|
334
|
+
dragTo: async (target, options) => {
|
|
335
|
+
const to = this.originals.get(target) ?? target;
|
|
336
|
+
const l = level();
|
|
337
|
+
if (l === "off")
|
|
338
|
+
return await original.dragTo(to, options);
|
|
339
|
+
const from = await this.aim(page, original, l);
|
|
340
|
+
if (!from)
|
|
341
|
+
return await original.dragTo(to, options);
|
|
342
|
+
const timing = clickTiming(l, this.rng);
|
|
343
|
+
await this.sleep(timing.beforePress);
|
|
344
|
+
await page.mouse.down();
|
|
345
|
+
let box = null;
|
|
346
|
+
try {
|
|
347
|
+
await this.sleep(this.rng.range(80, 200));
|
|
348
|
+
box = await to.boundingBox({ timeout: PREPARE_TIMEOUT_MS });
|
|
349
|
+
if (box)
|
|
350
|
+
await this.travel(page, pickPoint(box, this.rng), Math.min(box.width, box.height), l);
|
|
351
|
+
await this.sleep(this.rng.range(60, 150));
|
|
352
|
+
}
|
|
353
|
+
finally {
|
|
354
|
+
await page.mouse.up();
|
|
355
|
+
}
|
|
356
|
+
if (!box)
|
|
357
|
+
await original.dragTo(to, options);
|
|
358
|
+
},
|
|
359
|
+
});
|
|
360
|
+
this.originals.set(wrapped, original);
|
|
361
|
+
return wrapped;
|
|
362
|
+
}
|
|
363
|
+
/** A humanized view of the page's mouse (the coordinate tools). */
|
|
364
|
+
wrapMouse(page) {
|
|
365
|
+
const mouse = page.mouse;
|
|
366
|
+
return overlay(mouse, {
|
|
367
|
+
move: async (x, y, options) => {
|
|
368
|
+
const l = this.levelFor(page);
|
|
369
|
+
if (l === "off")
|
|
370
|
+
return await mouse.move(x, y, options);
|
|
371
|
+
await this.travel(page, { x, y }, 40, l);
|
|
372
|
+
},
|
|
373
|
+
click: async (x, y, options) => {
|
|
374
|
+
const l = this.levelFor(page);
|
|
375
|
+
if (l === "off")
|
|
376
|
+
return await mouse.click(x, y, options);
|
|
377
|
+
await this.travel(page, { x, y }, 40, l);
|
|
378
|
+
const timing = clickTiming(l, this.rng);
|
|
379
|
+
await this.sleep(timing.beforePress);
|
|
380
|
+
await mouse.click(x, y, { ...options, delay: options?.delay ?? timing.press });
|
|
381
|
+
},
|
|
382
|
+
wheel: async (dx, dy) => {
|
|
383
|
+
if (this.levelFor(page) !== "full")
|
|
384
|
+
return await mouse.wheel(dx, dy);
|
|
385
|
+
const start = this.now();
|
|
386
|
+
let at = 0;
|
|
387
|
+
for (const step of planWheel(dx, dy, this.rng)) {
|
|
388
|
+
at += step.wait;
|
|
389
|
+
await this.waitUntil(start, at);
|
|
390
|
+
await mouse.wheel(step.dx, step.dy);
|
|
391
|
+
}
|
|
392
|
+
},
|
|
393
|
+
});
|
|
394
|
+
}
|
|
395
|
+
/** A humanized view of the page's keyboard. */
|
|
396
|
+
wrapKeyboard(page) {
|
|
397
|
+
const keyboard = page.keyboard;
|
|
398
|
+
return overlay(keyboard, {
|
|
399
|
+
press: async (key, options) => {
|
|
400
|
+
const l = this.levelFor(page);
|
|
401
|
+
if (l === "off")
|
|
402
|
+
return await keyboard.press(key, options);
|
|
403
|
+
await keyboard.press(key, { ...options, delay: options?.delay ?? keyHold(l, this.rng) });
|
|
404
|
+
},
|
|
405
|
+
type: async (text, options) => {
|
|
406
|
+
const l = this.levelFor(page);
|
|
407
|
+
if (l === "off" || [...text].length > MAX_TYPED)
|
|
408
|
+
return await keyboard.type(text, options);
|
|
409
|
+
await this.typeText(page, text, l);
|
|
410
|
+
},
|
|
411
|
+
});
|
|
412
|
+
}
|
|
413
|
+
/** A view of one of upstream's tabs whose locators, mouse and keyboard are humanized. */
|
|
414
|
+
wrapTab(tab) {
|
|
415
|
+
const page = tab.page;
|
|
416
|
+
const t = tab;
|
|
417
|
+
const view = overlay(page, { mouse: this.wrapMouse(page), keyboard: this.wrapKeyboard(page) });
|
|
418
|
+
return overlay(tab, {
|
|
419
|
+
page: view,
|
|
420
|
+
targetLocator: async (params) => {
|
|
421
|
+
const r = await t.targetLocator(params);
|
|
422
|
+
return { ...r, locator: this.wrapLocator(r.locator, page) };
|
|
423
|
+
},
|
|
424
|
+
targetLocators: async (params) => (await t.targetLocators(params)).map((r) => ({ ...r, locator: this.wrapLocator(r.locator, page) })),
|
|
425
|
+
});
|
|
426
|
+
}
|
|
427
|
+
/** A view of upstream's per-session `Context` whose `ensureTab` hands out humanized tabs. */
|
|
428
|
+
wrapContext(context) {
|
|
429
|
+
const c = context;
|
|
430
|
+
return overlay(context, {
|
|
431
|
+
ensureTab: async (...args) => this.wrapTab(await c.ensureTab(...args)),
|
|
432
|
+
});
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
/**
|
|
436
|
+
* The stock tools of one browser, with the input tools running on humanized tabs. Schemas and
|
|
437
|
+
* every other tool are upstream's own objects.
|
|
438
|
+
*/
|
|
439
|
+
export function humanizeTools(tools, input) {
|
|
440
|
+
return tools.map((tool) => {
|
|
441
|
+
if (!HUMANIZED_TOOLS.has(tool.schema.name))
|
|
442
|
+
return tool;
|
|
443
|
+
const handled = tool;
|
|
444
|
+
const handle = (context, params, response, signal) => handled.handle(input.wrapContext(context), params, response, signal);
|
|
445
|
+
return { ...tool, handle };
|
|
446
|
+
});
|
|
447
|
+
}
|
|
448
|
+
//# sourceMappingURL=human-input.js.map
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Human-like input, the planning half: where the pointer goes and when, how long a key or a
|
|
3
|
+
* button stays down, how a scroll breaks into wheel notches. Pure functions of a seeded random
|
|
4
|
+
* source, so a test with a seed gets the same plan every run; `human-input.ts` plays a plan on a
|
|
5
|
+
* page.
|
|
6
|
+
*
|
|
7
|
+
* Levels (identity.json `humanize.level`, off by default):
|
|
8
|
+
*
|
|
9
|
+
* - `off`: Playwright's own input, as before: one `mousemove` to the exact center of the target,
|
|
10
|
+
* button down and up at once, text inserted in one go.
|
|
11
|
+
* - `light`: the pointer travels a curve from where it last was to a point inside the target, at
|
|
12
|
+
* a steady pace with small timing jitter; the button stays down for tens of milliseconds;
|
|
13
|
+
* short text is typed key by key with per-key delays.
|
|
14
|
+
* - `full`: `light` plus a duration that grows with distance and shrinks with target size
|
|
15
|
+
* (Fitts' law), a slow-fast-slow speed profile, overshoot and correction on long moves, a
|
|
16
|
+
* short pause to read a page the first time it is acted on, longer pauses between words, and
|
|
17
|
+
* scrolling to an element out of view in wheel notches instead of one jump.
|
|
18
|
+
* - `adaptive`: `light`, and `full` on a site after the identity met a challenge page there (the
|
|
19
|
+
* reputation log, or this session).
|
|
20
|
+
*
|
|
21
|
+
* The numbers come from published measurements: a mouse move to a 20 px target 630 px away takes
|
|
22
|
+
* a person about 750-850 ms; the average typist types 52 words a minute (about 230 ms a
|
|
23
|
+
* character, the fast ones about 100 ms); a button press lasts tens of milliseconds to about 150.
|
|
24
|
+
* Typing faster than a few milliseconds a key is physically impossible, so long text is pasted
|
|
25
|
+
* instead (`MAX_TYPED`), like a person pasting it.
|
|
26
|
+
*/
|
|
27
|
+
export type HumanizeLevel = "off" | "light" | "full" | "adaptive";
|
|
28
|
+
/** The levels a single action runs at, once `adaptive` is resolved. */
|
|
29
|
+
export type ActionLevel = "off" | "light" | "full";
|
|
30
|
+
export declare const HUMANIZE_LEVELS: readonly HumanizeLevel[];
|
|
31
|
+
/** identity.json `humanize`. */
|
|
32
|
+
export interface HumanizeSettings {
|
|
33
|
+
level: HumanizeLevel;
|
|
34
|
+
/** Makes every plan repeatable (tests, debugging). Without it each session draws its own. */
|
|
35
|
+
seed?: number;
|
|
36
|
+
}
|
|
37
|
+
/** Text longer than this is pasted (Playwright's `fill`), not typed. */
|
|
38
|
+
export declare const MAX_TYPED = 80;
|
|
39
|
+
export interface Point {
|
|
40
|
+
x: number;
|
|
41
|
+
y: number;
|
|
42
|
+
}
|
|
43
|
+
export interface Box extends Point {
|
|
44
|
+
width: number;
|
|
45
|
+
height: number;
|
|
46
|
+
}
|
|
47
|
+
/** One pointer position, reached `wait` ms after the previous one. */
|
|
48
|
+
export interface Step extends Point {
|
|
49
|
+
wait: number;
|
|
50
|
+
}
|
|
51
|
+
/** A seeded source of random numbers (mulberry32): small, fast and the same on every platform. */
|
|
52
|
+
export declare class Rng {
|
|
53
|
+
private state;
|
|
54
|
+
constructor(seed: number);
|
|
55
|
+
/** A number in [0, 1). */
|
|
56
|
+
next(): number;
|
|
57
|
+
range(min: number, max: number): number;
|
|
58
|
+
chance(p: number): boolean;
|
|
59
|
+
/** A normal draw (Box-Muller), clamped to [min, max]. */
|
|
60
|
+
normal(mean: number, sd: number, min?: number, max?: number): number;
|
|
61
|
+
}
|
|
62
|
+
/** A 32-bit FNV-1a hash, for a seed from a name. */
|
|
63
|
+
export declare function hashSeed(text: string): number;
|
|
64
|
+
/**
|
|
65
|
+
* The stable traits of one identity's "hand": the same identity moves and types at the same
|
|
66
|
+
* pace in every session (a person whose rhythm changed from one day to the next would stand out
|
|
67
|
+
* more than one who is a little slow).
|
|
68
|
+
*/
|
|
69
|
+
export interface Persona {
|
|
70
|
+
/** Scales pointer speed: above 1 is faster. */
|
|
71
|
+
speed: number;
|
|
72
|
+
/** Mean time between two keys, in ms. */
|
|
73
|
+
keyMs: number;
|
|
74
|
+
/** Which way the pointer's curves tend to bend: 1 or -1. */
|
|
75
|
+
bend: 1 | -1;
|
|
76
|
+
}
|
|
77
|
+
export declare function personaFor(seed: number): Persona;
|
|
78
|
+
/** How long a move takes at each level, before the persona's speed. */
|
|
79
|
+
export declare function moveDuration(distance: number, targetWidth: number, level: "light" | "full", rng: Rng): number;
|
|
80
|
+
/**
|
|
81
|
+
* The pointer path from `from` to `to` (which ends exactly on `to`), with the wait before each
|
|
82
|
+
* position. `targetWidth` is the smaller side of the target, for Fitts' law.
|
|
83
|
+
*/
|
|
84
|
+
export declare function planMove(from: Point, to: Point, targetWidth: number, level: "light" | "full", rng: Rng, persona: Persona): Step[];
|
|
85
|
+
/**
|
|
86
|
+
* A point inside the box: near the middle, never the exact center and never on the edge (a
|
|
87
|
+
* person aims at the visible middle of a button, and lands a few pixels off).
|
|
88
|
+
*/
|
|
89
|
+
export declare function pickPoint(box: Box, rng: Rng): Point;
|
|
90
|
+
/** A pause, in ms, at each level. */
|
|
91
|
+
export interface Timing {
|
|
92
|
+
/** Between arriving on the target and pressing the button. */
|
|
93
|
+
beforePress: number;
|
|
94
|
+
/** Button held down. */
|
|
95
|
+
press: number;
|
|
96
|
+
}
|
|
97
|
+
export declare function clickTiming(level: "light" | "full", rng: Rng): Timing;
|
|
98
|
+
/** A pause to look at a page acted on for the first time (`full` only). */
|
|
99
|
+
export declare function readingPause(rng: Rng): number;
|
|
100
|
+
/** How long a key stays down, in ms. */
|
|
101
|
+
export declare function keyHold(level: "light" | "full", rng: Rng): number;
|
|
102
|
+
/**
|
|
103
|
+
* The wait before each character of `text` (the first one included: the hand moves to the
|
|
104
|
+
* keyboard), in ms. `light`: the persona's pace with jitter. `full`: a skewed draw around the
|
|
105
|
+
* pace, faster inside a word than across a space, longer after punctuation, and now and then a
|
|
106
|
+
* pause to think.
|
|
107
|
+
*/
|
|
108
|
+
export declare function keyDelays(text: string, level: "light" | "full", rng: Rng, persona: Persona): number[];
|
|
109
|
+
/** One wheel event and the wait before it. */
|
|
110
|
+
export interface WheelStep {
|
|
111
|
+
dx: number;
|
|
112
|
+
dy: number;
|
|
113
|
+
wait: number;
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* A scroll of (dx, dy) in wheel notches of about 100 px, in short bursts with a pause between
|
|
117
|
+
* them, the way a mouse wheel turns (`full`). The notches add up to exactly (dx, dy).
|
|
118
|
+
*/
|
|
119
|
+
export declare function planWheel(dx: number, dy: number, rng: Rng): WheelStep[];
|
|
120
|
+
/** The total time of a plan, in ms. */
|
|
121
|
+
export declare function planTime(steps: readonly {
|
|
122
|
+
wait: number;
|
|
123
|
+
}[]): number;
|