omowright 0.0.0 → 0.2.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/LICENSE +21 -0
- package/README.md +290 -2
- package/TOOLS.md +487 -0
- package/bin/omowright-cloak.mjs +112 -0
- package/package.json +57 -4
- package/presets/agent-events/SKILL.md +75 -0
- package/presets/captcha/SKILL.md +129 -0
- package/presets/chrome/SKILL.md +88 -0
- package/presets/cloak-bridge/SKILL.md +43 -0
- package/presets/network/SKILL.md +126 -0
- package/presets/visual-browse/SKILL.md +103 -0
- package/skills/omowright/SKILL.md +193 -0
- package/skills/omowright/references/1password.md +118 -0
- package/skills/omowright/references/browserskill.md +195 -0
- package/skills/omowright/references/eval-kernel.md +100 -0
- package/skills/omowright/references/frames-layers-human.md +123 -0
- package/skills/omowright/references/quickstart.md +109 -0
- package/skills/omowright/references/stealth.md +174 -0
- package/src/agent-tabs.js +179 -0
- package/src/aria-hidden.js +47 -0
- package/src/bridge-schemas.js +44 -0
- package/src/bridge-transport.js +83 -0
- package/src/browser-context.js +105 -0
- package/src/browser-process.js +127 -0
- package/src/bsk/browser-signals.js +90 -0
- package/src/bsk/browsers.js +114 -0
- package/src/bsk/connect.js +75 -0
- package/src/bsk/daemon-info.js +100 -0
- package/src/bsk/external-extension.js +110 -0
- package/src/bsk/identify-browser.js +88 -0
- package/src/bsk/install-cli.js +81 -0
- package/src/bsk/ipc-client.js +236 -0
- package/src/bsk/onboard.js +140 -0
- package/src/bsk/screenshot.js +49 -0
- package/src/bsk/session.js +131 -0
- package/src/bsk/snapshot.js +56 -0
- package/src/bsk/targets.js +25 -0
- package/src/captcha-wait.js +79 -0
- package/src/captcha.js +142 -0
- package/src/cdp-errors.js +23 -0
- package/src/cdp-methods.js +69 -0
- package/src/chrome-profile.js +176 -0
- package/src/chrome.js +194 -0
- package/src/cloak-profile.js +266 -0
- package/src/connection.js +234 -0
- package/src/console-events.js +12 -0
- package/src/cookies.js +39 -0
- package/src/core.js +1702 -0
- package/src/cua.js +74 -0
- package/src/dialog-policy.js +80 -0
- package/src/emulate.js +127 -0
- package/src/events.js +111 -0
- package/src/frames-snapshot.js +138 -0
- package/src/har.js +103 -0
- package/src/human-handoff.js +124 -0
- package/src/index.js +89 -0
- package/src/injected.js +3 -0
- package/src/instance-lock.js +74 -0
- package/src/instances.js +490 -0
- package/src/internal-capability.js +1 -0
- package/src/launch-endpoint.js +185 -0
- package/src/layers.js +138 -0
- package/src/llm.js +201 -0
- package/src/lock-backend-win32.js +54 -0
- package/src/lock-backend.js +81 -0
- package/src/native-host-win32.js +201 -0
- package/src/network-events.js +223 -0
- package/src/network-snoop.js +229 -0
- package/src/nul-frames.js +28 -0
- package/src/open-shadow-query.js +101 -0
- package/src/owner-only.js +196 -0
- package/src/page-bundle.js +1478 -0
- package/src/page-events.js +45 -0
- package/src/page-routes.js +94 -0
- package/src/page-setup.js +239 -0
- package/src/pipe.js +418 -0
- package/src/process-start-time.js +64 -0
- package/src/routes.js +256 -0
- package/src/running-browser.js +85 -0
- package/src/scroll-collect.js +73 -0
- package/src/serial-emitter.js +17 -0
- package/src/shared-connections.js +285 -0
- package/src/snapshot-diff.js +75 -0
- package/src/text-matching.js +101 -0
- package/src/trace.js +183 -0
- package/src/url-match.js +148 -0
- package/src/viewport.js +12 -0
package/src/routes.js
ADDED
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Request interception via the CDP Fetch domain.
|
|
3
|
+
*
|
|
4
|
+
* Enabling Fetch changes request timing and blocking patterns that bot
|
|
5
|
+
* defenses can fingerprint. Keep interception off by default and scope
|
|
6
|
+
* patterns tightly.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
const controllers = new WeakMap();
|
|
10
|
+
|
|
11
|
+
function globToRegExp(glob) {
|
|
12
|
+
let source = "";
|
|
13
|
+
for (const char of glob) {
|
|
14
|
+
if (char === "*") source += ".*";
|
|
15
|
+
else if (char === "?") source += ".";
|
|
16
|
+
else if ("\\^$+{}[]()|.".includes(char)) source += `\\${char}`;
|
|
17
|
+
else source += char;
|
|
18
|
+
}
|
|
19
|
+
return new RegExp(`^${source}$`);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function hasGlobChars(value) {
|
|
23
|
+
return value.includes("*") || value.includes("?");
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function matches(match, request) {
|
|
27
|
+
if (typeof match === "function") {
|
|
28
|
+
return Boolean(match({
|
|
29
|
+
url: request.url,
|
|
30
|
+
method: request.method,
|
|
31
|
+
resourceType: request.resourceType,
|
|
32
|
+
headers: request.headers,
|
|
33
|
+
}));
|
|
34
|
+
}
|
|
35
|
+
if (match instanceof RegExp) return match.test(request.url);
|
|
36
|
+
if (typeof match === "string") {
|
|
37
|
+
if (hasGlobChars(match)) return globToRegExp(match).test(request.url);
|
|
38
|
+
return request.url.includes(match);
|
|
39
|
+
}
|
|
40
|
+
return false;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function sameMatch(left, right) {
|
|
44
|
+
if (left === right) return true;
|
|
45
|
+
if (left instanceof RegExp && right instanceof RegExp) return left.source === right.source && left.flags === right.flags;
|
|
46
|
+
return false;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function toHeaderEntries(headers) {
|
|
50
|
+
if (!headers) return [];
|
|
51
|
+
if (Array.isArray(headers)) {
|
|
52
|
+
return headers.map(header => ({ name: String(header.name), value: String(header.value ?? "") }));
|
|
53
|
+
}
|
|
54
|
+
return Object.entries(headers).map(([name, value]) => ({ name, value: String(value ?? "") }));
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function encodeBody(body) {
|
|
58
|
+
if (body == null) return "";
|
|
59
|
+
if (Buffer.isBuffer(body)) return body.toString("base64");
|
|
60
|
+
if (body instanceof Uint8Array) return Buffer.from(body).toString("base64");
|
|
61
|
+
return Buffer.from(String(body)).toString("base64");
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function buildContinue(requestId, { url, method, headers, postData } = {}) {
|
|
65
|
+
const params = { requestId };
|
|
66
|
+
if (url !== undefined) params.url = url;
|
|
67
|
+
if (method !== undefined) params.method = method;
|
|
68
|
+
if (headers !== undefined) params.headers = toHeaderEntries(headers);
|
|
69
|
+
if (postData !== undefined) params.postData = encodeBody(postData);
|
|
70
|
+
return params;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function buildFulfill(requestId, { status = 200, headers = {}, contentType, body = "" } = {}) {
|
|
74
|
+
const responseHeaders = toHeaderEntries(headers);
|
|
75
|
+
if (contentType != null && String(contentType).length > 0) {
|
|
76
|
+
const hasType = responseHeaders.some(header => header.name.toLowerCase() === "content-type");
|
|
77
|
+
if (!hasType) responseHeaders.push({ name: "content-type", value: String(contentType) });
|
|
78
|
+
}
|
|
79
|
+
return {
|
|
80
|
+
requestId,
|
|
81
|
+
responseCode: status,
|
|
82
|
+
responseHeaders,
|
|
83
|
+
body: encodeBody(body),
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export function createRoutes(page) {
|
|
88
|
+
if (!page?.cdp) throw new TypeError("createRoutes requires a page with cdp");
|
|
89
|
+
if (controllers.has(page)) return controllers.get(page);
|
|
90
|
+
|
|
91
|
+
const entries = [];
|
|
92
|
+
const warned = new WeakSet();
|
|
93
|
+
const settledIds = new Set();
|
|
94
|
+
let enabled = false;
|
|
95
|
+
let sessionId;
|
|
96
|
+
let unsubscribe = null;
|
|
97
|
+
let lifecycle = Promise.resolve();
|
|
98
|
+
let activeRequests = 0;
|
|
99
|
+
|
|
100
|
+
function warnOnce(handler, error) {
|
|
101
|
+
if (typeof handler === "function") {
|
|
102
|
+
if (warned.has(handler)) return;
|
|
103
|
+
warned.add(handler);
|
|
104
|
+
}
|
|
105
|
+
if (error) console.warn("[routes] route handler failed; continuing request", error);
|
|
106
|
+
else console.warn("[routes] route handler did not settle the request; continuing");
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
async function handlePaused(params, meta) {
|
|
110
|
+
if (!sessionId || meta?.sessionId !== sessionId) return;
|
|
111
|
+
const requestId = params?.requestId;
|
|
112
|
+
if (!requestId || settledIds.has(requestId)) return;
|
|
113
|
+
settledIds.add(requestId);
|
|
114
|
+
activeRequests += 1;
|
|
115
|
+
|
|
116
|
+
const request = {
|
|
117
|
+
url: params.request?.url ?? "",
|
|
118
|
+
method: params.request?.method ?? "GET",
|
|
119
|
+
headers: params.request?.headers ?? {},
|
|
120
|
+
postData: params.request?.postData,
|
|
121
|
+
resourceType: params.resourceType,
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
let settled = false;
|
|
125
|
+
const settle = (method, cdpParams) => {
|
|
126
|
+
if (settled) return Promise.resolve();
|
|
127
|
+
settled = true;
|
|
128
|
+
settledIds.add(requestId);
|
|
129
|
+
return page.cdp.send(method, cdpParams, sessionId);
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
const candidates = entries.slice();
|
|
133
|
+
let index = 0;
|
|
134
|
+
async function dispatch() {
|
|
135
|
+
while (index < candidates.length) {
|
|
136
|
+
const entry = candidates[index++];
|
|
137
|
+
if (!entries.includes(entry) || !matches(entry.match, request)) continue;
|
|
138
|
+
if (--entry.remaining === 0) entries.splice(entries.indexOf(entry), 1);
|
|
139
|
+
let acted = false;
|
|
140
|
+
let fallbackPromise;
|
|
141
|
+
const route = {
|
|
142
|
+
request,
|
|
143
|
+
continue(overrides = {}) {
|
|
144
|
+
acted = true;
|
|
145
|
+
return settle("Fetch.continueRequest", buildContinue(requestId, overrides));
|
|
146
|
+
},
|
|
147
|
+
fulfill(options = {}) {
|
|
148
|
+
acted = true;
|
|
149
|
+
return settle("Fetch.fulfillRequest", buildFulfill(requestId, options));
|
|
150
|
+
},
|
|
151
|
+
abort(errorReason = "Failed") {
|
|
152
|
+
acted = true;
|
|
153
|
+
return settle("Fetch.failRequest", { requestId, errorReason });
|
|
154
|
+
},
|
|
155
|
+
fallback() {
|
|
156
|
+
if (acted || settled) return Promise.resolve();
|
|
157
|
+
acted = true;
|
|
158
|
+
fallbackPromise = dispatch();
|
|
159
|
+
return fallbackPromise;
|
|
160
|
+
},
|
|
161
|
+
};
|
|
162
|
+
try {
|
|
163
|
+
await entry.handler(route);
|
|
164
|
+
await fallbackPromise;
|
|
165
|
+
} catch (error) {
|
|
166
|
+
warnOnce(entry.handler, error);
|
|
167
|
+
}
|
|
168
|
+
if (!acted && !settled) warnOnce(entry.handler);
|
|
169
|
+
break;
|
|
170
|
+
}
|
|
171
|
+
if (!settled) await settle("Fetch.continueRequest", { requestId });
|
|
172
|
+
}
|
|
173
|
+
try {
|
|
174
|
+
await dispatch();
|
|
175
|
+
} catch (error) {
|
|
176
|
+
warnOnce(undefined, error);
|
|
177
|
+
if (!settled) await settle("Fetch.continueRequest", { requestId });
|
|
178
|
+
} finally {
|
|
179
|
+
activeRequests -= 1;
|
|
180
|
+
if (entries.length === 0 && activeRequests === 0) await syncFetch();
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
function onPaused(params, meta) {
|
|
185
|
+
void handlePaused(params, meta).catch(error => {
|
|
186
|
+
console.warn("[routes] requestPaused handling failed", error);
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
function syncFetch() {
|
|
191
|
+
const operation = lifecycle.then(async () => {
|
|
192
|
+
if (entries.length > 0) {
|
|
193
|
+
if (enabled) return;
|
|
194
|
+
sessionId = await page.resolveSessionId();
|
|
195
|
+
if (!unsubscribe) unsubscribe = page.cdp.on("Fetch.requestPaused", onPaused);
|
|
196
|
+
try {
|
|
197
|
+
await page.cdp.send("Fetch.enable", { patterns: [{ urlPattern: "*", requestStage: "Request" }] }, sessionId);
|
|
198
|
+
} catch (error) {
|
|
199
|
+
unsubscribe();
|
|
200
|
+
unsubscribe = null;
|
|
201
|
+
throw error;
|
|
202
|
+
}
|
|
203
|
+
enabled = true;
|
|
204
|
+
} else if (enabled) {
|
|
205
|
+
await page.cdp.send("Fetch.disable", {}, sessionId);
|
|
206
|
+
enabled = false;
|
|
207
|
+
unsubscribe();
|
|
208
|
+
unsubscribe = null;
|
|
209
|
+
settledIds.clear();
|
|
210
|
+
}
|
|
211
|
+
});
|
|
212
|
+
lifecycle = operation.catch(() => {});
|
|
213
|
+
return operation;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
async function route(match, handler, { times = Infinity, prepend = false } = {}) {
|
|
217
|
+
if (typeof handler !== "function") throw new TypeError("handler must be a function");
|
|
218
|
+
if (times !== Infinity && (!Number.isInteger(times) || times < 1)) throw new TypeError("times must be a positive integer");
|
|
219
|
+
const entry = { match, handler, remaining: times };
|
|
220
|
+
if (prepend) entries.unshift(entry);
|
|
221
|
+
else entries.push(entry);
|
|
222
|
+
try {
|
|
223
|
+
await syncFetch();
|
|
224
|
+
} catch (error) {
|
|
225
|
+
const index = entries.indexOf(entry);
|
|
226
|
+
if (index !== -1) entries.splice(index, 1);
|
|
227
|
+
throw error;
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
async function unroute(match, handler) {
|
|
232
|
+
if (match === undefined) entries.length = 0;
|
|
233
|
+
else {
|
|
234
|
+
for (let index = entries.length - 1; index >= 0; index -= 1) {
|
|
235
|
+
if (sameMatch(entries[index].match, match) && (handler === undefined || entries[index].handler === handler)) entries.splice(index, 1);
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
await syncFetch();
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
async function dispose() {
|
|
242
|
+
entries.length = 0;
|
|
243
|
+
await syncFetch();
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
const controller = {
|
|
247
|
+
route,
|
|
248
|
+
unroute,
|
|
249
|
+
dispose,
|
|
250
|
+
get enabled() {
|
|
251
|
+
return enabled;
|
|
252
|
+
},
|
|
253
|
+
};
|
|
254
|
+
controllers.set(page, controller);
|
|
255
|
+
return controller;
|
|
256
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
// Find a Chromium browser the user already runs with remote debugging turned on
|
|
2
|
+
// (chrome://inspect/#remote-debugging on Chrome 144+, or --remote-debugging-port)
|
|
3
|
+
// and attach to it over CDP, without a browser extension in the way.
|
|
4
|
+
// Idea adapted from vercel-labs/agent-browser (Apache-2.0), cli/src/native/cdp/chrome.rs
|
|
5
|
+
// `auto_connect_cdp`: read DevToolsActivePort and use its exact WebSocket path, which
|
|
6
|
+
// asks the user once instead of once per discovery probe. Written fresh for this file.
|
|
7
|
+
import { readFile } from "node:fs/promises";
|
|
8
|
+
import { connect as connectTcp } from "node:net";
|
|
9
|
+
import path from "node:path";
|
|
10
|
+
import { catalogBrowsers } from "./bsk/browsers.js";
|
|
11
|
+
import { connect } from "./connection.js";
|
|
12
|
+
|
|
13
|
+
const DEFAULT_PORT_CHECK_MS = 1_000;
|
|
14
|
+
|
|
15
|
+
export class RunningBrowserError extends Error {
|
|
16
|
+
constructor(code, message, candidates = []) {
|
|
17
|
+
super(message);
|
|
18
|
+
this.name = "RunningBrowserError";
|
|
19
|
+
this.code = code;
|
|
20
|
+
this.candidates = candidates;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export async function readDevToolsActivePort(userDataDir) {
|
|
25
|
+
const text = await readFile(path.join(userDataDir, "DevToolsActivePort"), "utf8").catch(() => null);
|
|
26
|
+
if (text === null) return null;
|
|
27
|
+
const [portLine, wsPath = ""] = text.split(/\r?\n/);
|
|
28
|
+
const port = Number(portLine.trim());
|
|
29
|
+
if (!Number.isInteger(port) || port <= 0 || port > 65_535) return null;
|
|
30
|
+
const pathPart = wsPath.trim();
|
|
31
|
+
return { port, wsPath: pathPart.startsWith("/devtools/browser") ? pathPart : null };
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// A bare TCP connect: it tells a live port from a stale file without opening a
|
|
35
|
+
// WebSocket, so it never raises Chrome's "Allow remote debugging?" prompt.
|
|
36
|
+
function portIsListening(port, timeoutMs) {
|
|
37
|
+
return new Promise((resolve) => {
|
|
38
|
+
const socket = connectTcp({ host: "127.0.0.1", port });
|
|
39
|
+
const done = (live) => {
|
|
40
|
+
socket.destroy();
|
|
41
|
+
resolve(live);
|
|
42
|
+
};
|
|
43
|
+
socket.setTimeout(timeoutMs, () => done(false));
|
|
44
|
+
socket.once("connect", () => done(true));
|
|
45
|
+
socket.once("error", () => done(false));
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function candidateDirs({ browser, userDataDirs, catalog }) {
|
|
50
|
+
if (userDataDirs) return userDataDirs.map((dir) => ({ id: path.basename(dir), label: dir, userDataDir: dir }));
|
|
51
|
+
const rows = catalog().filter((row) => row.hasProfile);
|
|
52
|
+
return browser ? rows.filter((row) => row.id === browser) : rows;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// Never deletes or edits anything in the profile: a stale DevToolsActivePort is skipped,
|
|
56
|
+
// because the user-data folder is the user's real browser profile.
|
|
57
|
+
export async function findRunningBrowser({ browser, userDataDirs, portCheckMs = DEFAULT_PORT_CHECK_MS, catalog = catalogBrowsers } = {}) {
|
|
58
|
+
const live = [];
|
|
59
|
+
for (const candidate of candidateDirs({ browser, userDataDirs, catalog })) {
|
|
60
|
+
const active = await readDevToolsActivePort(candidate.userDataDir);
|
|
61
|
+
if (!active || !active.wsPath) continue;
|
|
62
|
+
if (!(await portIsListening(active.port, portCheckMs))) continue;
|
|
63
|
+
live.push({ ...candidate, port: active.port, wsUrl: `ws://127.0.0.1:${active.port}${active.wsPath}` });
|
|
64
|
+
}
|
|
65
|
+
if (live.length === 1) return live[0];
|
|
66
|
+
if (live.length === 0) {
|
|
67
|
+
throw new RunningBrowserError(
|
|
68
|
+
"not_found",
|
|
69
|
+
"No running browser has remote debugging on. Ask the user to open chrome://inspect/#remote-debugging and allow it, then retry.",
|
|
70
|
+
);
|
|
71
|
+
}
|
|
72
|
+
throw new RunningBrowserError(
|
|
73
|
+
"choose_browser",
|
|
74
|
+
`More than one browser has remote debugging on (${live.map((row) => row.id).join(", ")}); pass { browser: "<id>" }.`,
|
|
75
|
+
live,
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// Chrome holds the WebSocket handshake while it asks the user, so this call waits until
|
|
80
|
+
// the user allows or denies the prompt; one shared connection per browser keeps it to one prompt.
|
|
81
|
+
export async function connectRunningBrowser(options = {}) {
|
|
82
|
+
const found = await findRunningBrowser(options);
|
|
83
|
+
const browser = await connect(found.wsUrl, options.connect ?? {});
|
|
84
|
+
return { browser, found };
|
|
85
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Scroll-driven collection over a network snoop (see ./network-snoop.js).
|
|
3
|
+
*/
|
|
4
|
+
async function viewportOf(page) {
|
|
5
|
+
return page.viewportSize() ?? await page.evaluate(() => ({ width: innerWidth, height: innerHeight }));
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
async function scrollOnce(page, mode) {
|
|
9
|
+
if (mode === "end") {
|
|
10
|
+
await page.evaluate(() => scrollTo(0, document.scrollingElement.scrollHeight));
|
|
11
|
+
return;
|
|
12
|
+
}
|
|
13
|
+
const viewport = await viewportOf(page);
|
|
14
|
+
await page.mouse.move(Math.floor(viewport.width / 2), Math.floor(viewport.height / 2));
|
|
15
|
+
await page.mouse.wheel(0, Math.floor(viewport.height * 0.9));
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Scrolls a page while draining JSON responses out of a snoop.
|
|
20
|
+
*
|
|
21
|
+
* Yields every extracted item and returns { rounds, total, stoppedBecause } when it stops.
|
|
22
|
+
*/
|
|
23
|
+
export async function* collectWhileScrolling(page, snoop, options = {}) {
|
|
24
|
+
const {
|
|
25
|
+
minItems = Infinity,
|
|
26
|
+
maxScrolls = 10,
|
|
27
|
+
extract = json => (Array.isArray(json) ? json : []),
|
|
28
|
+
scroll = "wheel",
|
|
29
|
+
settleMs = 800,
|
|
30
|
+
} = options;
|
|
31
|
+
if (scroll !== "wheel" && scroll !== "end") throw new TypeError(`Unknown scroll mode: ${String(scroll)}`);
|
|
32
|
+
|
|
33
|
+
let rounds = 0;
|
|
34
|
+
let total = 0;
|
|
35
|
+
let lastHeight = null;
|
|
36
|
+
let unchanged = 0;
|
|
37
|
+
let stoppedBecause = "maxScrolls";
|
|
38
|
+
|
|
39
|
+
for (;;) {
|
|
40
|
+
for (const json of snoop.popJson()) {
|
|
41
|
+
for (const item of extract(json)) {
|
|
42
|
+
total += 1;
|
|
43
|
+
yield item;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
if (total >= minItems) {
|
|
47
|
+
stoppedBecause = "minItems";
|
|
48
|
+
break;
|
|
49
|
+
}
|
|
50
|
+
if (rounds >= maxScrolls) {
|
|
51
|
+
stoppedBecause = "maxScrolls";
|
|
52
|
+
break;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const height = await page.evaluate(() => document.scrollingElement.scrollHeight);
|
|
56
|
+
if (lastHeight !== null && height === lastHeight) {
|
|
57
|
+
unchanged += 1;
|
|
58
|
+
if (unchanged >= 2) {
|
|
59
|
+
stoppedBecause = "noGrowth";
|
|
60
|
+
break;
|
|
61
|
+
}
|
|
62
|
+
} else {
|
|
63
|
+
unchanged = 0;
|
|
64
|
+
}
|
|
65
|
+
lastHeight = height;
|
|
66
|
+
|
|
67
|
+
await scrollOnce(page, scroll);
|
|
68
|
+
rounds += 1;
|
|
69
|
+
await new Promise(resolve => setTimeout(resolve, settleMs));
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
return { rounds, total, stoppedBecause };
|
|
73
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
// Event emitter for the pipe transport: listeners run one after another, in
|
|
2
|
+
// registration order, and emit() resolves once the last one has settled.
|
|
3
|
+
export class SerialEmitter {
|
|
4
|
+
#listeners = new Map();
|
|
5
|
+
on(event, fn) {
|
|
6
|
+
const list = this.#listeners.get(event) ?? [];
|
|
7
|
+
list.push(fn);
|
|
8
|
+
this.#listeners.set(event, list);
|
|
9
|
+
return () => {
|
|
10
|
+
const cur = this.#listeners.get(event) ?? [];
|
|
11
|
+
this.#listeners.set(event, cur.filter(f => f !== fn));
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
async emit(event, data) {
|
|
15
|
+
for (const fn of this.#listeners.get(event) ?? []) await fn(data);
|
|
16
|
+
}
|
|
17
|
+
}
|