@quitecode/chromium-automaton 0.1.0-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +118 -0
- package/dist/assert/expect.d.ts +1 -0
- package/dist/assert/expect.js +7 -0
- package/dist/assert/expect.js.map +1 -0
- package/dist/chunk-ADOXZ36A.js +292 -0
- package/dist/chunk-ADOXZ36A.js.map +1 -0
- package/dist/cli.js +1492 -0
- package/dist/cli.js.map +1 -0
- package/dist/expect-CBZIoH1D.d.ts +276 -0
- package/dist/index.d.ts +45 -0
- package/dist/index.js +1470 -0
- package/dist/index.js.map +1 -0
- package/package.json +53 -0
package/dist/cli.js
ADDED
|
@@ -0,0 +1,1492 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
// src/browser/ChromiumManager.ts
|
|
4
|
+
import fs3 from "fs";
|
|
5
|
+
import path3 from "path";
|
|
6
|
+
import http from "http";
|
|
7
|
+
import { spawn as spawn2 } from "child_process";
|
|
8
|
+
|
|
9
|
+
// src/logging/Logger.ts
|
|
10
|
+
var LEVEL_ORDER = {
|
|
11
|
+
error: 0,
|
|
12
|
+
warn: 1,
|
|
13
|
+
info: 2,
|
|
14
|
+
debug: 3,
|
|
15
|
+
trace: 4
|
|
16
|
+
};
|
|
17
|
+
var REDACT_KEYS = ["password", "token", "secret", "authorization", "cookie"];
|
|
18
|
+
function redactValue(value) {
|
|
19
|
+
if (typeof value === "string") {
|
|
20
|
+
try {
|
|
21
|
+
const url = new URL(value);
|
|
22
|
+
if (url.search) {
|
|
23
|
+
url.search = "?redacted";
|
|
24
|
+
}
|
|
25
|
+
return url.toString();
|
|
26
|
+
} catch {
|
|
27
|
+
return value;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
if (value && typeof value === "object") {
|
|
31
|
+
return JSON.stringify(value, (key, val) => {
|
|
32
|
+
if (REDACT_KEYS.some((k) => key.toLowerCase().includes(k))) {
|
|
33
|
+
return "[redacted]";
|
|
34
|
+
}
|
|
35
|
+
return val;
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
return String(value);
|
|
39
|
+
}
|
|
40
|
+
var Logger = class {
|
|
41
|
+
level;
|
|
42
|
+
constructor(level = "info") {
|
|
43
|
+
this.level = level;
|
|
44
|
+
}
|
|
45
|
+
setLevel(level) {
|
|
46
|
+
this.level = level;
|
|
47
|
+
}
|
|
48
|
+
error(message, ...args) {
|
|
49
|
+
this.log("error", message, ...args);
|
|
50
|
+
}
|
|
51
|
+
warn(message, ...args) {
|
|
52
|
+
this.log("warn", message, ...args);
|
|
53
|
+
}
|
|
54
|
+
info(message, ...args) {
|
|
55
|
+
this.log("info", message, ...args);
|
|
56
|
+
}
|
|
57
|
+
debug(message, ...args) {
|
|
58
|
+
this.log("debug", message, ...args);
|
|
59
|
+
}
|
|
60
|
+
trace(message, ...args) {
|
|
61
|
+
this.log("trace", message, ...args);
|
|
62
|
+
}
|
|
63
|
+
log(level, message, ...args) {
|
|
64
|
+
if (LEVEL_ORDER[level] > LEVEL_ORDER[this.level]) {
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
const time = (/* @__PURE__ */ new Date()).toISOString();
|
|
68
|
+
const suffix = args.length ? " " + args.map(redactValue).join(" ") : "";
|
|
69
|
+
const line = `[${time}] [${level}] ${message}${suffix}`;
|
|
70
|
+
if (level === "error") {
|
|
71
|
+
console.error(line);
|
|
72
|
+
} else if (level === "warn") {
|
|
73
|
+
console.warn(line);
|
|
74
|
+
} else {
|
|
75
|
+
console.log(line);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
// src/core/Events.ts
|
|
81
|
+
import { EventEmitter } from "events";
|
|
82
|
+
var AutomationEvents = class {
|
|
83
|
+
emitter = new EventEmitter();
|
|
84
|
+
on(event, handler) {
|
|
85
|
+
this.emitter.on(event, handler);
|
|
86
|
+
}
|
|
87
|
+
off(event, handler) {
|
|
88
|
+
this.emitter.off(event, handler);
|
|
89
|
+
}
|
|
90
|
+
emit(event, payload) {
|
|
91
|
+
this.emitter.emit(event, payload);
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
// src/cdp/Connection.ts
|
|
96
|
+
import WebSocket from "ws";
|
|
97
|
+
import { EventEmitter as EventEmitter3 } from "events";
|
|
98
|
+
|
|
99
|
+
// src/cdp/Session.ts
|
|
100
|
+
import { EventEmitter as EventEmitter2 } from "events";
|
|
101
|
+
var Session = class {
|
|
102
|
+
connection;
|
|
103
|
+
sessionId;
|
|
104
|
+
emitter = new EventEmitter2();
|
|
105
|
+
constructor(connection, sessionId) {
|
|
106
|
+
this.connection = connection;
|
|
107
|
+
this.sessionId = sessionId;
|
|
108
|
+
}
|
|
109
|
+
on(event, handler) {
|
|
110
|
+
this.emitter.on(event, handler);
|
|
111
|
+
}
|
|
112
|
+
once(event, handler) {
|
|
113
|
+
this.emitter.once(event, handler);
|
|
114
|
+
}
|
|
115
|
+
async send(method, params = {}) {
|
|
116
|
+
return this.connection.send(method, params, this.sessionId);
|
|
117
|
+
}
|
|
118
|
+
dispatch(method, params) {
|
|
119
|
+
this.emitter.emit(method, params);
|
|
120
|
+
}
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
// src/cdp/Connection.ts
|
|
124
|
+
var Connection = class {
|
|
125
|
+
ws;
|
|
126
|
+
id = 0;
|
|
127
|
+
callbacks = /* @__PURE__ */ new Map();
|
|
128
|
+
sessions = /* @__PURE__ */ new Map();
|
|
129
|
+
emitter = new EventEmitter3();
|
|
130
|
+
logger;
|
|
131
|
+
constructor(url, logger) {
|
|
132
|
+
this.logger = logger;
|
|
133
|
+
this.ws = new WebSocket(url);
|
|
134
|
+
this.ws.on("message", (data) => this.onMessage(data.toString()));
|
|
135
|
+
this.ws.on("error", (err) => this.onError(err));
|
|
136
|
+
}
|
|
137
|
+
async waitForOpen() {
|
|
138
|
+
if (this.ws.readyState === WebSocket.OPEN) {
|
|
139
|
+
return;
|
|
140
|
+
}
|
|
141
|
+
await new Promise((resolve, reject) => {
|
|
142
|
+
this.ws.once("open", () => resolve());
|
|
143
|
+
this.ws.once("error", (err) => reject(err));
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
createSession(sessionId) {
|
|
147
|
+
const session = new Session(this, sessionId);
|
|
148
|
+
this.sessions.set(sessionId, session);
|
|
149
|
+
return session;
|
|
150
|
+
}
|
|
151
|
+
removeSession(sessionId) {
|
|
152
|
+
this.sessions.delete(sessionId);
|
|
153
|
+
}
|
|
154
|
+
on(event, handler) {
|
|
155
|
+
this.emitter.on(event, handler);
|
|
156
|
+
}
|
|
157
|
+
async send(method, params = {}, sessionId) {
|
|
158
|
+
await this.waitForOpen();
|
|
159
|
+
const id = ++this.id;
|
|
160
|
+
const payload = sessionId ? { id, method, params, sessionId } : { id, method, params };
|
|
161
|
+
const start = Date.now();
|
|
162
|
+
const promise = new Promise((resolve, reject) => {
|
|
163
|
+
this.callbacks.set(id, { resolve, reject, method, start });
|
|
164
|
+
});
|
|
165
|
+
if (this.logger) {
|
|
166
|
+
this.logger.trace("CDP send", method);
|
|
167
|
+
}
|
|
168
|
+
this.ws.send(JSON.stringify(payload));
|
|
169
|
+
return promise;
|
|
170
|
+
}
|
|
171
|
+
async close() {
|
|
172
|
+
if (this.ws.readyState === WebSocket.CLOSED) {
|
|
173
|
+
return;
|
|
174
|
+
}
|
|
175
|
+
await new Promise((resolve) => {
|
|
176
|
+
this.ws.once("close", () => resolve());
|
|
177
|
+
this.ws.close();
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
onError(err) {
|
|
181
|
+
this.logger.error("CDP socket error", err);
|
|
182
|
+
}
|
|
183
|
+
onMessage(message) {
|
|
184
|
+
const parsed = JSON.parse(message);
|
|
185
|
+
if (typeof parsed.id === "number") {
|
|
186
|
+
const callback = this.callbacks.get(parsed.id);
|
|
187
|
+
if (!callback) {
|
|
188
|
+
return;
|
|
189
|
+
}
|
|
190
|
+
this.callbacks.delete(parsed.id);
|
|
191
|
+
const duration = Date.now() - callback.start;
|
|
192
|
+
this.logger.debug("CDP recv", callback.method, `${duration}ms`);
|
|
193
|
+
if (parsed.error) {
|
|
194
|
+
callback.reject(new Error(parsed.error.message));
|
|
195
|
+
} else {
|
|
196
|
+
callback.resolve(parsed.result);
|
|
197
|
+
}
|
|
198
|
+
return;
|
|
199
|
+
}
|
|
200
|
+
if (parsed.sessionId) {
|
|
201
|
+
const session = this.sessions.get(parsed.sessionId);
|
|
202
|
+
if (session) {
|
|
203
|
+
session.dispatch(parsed.method, parsed.params);
|
|
204
|
+
}
|
|
205
|
+
return;
|
|
206
|
+
}
|
|
207
|
+
if (parsed.method) {
|
|
208
|
+
this.emitter.emit(parsed.method, parsed.params);
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
};
|
|
212
|
+
|
|
213
|
+
// src/core/Page.ts
|
|
214
|
+
import fs from "fs";
|
|
215
|
+
import path from "path";
|
|
216
|
+
|
|
217
|
+
// src/core/Waiter.ts
|
|
218
|
+
async function waitFor(predicate, options = {}) {
|
|
219
|
+
const timeoutMs = options.timeoutMs ?? 3e4;
|
|
220
|
+
const intervalMs = options.intervalMs ?? 100;
|
|
221
|
+
const start = Date.now();
|
|
222
|
+
let lastError;
|
|
223
|
+
while (Date.now() - start < timeoutMs) {
|
|
224
|
+
try {
|
|
225
|
+
const result = await predicate();
|
|
226
|
+
if (result) {
|
|
227
|
+
return result;
|
|
228
|
+
}
|
|
229
|
+
} catch (err) {
|
|
230
|
+
lastError = err;
|
|
231
|
+
}
|
|
232
|
+
await new Promise((resolve) => setTimeout(resolve, intervalMs));
|
|
233
|
+
}
|
|
234
|
+
const description = options.description ? ` (${options.description})` : "";
|
|
235
|
+
const error = new Error(`Timeout after ${timeoutMs}ms${description}`);
|
|
236
|
+
error.cause = lastError;
|
|
237
|
+
throw error;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
// src/core/Selectors.ts
|
|
241
|
+
function isXPathSelector(input) {
|
|
242
|
+
if (input.startsWith("/")) return true;
|
|
243
|
+
if (input.startsWith("./")) return true;
|
|
244
|
+
if (input.startsWith(".//")) return true;
|
|
245
|
+
if (input.startsWith("..")) return true;
|
|
246
|
+
if (input.startsWith("(")) {
|
|
247
|
+
const trimmed = input.trimStart();
|
|
248
|
+
if (trimmed.startsWith("(")) {
|
|
249
|
+
const inner = trimmed.slice(1).trimStart();
|
|
250
|
+
return inner.startsWith("/") || inner.startsWith(".");
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
return false;
|
|
254
|
+
}
|
|
255
|
+
function parseSelector(input) {
|
|
256
|
+
const value = input.trim();
|
|
257
|
+
return {
|
|
258
|
+
type: isXPathSelector(value) ? "xpath" : "css",
|
|
259
|
+
value
|
|
260
|
+
};
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
// src/core/ShadowDom.ts
|
|
264
|
+
function querySelectorDeep(root, selector) {
|
|
265
|
+
const elements = Array.from(root.querySelectorAll("*"));
|
|
266
|
+
for (const el of elements) {
|
|
267
|
+
if (el.matches(selector)) {
|
|
268
|
+
return el;
|
|
269
|
+
}
|
|
270
|
+
const shadow = el.shadowRoot;
|
|
271
|
+
if (shadow) {
|
|
272
|
+
const found = querySelectorDeep(shadow, selector);
|
|
273
|
+
if (found) {
|
|
274
|
+
return found;
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
return null;
|
|
279
|
+
}
|
|
280
|
+
function querySelectorAllDeep(root, selector) {
|
|
281
|
+
const results = [];
|
|
282
|
+
const elements = Array.from(root.querySelectorAll("*"));
|
|
283
|
+
for (const el of elements) {
|
|
284
|
+
if (el.matches(selector)) {
|
|
285
|
+
results.push(el);
|
|
286
|
+
}
|
|
287
|
+
const shadow = el.shadowRoot;
|
|
288
|
+
if (shadow) {
|
|
289
|
+
results.push(...querySelectorAllDeep(shadow, selector));
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
return results;
|
|
293
|
+
}
|
|
294
|
+
function serializeShadowDomHelpers() {
|
|
295
|
+
return {
|
|
296
|
+
querySelectorDeep: querySelectorDeep.toString(),
|
|
297
|
+
querySelectorAllDeep: querySelectorAllDeep.toString()
|
|
298
|
+
};
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
// src/core/Locator.ts
|
|
302
|
+
var Locator = class {
|
|
303
|
+
frame;
|
|
304
|
+
selector;
|
|
305
|
+
options;
|
|
306
|
+
constructor(frame, selector, options = {}) {
|
|
307
|
+
this.frame = frame;
|
|
308
|
+
this.selector = selector;
|
|
309
|
+
this.options = options;
|
|
310
|
+
}
|
|
311
|
+
async click(options = {}) {
|
|
312
|
+
return this.frame.click(this.selector, { ...this.options, ...options });
|
|
313
|
+
}
|
|
314
|
+
async dblclick(options = {}) {
|
|
315
|
+
return this.frame.dblclick(this.selector, { ...this.options, ...options });
|
|
316
|
+
}
|
|
317
|
+
async type(text, options = {}) {
|
|
318
|
+
return this.frame.type(this.selector, text, { ...this.options, ...options });
|
|
319
|
+
}
|
|
320
|
+
async exists() {
|
|
321
|
+
return this.frame.exists(this.selector, this.options);
|
|
322
|
+
}
|
|
323
|
+
async text() {
|
|
324
|
+
return this.frame.text(this.selector, this.options);
|
|
325
|
+
}
|
|
326
|
+
};
|
|
327
|
+
|
|
328
|
+
// src/core/Frame.ts
|
|
329
|
+
var Frame = class {
|
|
330
|
+
id;
|
|
331
|
+
name;
|
|
332
|
+
url;
|
|
333
|
+
parentId;
|
|
334
|
+
session;
|
|
335
|
+
logger;
|
|
336
|
+
events;
|
|
337
|
+
contextId;
|
|
338
|
+
defaultTimeout = 3e4;
|
|
339
|
+
constructor(id, session, logger, events) {
|
|
340
|
+
this.id = id;
|
|
341
|
+
this.session = session;
|
|
342
|
+
this.logger = logger;
|
|
343
|
+
this.events = events;
|
|
344
|
+
}
|
|
345
|
+
setExecutionContext(contextId) {
|
|
346
|
+
this.contextId = contextId;
|
|
347
|
+
}
|
|
348
|
+
getExecutionContext() {
|
|
349
|
+
return this.contextId;
|
|
350
|
+
}
|
|
351
|
+
setMeta(meta) {
|
|
352
|
+
this.name = meta.name;
|
|
353
|
+
this.url = meta.url;
|
|
354
|
+
this.parentId = meta.parentId;
|
|
355
|
+
}
|
|
356
|
+
async evaluate(fnOrString, ...args) {
|
|
357
|
+
return this.evaluateInContext(fnOrString, args);
|
|
358
|
+
}
|
|
359
|
+
async query(selector, options = {}) {
|
|
360
|
+
return this.querySelectorInternal(selector, options, false);
|
|
361
|
+
}
|
|
362
|
+
async queryAll(selector, options = {}) {
|
|
363
|
+
return this.querySelectorAllInternal(selector, options, false);
|
|
364
|
+
}
|
|
365
|
+
async queryXPath(selector, options = {}) {
|
|
366
|
+
return this.querySelectorInternal(selector, options, true);
|
|
367
|
+
}
|
|
368
|
+
async queryAllXPath(selector, options = {}) {
|
|
369
|
+
return this.querySelectorAllInternal(selector, options, true);
|
|
370
|
+
}
|
|
371
|
+
locator(selector, options = {}) {
|
|
372
|
+
return new Locator(this, selector, options);
|
|
373
|
+
}
|
|
374
|
+
async click(selector, options = {}) {
|
|
375
|
+
await this.performClick(selector, options, false);
|
|
376
|
+
}
|
|
377
|
+
async dblclick(selector, options = {}) {
|
|
378
|
+
await this.performClick(selector, options, true);
|
|
379
|
+
}
|
|
380
|
+
async type(selector, text, options = {}) {
|
|
381
|
+
const start = Date.now();
|
|
382
|
+
this.events.emit("action:start", { name: "type", selector, frameId: this.id });
|
|
383
|
+
await waitFor(async () => {
|
|
384
|
+
const box = await this.resolveElementBox(selector, options);
|
|
385
|
+
if (!box || !box.visible) {
|
|
386
|
+
return false;
|
|
387
|
+
}
|
|
388
|
+
return true;
|
|
389
|
+
}, { timeoutMs: options.timeoutMs ?? this.defaultTimeout, description: `type ${selector}` });
|
|
390
|
+
const helpers = serializeShadowDomHelpers();
|
|
391
|
+
const focusExpression = `(function() {
|
|
392
|
+
const querySelectorDeep = ${helpers.querySelectorDeep};
|
|
393
|
+
const root = document;
|
|
394
|
+
const selector = ${JSON.stringify(selector)};
|
|
395
|
+
const el = ${options.pierceShadowDom ? "querySelectorDeep(root, selector)" : "root.querySelector(selector)"};
|
|
396
|
+
if (!el) {
|
|
397
|
+
return;
|
|
398
|
+
}
|
|
399
|
+
el.focus();
|
|
400
|
+
})()`;
|
|
401
|
+
const focusParams = {
|
|
402
|
+
expression: focusExpression,
|
|
403
|
+
returnByValue: true
|
|
404
|
+
};
|
|
405
|
+
if (this.contextId) {
|
|
406
|
+
focusParams.contextId = this.contextId;
|
|
407
|
+
}
|
|
408
|
+
await this.session.send("Runtime.evaluate", focusParams);
|
|
409
|
+
await this.session.send("Input.insertText", { text });
|
|
410
|
+
const duration = Date.now() - start;
|
|
411
|
+
this.events.emit("action:end", { name: "type", selector, frameId: this.id, durationMs: duration });
|
|
412
|
+
this.logger.debug("Type", selector, `${duration}ms`);
|
|
413
|
+
}
|
|
414
|
+
async exists(selector, options = {}) {
|
|
415
|
+
const handle = await this.query(selector, options);
|
|
416
|
+
if (handle) {
|
|
417
|
+
await this.releaseObject(handle.objectId);
|
|
418
|
+
return true;
|
|
419
|
+
}
|
|
420
|
+
return false;
|
|
421
|
+
}
|
|
422
|
+
async isVisible(selector, options = {}) {
|
|
423
|
+
const box = await this.resolveElementBox(selector, options);
|
|
424
|
+
return Boolean(box && box.visible);
|
|
425
|
+
}
|
|
426
|
+
async text(selector, options = {}) {
|
|
427
|
+
const helpers = serializeShadowDomHelpers();
|
|
428
|
+
const expression = `(function() {
|
|
429
|
+
const querySelectorDeep = ${helpers.querySelectorDeep};
|
|
430
|
+
const root = document;
|
|
431
|
+
const selector = ${JSON.stringify(selector)};
|
|
432
|
+
const el = ${options.pierceShadowDom ? "querySelectorDeep(root, selector)" : "root.querySelector(selector)"};
|
|
433
|
+
return el ? el.textContent || "" : null;
|
|
434
|
+
})()`;
|
|
435
|
+
const params = {
|
|
436
|
+
expression,
|
|
437
|
+
returnByValue: true
|
|
438
|
+
};
|
|
439
|
+
if (this.contextId) {
|
|
440
|
+
params.contextId = this.contextId;
|
|
441
|
+
}
|
|
442
|
+
const result = await this.session.send("Runtime.evaluate", params);
|
|
443
|
+
return result.result.value ?? null;
|
|
444
|
+
}
|
|
445
|
+
async attribute(selector, name, options = {}) {
|
|
446
|
+
return this.evalOnSelector(selector, options, false, `
|
|
447
|
+
if (!el || !(el instanceof Element)) {
|
|
448
|
+
return null;
|
|
449
|
+
}
|
|
450
|
+
return el.getAttribute(${JSON.stringify(name)});
|
|
451
|
+
`);
|
|
452
|
+
}
|
|
453
|
+
async value(selector, options = {}) {
|
|
454
|
+
return this.evalOnSelector(selector, options, false, `
|
|
455
|
+
if (!el) {
|
|
456
|
+
return null;
|
|
457
|
+
}
|
|
458
|
+
if ("value" in el) {
|
|
459
|
+
return el.value ?? "";
|
|
460
|
+
}
|
|
461
|
+
return el.getAttribute("value");
|
|
462
|
+
`);
|
|
463
|
+
}
|
|
464
|
+
async isEnabled(selector, options = {}) {
|
|
465
|
+
return this.evalOnSelector(selector, options, false, `
|
|
466
|
+
if (!el) {
|
|
467
|
+
return null;
|
|
468
|
+
}
|
|
469
|
+
const disabled = Boolean(el.disabled) || el.hasAttribute("disabled");
|
|
470
|
+
const ariaDisabled = el.getAttribute && el.getAttribute("aria-disabled") === "true";
|
|
471
|
+
return !(disabled || ariaDisabled);
|
|
472
|
+
`);
|
|
473
|
+
}
|
|
474
|
+
async isChecked(selector, options = {}) {
|
|
475
|
+
return this.evalOnSelector(selector, options, false, `
|
|
476
|
+
if (!el) {
|
|
477
|
+
return null;
|
|
478
|
+
}
|
|
479
|
+
const aria = el.getAttribute && el.getAttribute("aria-checked");
|
|
480
|
+
if (aria === "true") {
|
|
481
|
+
return true;
|
|
482
|
+
}
|
|
483
|
+
if (aria === "false") {
|
|
484
|
+
return false;
|
|
485
|
+
}
|
|
486
|
+
if ("checked" in el) {
|
|
487
|
+
return Boolean(el.checked);
|
|
488
|
+
}
|
|
489
|
+
return null;
|
|
490
|
+
`);
|
|
491
|
+
}
|
|
492
|
+
async count(selector, options = {}) {
|
|
493
|
+
const parsed = parseSelector(selector);
|
|
494
|
+
const pierce = Boolean(options.pierceShadowDom);
|
|
495
|
+
const helpers = serializeShadowDomHelpers();
|
|
496
|
+
const expression = parsed.type === "xpath" ? `(function() {
|
|
497
|
+
const result = document.evaluate(${JSON.stringify(parsed.value)}, document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
|
|
498
|
+
return result.snapshotLength;
|
|
499
|
+
})()` : `(function() {
|
|
500
|
+
const querySelectorAllDeep = ${helpers.querySelectorAllDeep};
|
|
501
|
+
const root = document;
|
|
502
|
+
const selector = ${JSON.stringify(parsed.value)};
|
|
503
|
+
const nodes = ${pierce ? "querySelectorAllDeep(root, selector)" : "root.querySelectorAll(selector)"};
|
|
504
|
+
return nodes.length;
|
|
505
|
+
})()`;
|
|
506
|
+
const params = {
|
|
507
|
+
expression,
|
|
508
|
+
returnByValue: true
|
|
509
|
+
};
|
|
510
|
+
if (this.contextId) {
|
|
511
|
+
params.contextId = this.contextId;
|
|
512
|
+
}
|
|
513
|
+
const result = await this.session.send("Runtime.evaluate", params);
|
|
514
|
+
return result.result.value ?? 0;
|
|
515
|
+
}
|
|
516
|
+
async classes(selector, options = {}) {
|
|
517
|
+
return this.evalOnSelector(selector, options, false, `
|
|
518
|
+
if (!el) {
|
|
519
|
+
return null;
|
|
520
|
+
}
|
|
521
|
+
if (!el.classList) {
|
|
522
|
+
return [];
|
|
523
|
+
}
|
|
524
|
+
return Array.from(el.classList);
|
|
525
|
+
`);
|
|
526
|
+
}
|
|
527
|
+
async css(selector, property, options = {}) {
|
|
528
|
+
return this.evalOnSelector(selector, options, false, `
|
|
529
|
+
if (!el) {
|
|
530
|
+
return null;
|
|
531
|
+
}
|
|
532
|
+
const style = window.getComputedStyle(el);
|
|
533
|
+
return style.getPropertyValue(${JSON.stringify(property)}) || "";
|
|
534
|
+
`);
|
|
535
|
+
}
|
|
536
|
+
async hasFocus(selector, options = {}) {
|
|
537
|
+
return this.evalOnSelector(selector, options, false, `
|
|
538
|
+
if (!el) {
|
|
539
|
+
return null;
|
|
540
|
+
}
|
|
541
|
+
return document.activeElement === el;
|
|
542
|
+
`);
|
|
543
|
+
}
|
|
544
|
+
async isInViewport(selector, options = {}, fully = false) {
|
|
545
|
+
return this.evalOnSelector(selector, options, false, `
|
|
546
|
+
if (!el) {
|
|
547
|
+
return null;
|
|
548
|
+
}
|
|
549
|
+
const rect = el.getBoundingClientRect();
|
|
550
|
+
const viewWidth = window.innerWidth || document.documentElement.clientWidth;
|
|
551
|
+
const viewHeight = window.innerHeight || document.documentElement.clientHeight;
|
|
552
|
+
if (${fully ? "true" : "false"}) {
|
|
553
|
+
return rect.top >= 0 && rect.left >= 0 && rect.bottom <= viewHeight && rect.right <= viewWidth;
|
|
554
|
+
}
|
|
555
|
+
return rect.bottom > 0 && rect.right > 0 && rect.top < viewHeight && rect.left < viewWidth;
|
|
556
|
+
`);
|
|
557
|
+
}
|
|
558
|
+
async isEditable(selector, options = {}) {
|
|
559
|
+
return this.evalOnSelector(selector, options, false, `
|
|
560
|
+
if (!el) {
|
|
561
|
+
return null;
|
|
562
|
+
}
|
|
563
|
+
const disabled = Boolean(el.disabled) || el.hasAttribute("disabled");
|
|
564
|
+
const readOnly = Boolean(el.readOnly) || el.hasAttribute("readonly");
|
|
565
|
+
const ariaDisabled = el.getAttribute && el.getAttribute("aria-disabled") === "true";
|
|
566
|
+
return !(disabled || readOnly || ariaDisabled);
|
|
567
|
+
`);
|
|
568
|
+
}
|
|
569
|
+
async performClick(selector, options, isDouble) {
|
|
570
|
+
const start = Date.now();
|
|
571
|
+
const actionName = isDouble ? "dblclick" : "click";
|
|
572
|
+
this.events.emit("action:start", { name: actionName, selector, frameId: this.id });
|
|
573
|
+
const box = await waitFor(async () => {
|
|
574
|
+
const result = await this.resolveElementBox(selector, options);
|
|
575
|
+
if (!result || !result.visible) {
|
|
576
|
+
return null;
|
|
577
|
+
}
|
|
578
|
+
return result;
|
|
579
|
+
}, { timeoutMs: options.timeoutMs ?? this.defaultTimeout, description: `${actionName} ${selector}` });
|
|
580
|
+
const centerX = box.x + box.width / 2;
|
|
581
|
+
const centerY = box.y + box.height / 2;
|
|
582
|
+
await this.session.send("Input.dispatchMouseEvent", { type: "mouseMoved", x: centerX, y: centerY });
|
|
583
|
+
await this.session.send("Input.dispatchMouseEvent", { type: "mousePressed", x: centerX, y: centerY, button: "left", clickCount: 1, buttons: 1 });
|
|
584
|
+
await this.session.send("Input.dispatchMouseEvent", { type: "mouseReleased", x: centerX, y: centerY, button: "left", clickCount: 1, buttons: 0 });
|
|
585
|
+
if (isDouble) {
|
|
586
|
+
await this.session.send("Input.dispatchMouseEvent", { type: "mouseMoved", x: centerX, y: centerY });
|
|
587
|
+
await this.session.send("Input.dispatchMouseEvent", { type: "mousePressed", x: centerX, y: centerY, button: "left", clickCount: 2, buttons: 1 });
|
|
588
|
+
await this.session.send("Input.dispatchMouseEvent", { type: "mouseReleased", x: centerX, y: centerY, button: "left", clickCount: 2, buttons: 0 });
|
|
589
|
+
}
|
|
590
|
+
const duration = Date.now() - start;
|
|
591
|
+
this.events.emit("action:end", { name: actionName, selector, frameId: this.id, durationMs: duration });
|
|
592
|
+
this.logger.debug("Click", selector, `${duration}ms`);
|
|
593
|
+
}
|
|
594
|
+
async resolveElementBox(selector, options) {
|
|
595
|
+
const parsed = parseSelector(selector);
|
|
596
|
+
const pierce = Boolean(options.pierceShadowDom);
|
|
597
|
+
const helpers = serializeShadowDomHelpers();
|
|
598
|
+
const expression = parsed.type === "xpath" ? `(function() {
|
|
599
|
+
const result = document.evaluate(${JSON.stringify(parsed.value)}, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
|
|
600
|
+
if (!result || !(result instanceof Element)) {
|
|
601
|
+
return null;
|
|
602
|
+
}
|
|
603
|
+
result.scrollIntoView({ block: 'center', inline: 'center' });
|
|
604
|
+
const rect = result.getBoundingClientRect();
|
|
605
|
+
const style = window.getComputedStyle(result);
|
|
606
|
+
return { x: rect.x, y: rect.y, width: rect.width, height: rect.height, visible: rect.width > 0 && rect.height > 0 && style.visibility !== 'hidden' && style.display !== 'none' && Number(style.opacity || '1') > 0 };
|
|
607
|
+
})()` : `(function() {
|
|
608
|
+
const querySelectorDeep = ${helpers.querySelectorDeep};
|
|
609
|
+
const root = document;
|
|
610
|
+
const selector = ${JSON.stringify(parsed.value)};
|
|
611
|
+
const el = ${pierce ? "querySelectorDeep(root, selector)" : "root.querySelector(selector)"};
|
|
612
|
+
if (!el) {
|
|
613
|
+
return null;
|
|
614
|
+
}
|
|
615
|
+
el.scrollIntoView({ block: 'center', inline: 'center' });
|
|
616
|
+
const rect = el.getBoundingClientRect();
|
|
617
|
+
const style = window.getComputedStyle(el);
|
|
618
|
+
return { x: rect.x, y: rect.y, width: rect.width, height: rect.height, visible: rect.width > 0 && rect.height > 0 && style.visibility !== 'hidden' && style.display !== 'none' && Number(style.opacity || '1') > 0 };
|
|
619
|
+
})()`;
|
|
620
|
+
const boxParams = {
|
|
621
|
+
expression,
|
|
622
|
+
returnByValue: true
|
|
623
|
+
};
|
|
624
|
+
if (this.contextId) {
|
|
625
|
+
boxParams.contextId = this.contextId;
|
|
626
|
+
}
|
|
627
|
+
const result = await this.session.send("Runtime.evaluate", boxParams);
|
|
628
|
+
return result?.result?.value ?? null;
|
|
629
|
+
}
|
|
630
|
+
async querySelectorInternal(selector, options, forceXPath) {
|
|
631
|
+
const parsed = forceXPath ? { type: "xpath", value: selector.trim() } : parseSelector(selector);
|
|
632
|
+
const pierce = Boolean(options.pierceShadowDom);
|
|
633
|
+
const helpers = serializeShadowDomHelpers();
|
|
634
|
+
const expression = parsed.type === "xpath" ? `(function() {
|
|
635
|
+
const result = document.evaluate(${JSON.stringify(parsed.value)}, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
|
|
636
|
+
return result || null;
|
|
637
|
+
})()` : `(function() {
|
|
638
|
+
const querySelectorDeep = ${helpers.querySelectorDeep};
|
|
639
|
+
const root = document;
|
|
640
|
+
const selector = ${JSON.stringify(parsed.value)};
|
|
641
|
+
return ${pierce ? "querySelectorDeep(root, selector)" : "root.querySelector(selector)"};
|
|
642
|
+
})()`;
|
|
643
|
+
const queryParams = {
|
|
644
|
+
expression,
|
|
645
|
+
returnByValue: false
|
|
646
|
+
};
|
|
647
|
+
if (this.contextId) {
|
|
648
|
+
queryParams.contextId = this.contextId;
|
|
649
|
+
}
|
|
650
|
+
const response = await this.session.send("Runtime.evaluate", queryParams);
|
|
651
|
+
if (response.result?.subtype === "null" || !response.result?.objectId) {
|
|
652
|
+
return null;
|
|
653
|
+
}
|
|
654
|
+
return { objectId: response.result.objectId, contextId: this.contextId ?? 0 };
|
|
655
|
+
}
|
|
656
|
+
async querySelectorAllInternal(selector, options, forceXPath) {
|
|
657
|
+
const parsed = forceXPath ? { type: "xpath", value: selector.trim() } : parseSelector(selector);
|
|
658
|
+
const pierce = Boolean(options.pierceShadowDom);
|
|
659
|
+
const helpers = serializeShadowDomHelpers();
|
|
660
|
+
const expression = parsed.type === "xpath" ? `(function() {
|
|
661
|
+
const result = document.evaluate(${JSON.stringify(parsed.value)}, document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
|
|
662
|
+
const nodes = [];
|
|
663
|
+
for (let i = 0; i < result.snapshotLength; i += 1) {
|
|
664
|
+
nodes.push(result.snapshotItem(i));
|
|
665
|
+
}
|
|
666
|
+
return nodes;
|
|
667
|
+
})()` : `(function() {
|
|
668
|
+
const querySelectorAllDeep = ${helpers.querySelectorAllDeep};
|
|
669
|
+
const root = document;
|
|
670
|
+
const selector = ${JSON.stringify(parsed.value)};
|
|
671
|
+
return ${pierce ? "querySelectorAllDeep(root, selector)" : "Array.from(root.querySelectorAll(selector))"};
|
|
672
|
+
})()`;
|
|
673
|
+
const listParams = {
|
|
674
|
+
expression,
|
|
675
|
+
returnByValue: false
|
|
676
|
+
};
|
|
677
|
+
if (this.contextId) {
|
|
678
|
+
listParams.contextId = this.contextId;
|
|
679
|
+
}
|
|
680
|
+
const response = await this.session.send("Runtime.evaluate", listParams);
|
|
681
|
+
if (!response.result?.objectId) {
|
|
682
|
+
return [];
|
|
683
|
+
}
|
|
684
|
+
const properties = await this.session.send("Runtime.getProperties", {
|
|
685
|
+
objectId: response.result.objectId,
|
|
686
|
+
ownProperties: true
|
|
687
|
+
});
|
|
688
|
+
const handles = [];
|
|
689
|
+
for (const prop of properties.result) {
|
|
690
|
+
if (prop.name && !/^\d+$/.test(prop.name)) {
|
|
691
|
+
continue;
|
|
692
|
+
}
|
|
693
|
+
const objectId = prop.value?.objectId;
|
|
694
|
+
if (objectId) {
|
|
695
|
+
handles.push({ objectId, contextId: this.contextId ?? 0 });
|
|
696
|
+
}
|
|
697
|
+
}
|
|
698
|
+
await this.releaseObject(response.result.objectId);
|
|
699
|
+
return handles;
|
|
700
|
+
}
|
|
701
|
+
async evaluateInContext(fnOrString, args) {
|
|
702
|
+
if (typeof fnOrString === "string") {
|
|
703
|
+
const params2 = {
|
|
704
|
+
expression: fnOrString,
|
|
705
|
+
returnByValue: true,
|
|
706
|
+
awaitPromise: true
|
|
707
|
+
};
|
|
708
|
+
if (this.contextId) {
|
|
709
|
+
params2.contextId = this.contextId;
|
|
710
|
+
}
|
|
711
|
+
const result2 = await this.session.send("Runtime.evaluate", params2);
|
|
712
|
+
return result2.result.value;
|
|
713
|
+
}
|
|
714
|
+
const serializedArgs = args.map((arg) => serializeArgument(arg)).join(", ");
|
|
715
|
+
const expression = `(${fnOrString.toString()})(${serializedArgs})`;
|
|
716
|
+
const params = {
|
|
717
|
+
expression,
|
|
718
|
+
returnByValue: true,
|
|
719
|
+
awaitPromise: true
|
|
720
|
+
};
|
|
721
|
+
if (this.contextId) {
|
|
722
|
+
params.contextId = this.contextId;
|
|
723
|
+
}
|
|
724
|
+
const result = await this.session.send("Runtime.evaluate", params);
|
|
725
|
+
return result.result.value;
|
|
726
|
+
}
|
|
727
|
+
async releaseObject(objectId) {
|
|
728
|
+
try {
|
|
729
|
+
await this.session.send("Runtime.releaseObject", { objectId });
|
|
730
|
+
} catch {
|
|
731
|
+
}
|
|
732
|
+
}
|
|
733
|
+
buildElementExpression(selector, options, forceXPath, body) {
|
|
734
|
+
const parsed = forceXPath ? { type: "xpath", value: selector.trim() } : parseSelector(selector);
|
|
735
|
+
const pierce = Boolean(options.pierceShadowDom);
|
|
736
|
+
const helpers = serializeShadowDomHelpers();
|
|
737
|
+
if (parsed.type === "xpath") {
|
|
738
|
+
return `(function() {
|
|
739
|
+
const el = document.evaluate(${JSON.stringify(parsed.value)}, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
|
|
740
|
+
${body}
|
|
741
|
+
})()`;
|
|
742
|
+
}
|
|
743
|
+
return `(function() {
|
|
744
|
+
const querySelectorDeep = ${helpers.querySelectorDeep};
|
|
745
|
+
const root = document;
|
|
746
|
+
const selector = ${JSON.stringify(parsed.value)};
|
|
747
|
+
const el = ${pierce ? "querySelectorDeep(root, selector)" : "root.querySelector(selector)"};
|
|
748
|
+
${body}
|
|
749
|
+
})()`;
|
|
750
|
+
}
|
|
751
|
+
async evalOnSelector(selector, options, forceXPath, body) {
|
|
752
|
+
const expression = this.buildElementExpression(selector, options, forceXPath, body);
|
|
753
|
+
const params = {
|
|
754
|
+
expression,
|
|
755
|
+
returnByValue: true
|
|
756
|
+
};
|
|
757
|
+
if (this.contextId) {
|
|
758
|
+
params.contextId = this.contextId;
|
|
759
|
+
}
|
|
760
|
+
const result = await this.session.send("Runtime.evaluate", params);
|
|
761
|
+
return result.result.value;
|
|
762
|
+
}
|
|
763
|
+
};
|
|
764
|
+
function serializeArgument(value) {
|
|
765
|
+
if (value === void 0) {
|
|
766
|
+
return "undefined";
|
|
767
|
+
}
|
|
768
|
+
return JSON.stringify(value);
|
|
769
|
+
}
|
|
770
|
+
|
|
771
|
+
// src/core/UrlGuard.ts
|
|
772
|
+
function ensureAllowedUrl(url, options = {}) {
|
|
773
|
+
let parsed;
|
|
774
|
+
try {
|
|
775
|
+
parsed = new URL(url);
|
|
776
|
+
} catch {
|
|
777
|
+
throw new Error(`Invalid URL: ${url}`);
|
|
778
|
+
}
|
|
779
|
+
if (parsed.protocol === "http:" || parsed.protocol === "https:") {
|
|
780
|
+
return;
|
|
781
|
+
}
|
|
782
|
+
if (parsed.protocol === "file:" && options.allowFileUrl) {
|
|
783
|
+
return;
|
|
784
|
+
}
|
|
785
|
+
throw new Error(`URL protocol not allowed: ${parsed.protocol}`);
|
|
786
|
+
}
|
|
787
|
+
|
|
788
|
+
// src/core/Page.ts
|
|
789
|
+
var Page = class {
|
|
790
|
+
session;
|
|
791
|
+
logger;
|
|
792
|
+
events;
|
|
793
|
+
framesById = /* @__PURE__ */ new Map();
|
|
794
|
+
mainFrameId;
|
|
795
|
+
lifecycleEvents = /* @__PURE__ */ new Map();
|
|
796
|
+
defaultTimeout = 3e4;
|
|
797
|
+
constructor(session, logger, events) {
|
|
798
|
+
this.session = session;
|
|
799
|
+
this.logger = logger;
|
|
800
|
+
this.events = events;
|
|
801
|
+
}
|
|
802
|
+
async initialize() {
|
|
803
|
+
await this.session.send("Page.enable");
|
|
804
|
+
await this.session.send("DOM.enable");
|
|
805
|
+
await this.session.send("Runtime.enable");
|
|
806
|
+
await this.session.send("Network.enable");
|
|
807
|
+
await this.session.send("Page.setLifecycleEventsEnabled", { enabled: true });
|
|
808
|
+
this.session.on("Page.frameAttached", (params) => this.onFrameAttached(params));
|
|
809
|
+
this.session.on("Page.frameNavigated", (params) => this.onFrameNavigated(params));
|
|
810
|
+
this.session.on("Page.frameDetached", (params) => this.onFrameDetached(params));
|
|
811
|
+
this.session.on("Runtime.executionContextCreated", (params) => this.onExecutionContextCreated(params));
|
|
812
|
+
this.session.on("Runtime.executionContextDestroyed", (params) => this.onExecutionContextDestroyed(params));
|
|
813
|
+
this.session.on("Runtime.executionContextsCleared", () => this.onExecutionContextsCleared());
|
|
814
|
+
this.session.on("Page.lifecycleEvent", (params) => this.onLifecycleEvent(params));
|
|
815
|
+
const tree = await this.session.send("Page.getFrameTree");
|
|
816
|
+
this.buildFrameTree(tree.frameTree);
|
|
817
|
+
}
|
|
818
|
+
frames() {
|
|
819
|
+
return Array.from(this.framesById.values());
|
|
820
|
+
}
|
|
821
|
+
mainFrame() {
|
|
822
|
+
if (!this.mainFrameId) {
|
|
823
|
+
throw new Error("Main frame not initialized");
|
|
824
|
+
}
|
|
825
|
+
const frame = this.framesById.get(this.mainFrameId);
|
|
826
|
+
if (!frame) {
|
|
827
|
+
throw new Error("Main frame missing");
|
|
828
|
+
}
|
|
829
|
+
return frame;
|
|
830
|
+
}
|
|
831
|
+
frame(options) {
|
|
832
|
+
for (const frame of this.framesById.values()) {
|
|
833
|
+
if (options.name && frame.name !== options.name) continue;
|
|
834
|
+
if (options.urlIncludes && !frame.url?.includes(options.urlIncludes)) continue;
|
|
835
|
+
if (options.predicate && !options.predicate(frame)) continue;
|
|
836
|
+
return frame;
|
|
837
|
+
}
|
|
838
|
+
return null;
|
|
839
|
+
}
|
|
840
|
+
locator(selector, options) {
|
|
841
|
+
return new Locator(this.mainFrame(), selector, options);
|
|
842
|
+
}
|
|
843
|
+
async goto(url, options = {}) {
|
|
844
|
+
ensureAllowedUrl(url, { allowFileUrl: options.allowFileUrl });
|
|
845
|
+
const waitUntil = options.waitUntil ?? "load";
|
|
846
|
+
const lifecycleName = waitUntil === "domcontentloaded" ? "DOMContentLoaded" : "load";
|
|
847
|
+
const timeoutMs = options.timeoutMs ?? this.defaultTimeout;
|
|
848
|
+
this.events.emit("action:start", { name: "goto", selector: url, frameId: this.mainFrameId });
|
|
849
|
+
const start = Date.now();
|
|
850
|
+
await this.session.send("Page.navigate", { url });
|
|
851
|
+
await this.waitForLifecycle(this.mainFrameId, lifecycleName, timeoutMs);
|
|
852
|
+
const duration = Date.now() - start;
|
|
853
|
+
this.events.emit("action:end", { name: "goto", selector: url, frameId: this.mainFrameId, durationMs: duration });
|
|
854
|
+
this.logger.info("Goto", url, `${duration}ms`);
|
|
855
|
+
}
|
|
856
|
+
async query(selector, options) {
|
|
857
|
+
return this.mainFrame().query(selector, options);
|
|
858
|
+
}
|
|
859
|
+
async queryAll(selector, options) {
|
|
860
|
+
return this.mainFrame().queryAll(selector, options);
|
|
861
|
+
}
|
|
862
|
+
async queryXPath(selector, options) {
|
|
863
|
+
return this.mainFrame().queryXPath(selector, options);
|
|
864
|
+
}
|
|
865
|
+
async queryAllXPath(selector, options) {
|
|
866
|
+
return this.mainFrame().queryAllXPath(selector, options);
|
|
867
|
+
}
|
|
868
|
+
async click(selector, options) {
|
|
869
|
+
return this.mainFrame().click(selector, options);
|
|
870
|
+
}
|
|
871
|
+
async dblclick(selector, options) {
|
|
872
|
+
return this.mainFrame().dblclick(selector, options);
|
|
873
|
+
}
|
|
874
|
+
async type(selector, text, options) {
|
|
875
|
+
return this.mainFrame().type(selector, text, options);
|
|
876
|
+
}
|
|
877
|
+
async evaluate(fnOrString, ...args) {
|
|
878
|
+
return this.mainFrame().evaluate(fnOrString, ...args);
|
|
879
|
+
}
|
|
880
|
+
async screenshot(options = {}) {
|
|
881
|
+
const start = Date.now();
|
|
882
|
+
this.events.emit("action:start", { name: "screenshot", frameId: this.mainFrameId });
|
|
883
|
+
const result = await this.session.send("Page.captureScreenshot", {
|
|
884
|
+
format: options.format ?? "png",
|
|
885
|
+
quality: options.quality,
|
|
886
|
+
fromSurface: true
|
|
887
|
+
});
|
|
888
|
+
const buffer = Buffer.from(result.data, "base64");
|
|
889
|
+
if (options.path) {
|
|
890
|
+
const resolved = path.resolve(options.path);
|
|
891
|
+
fs.writeFileSync(resolved, buffer);
|
|
892
|
+
}
|
|
893
|
+
const duration = Date.now() - start;
|
|
894
|
+
this.events.emit("action:end", { name: "screenshot", frameId: this.mainFrameId, durationMs: duration });
|
|
895
|
+
return buffer;
|
|
896
|
+
}
|
|
897
|
+
async screenshotBase64(options = {}) {
|
|
898
|
+
const start = Date.now();
|
|
899
|
+
this.events.emit("action:start", { name: "screenshotBase64", frameId: this.mainFrameId });
|
|
900
|
+
const result = await this.session.send("Page.captureScreenshot", {
|
|
901
|
+
format: options.format ?? "png",
|
|
902
|
+
quality: options.quality,
|
|
903
|
+
fromSurface: true
|
|
904
|
+
});
|
|
905
|
+
const duration = Date.now() - start;
|
|
906
|
+
this.events.emit("action:end", { name: "screenshotBase64", frameId: this.mainFrameId, durationMs: duration });
|
|
907
|
+
return result.data;
|
|
908
|
+
}
|
|
909
|
+
getEvents() {
|
|
910
|
+
return this.events;
|
|
911
|
+
}
|
|
912
|
+
getDefaultTimeout() {
|
|
913
|
+
return this.defaultTimeout;
|
|
914
|
+
}
|
|
915
|
+
buildFrameTree(tree) {
|
|
916
|
+
const frame = this.ensureFrame(tree.frame.id);
|
|
917
|
+
frame.setMeta({ name: tree.frame.name, url: tree.frame.url, parentId: tree.frame.parentId });
|
|
918
|
+
if (!tree.frame.parentId) {
|
|
919
|
+
this.mainFrameId = tree.frame.id;
|
|
920
|
+
}
|
|
921
|
+
if (tree.childFrames) {
|
|
922
|
+
for (const child of tree.childFrames) {
|
|
923
|
+
this.buildFrameTree(child);
|
|
924
|
+
}
|
|
925
|
+
}
|
|
926
|
+
}
|
|
927
|
+
ensureFrame(id) {
|
|
928
|
+
let frame = this.framesById.get(id);
|
|
929
|
+
if (!frame) {
|
|
930
|
+
frame = new Frame(id, this.session, this.logger, this.events);
|
|
931
|
+
this.framesById.set(id, frame);
|
|
932
|
+
}
|
|
933
|
+
return frame;
|
|
934
|
+
}
|
|
935
|
+
onFrameAttached(params) {
|
|
936
|
+
const frame = this.ensureFrame(params.frameId);
|
|
937
|
+
frame.setMeta({ parentId: params.parentFrameId });
|
|
938
|
+
}
|
|
939
|
+
onFrameNavigated(params) {
|
|
940
|
+
const frame = this.ensureFrame(params.frame.id);
|
|
941
|
+
frame.setMeta({ name: params.frame.name, url: params.frame.url, parentId: params.frame.parentId });
|
|
942
|
+
if (!params.frame.parentId) {
|
|
943
|
+
this.mainFrameId = params.frame.id;
|
|
944
|
+
}
|
|
945
|
+
}
|
|
946
|
+
onFrameDetached(params) {
|
|
947
|
+
this.framesById.delete(params.frameId);
|
|
948
|
+
}
|
|
949
|
+
onExecutionContextCreated(params) {
|
|
950
|
+
const frameId = params.context.auxData?.frameId;
|
|
951
|
+
if (!frameId) {
|
|
952
|
+
return;
|
|
953
|
+
}
|
|
954
|
+
const frame = this.ensureFrame(frameId);
|
|
955
|
+
frame.setExecutionContext(params.context.id);
|
|
956
|
+
}
|
|
957
|
+
onExecutionContextDestroyed(params) {
|
|
958
|
+
for (const frame of this.framesById.values()) {
|
|
959
|
+
if (frame.getExecutionContext() === params.executionContextId) {
|
|
960
|
+
frame.setExecutionContext(void 0);
|
|
961
|
+
}
|
|
962
|
+
}
|
|
963
|
+
}
|
|
964
|
+
onExecutionContextsCleared() {
|
|
965
|
+
for (const frame of this.framesById.values()) {
|
|
966
|
+
frame.setExecutionContext(void 0);
|
|
967
|
+
}
|
|
968
|
+
}
|
|
969
|
+
onLifecycleEvent(params) {
|
|
970
|
+
if (!this.lifecycleEvents.has(params.frameId)) {
|
|
971
|
+
this.lifecycleEvents.set(params.frameId, /* @__PURE__ */ new Set());
|
|
972
|
+
}
|
|
973
|
+
this.lifecycleEvents.get(params.frameId).add(params.name);
|
|
974
|
+
}
|
|
975
|
+
async waitForLifecycle(frameId, eventName, timeoutMs) {
|
|
976
|
+
if (!frameId) {
|
|
977
|
+
throw new Error("Missing frame id for lifecycle wait");
|
|
978
|
+
}
|
|
979
|
+
const start = Date.now();
|
|
980
|
+
while (Date.now() - start < timeoutMs) {
|
|
981
|
+
const events = this.lifecycleEvents.get(frameId);
|
|
982
|
+
if (events && events.has(eventName)) {
|
|
983
|
+
return;
|
|
984
|
+
}
|
|
985
|
+
await new Promise((resolve) => setTimeout(resolve, 100));
|
|
986
|
+
}
|
|
987
|
+
throw new Error(`Timeout waiting for lifecycle event: ${eventName}`);
|
|
988
|
+
}
|
|
989
|
+
};
|
|
990
|
+
|
|
991
|
+
// src/core/Browser.ts
|
|
992
|
+
var Browser = class {
|
|
993
|
+
connection;
|
|
994
|
+
process;
|
|
995
|
+
logger;
|
|
996
|
+
events;
|
|
997
|
+
constructor(connection, child, logger, events) {
|
|
998
|
+
this.connection = connection;
|
|
999
|
+
this.process = child;
|
|
1000
|
+
this.logger = logger;
|
|
1001
|
+
this.events = events;
|
|
1002
|
+
}
|
|
1003
|
+
on(event, handler) {
|
|
1004
|
+
this.events.on(event, handler);
|
|
1005
|
+
}
|
|
1006
|
+
async newPage() {
|
|
1007
|
+
const { targetId } = await this.connection.send("Target.createTarget", { url: "about:blank" });
|
|
1008
|
+
const { sessionId } = await this.connection.send("Target.attachToTarget", { targetId, flatten: true });
|
|
1009
|
+
const session = this.connection.createSession(sessionId);
|
|
1010
|
+
const page = new Page(session, this.logger, this.events);
|
|
1011
|
+
await page.initialize();
|
|
1012
|
+
return page;
|
|
1013
|
+
}
|
|
1014
|
+
async close() {
|
|
1015
|
+
try {
|
|
1016
|
+
await this.connection.send("Browser.close");
|
|
1017
|
+
} catch {
|
|
1018
|
+
}
|
|
1019
|
+
await this.connection.close();
|
|
1020
|
+
if (!this.process.killed) {
|
|
1021
|
+
this.process.kill();
|
|
1022
|
+
}
|
|
1023
|
+
}
|
|
1024
|
+
};
|
|
1025
|
+
|
|
1026
|
+
// src/browser/Downloader.ts
|
|
1027
|
+
import fs2 from "fs";
|
|
1028
|
+
import path2 from "path";
|
|
1029
|
+
import os from "os";
|
|
1030
|
+
import https from "https";
|
|
1031
|
+
import { spawn } from "child_process";
|
|
1032
|
+
import yauzl from "yauzl";
|
|
1033
|
+
var SNAPSHOT_BASE = "https://commondatastorage.googleapis.com/chromium-browser-snapshots";
|
|
1034
|
+
function detectPlatform(platform = process.platform) {
|
|
1035
|
+
if (platform === "linux") return "linux";
|
|
1036
|
+
if (platform === "darwin") return "mac";
|
|
1037
|
+
if (platform === "win32") return "win";
|
|
1038
|
+
throw new Error(`Unsupported platform: ${platform}`);
|
|
1039
|
+
}
|
|
1040
|
+
function platformFolder(platform) {
|
|
1041
|
+
if (platform === "linux") return "Linux_x64";
|
|
1042
|
+
if (platform === "mac") return "Mac";
|
|
1043
|
+
return "Win";
|
|
1044
|
+
}
|
|
1045
|
+
function defaultCacheRoot(platform) {
|
|
1046
|
+
if (platform === "win") {
|
|
1047
|
+
const localAppData = process.env.LOCALAPPDATA || path2.join(os.homedir(), "AppData", "Local");
|
|
1048
|
+
return path2.join(localAppData, "chromium-automaton");
|
|
1049
|
+
}
|
|
1050
|
+
return path2.join(os.homedir(), ".cache", "chromium-automaton");
|
|
1051
|
+
}
|
|
1052
|
+
function ensureWithinRoot(root, target) {
|
|
1053
|
+
const resolvedRoot = path2.resolve(root);
|
|
1054
|
+
const resolvedTarget = path2.resolve(target);
|
|
1055
|
+
if (resolvedTarget === resolvedRoot) {
|
|
1056
|
+
return;
|
|
1057
|
+
}
|
|
1058
|
+
if (!resolvedTarget.startsWith(resolvedRoot + path2.sep)) {
|
|
1059
|
+
throw new Error(`Path escapes cache root: ${resolvedTarget}`);
|
|
1060
|
+
}
|
|
1061
|
+
}
|
|
1062
|
+
function chromiumExecutableRelativePath(platform) {
|
|
1063
|
+
if (platform === "linux") return path2.join("chrome-linux", "chrome");
|
|
1064
|
+
if (platform === "mac") return path2.join("chrome-mac", "Chromium.app", "Contents", "MacOS", "Chromium");
|
|
1065
|
+
return path2.join("chrome-win", "chrome.exe");
|
|
1066
|
+
}
|
|
1067
|
+
async function fetchLatestRevision(platform) {
|
|
1068
|
+
const folder = platformFolder(platform);
|
|
1069
|
+
const url = `${SNAPSHOT_BASE}/${folder}/LAST_CHANGE`;
|
|
1070
|
+
return new Promise((resolve, reject) => {
|
|
1071
|
+
https.get(url, (res) => {
|
|
1072
|
+
if (res.statusCode && res.statusCode >= 400) {
|
|
1073
|
+
reject(new Error(`Failed to fetch LAST_CHANGE: ${res.statusCode}`));
|
|
1074
|
+
return;
|
|
1075
|
+
}
|
|
1076
|
+
let data = "";
|
|
1077
|
+
res.on("data", (chunk) => data += chunk.toString());
|
|
1078
|
+
res.on("end", () => resolve(data.trim()));
|
|
1079
|
+
}).on("error", reject);
|
|
1080
|
+
});
|
|
1081
|
+
}
|
|
1082
|
+
async function ensureDownloaded(options) {
|
|
1083
|
+
const { cacheRoot, platform, revision, logger } = options;
|
|
1084
|
+
ensureWithinRoot(cacheRoot, cacheRoot);
|
|
1085
|
+
const platformDir = path2.join(cacheRoot, platform);
|
|
1086
|
+
const revisionDir = path2.join(platformDir, revision);
|
|
1087
|
+
ensureWithinRoot(cacheRoot, revisionDir);
|
|
1088
|
+
const executablePath = path2.join(revisionDir, chromiumExecutableRelativePath(platform));
|
|
1089
|
+
const markerFile = path2.join(revisionDir, "INSTALLATION_COMPLETE");
|
|
1090
|
+
if (fs2.existsSync(executablePath) && fs2.existsSync(markerFile)) {
|
|
1091
|
+
return { executablePath, revisionDir };
|
|
1092
|
+
}
|
|
1093
|
+
fs2.mkdirSync(revisionDir, { recursive: true });
|
|
1094
|
+
const folder = platformFolder(platform);
|
|
1095
|
+
const zipName = platform === "win" ? "chrome-win.zip" : platform === "mac" ? "chrome-mac.zip" : "chrome-linux.zip";
|
|
1096
|
+
const downloadUrl = `${SNAPSHOT_BASE}/${folder}/${revision}/${zipName}`;
|
|
1097
|
+
const tempZipPath = path2.join(os.tmpdir(), `chromium-automaton-${platform}-${revision}.zip`);
|
|
1098
|
+
logger.info("Downloading Chromium snapshot", downloadUrl);
|
|
1099
|
+
await downloadFile(downloadUrl, tempZipPath, logger);
|
|
1100
|
+
logger.info("Extracting Chromium snapshot", tempZipPath);
|
|
1101
|
+
await extractZipSafe(tempZipPath, revisionDir);
|
|
1102
|
+
fs2.writeFileSync(markerFile, (/* @__PURE__ */ new Date()).toISOString());
|
|
1103
|
+
fs2.unlinkSync(tempZipPath);
|
|
1104
|
+
if (!fs2.existsSync(executablePath)) {
|
|
1105
|
+
throw new Error(`Executable not found after extraction: ${executablePath}`);
|
|
1106
|
+
}
|
|
1107
|
+
ensureExecutable(executablePath, platform);
|
|
1108
|
+
return { executablePath, revisionDir };
|
|
1109
|
+
}
|
|
1110
|
+
function downloadFile(url, dest, logger) {
|
|
1111
|
+
return new Promise((resolve, reject) => {
|
|
1112
|
+
const file = fs2.createWriteStream(dest);
|
|
1113
|
+
https.get(url, (res) => {
|
|
1114
|
+
if (res.statusCode && res.statusCode >= 400) {
|
|
1115
|
+
reject(new Error(`Failed to download: ${res.statusCode}`));
|
|
1116
|
+
return;
|
|
1117
|
+
}
|
|
1118
|
+
const total = Number(res.headers["content-length"] || 0);
|
|
1119
|
+
let downloaded = 0;
|
|
1120
|
+
let lastLoggedPercent = -1;
|
|
1121
|
+
let lastLoggedTime = Date.now();
|
|
1122
|
+
res.pipe(file);
|
|
1123
|
+
res.on("data", (chunk) => {
|
|
1124
|
+
downloaded += chunk.length;
|
|
1125
|
+
if (!total) {
|
|
1126
|
+
const now = Date.now();
|
|
1127
|
+
if (now - lastLoggedTime > 2e3) {
|
|
1128
|
+
logger.info("Download progress", `${(downloaded / (1024 * 1024)).toFixed(1)} MB`);
|
|
1129
|
+
lastLoggedTime = now;
|
|
1130
|
+
}
|
|
1131
|
+
return;
|
|
1132
|
+
}
|
|
1133
|
+
const percent = Math.floor(downloaded / total * 100);
|
|
1134
|
+
if (percent >= lastLoggedPercent + 5) {
|
|
1135
|
+
logger.info("Download progress", `${percent}%`);
|
|
1136
|
+
lastLoggedPercent = percent;
|
|
1137
|
+
}
|
|
1138
|
+
});
|
|
1139
|
+
file.on("finish", () => file.close(() => resolve()));
|
|
1140
|
+
}).on("error", (err) => {
|
|
1141
|
+
fs2.unlink(dest, () => reject(err));
|
|
1142
|
+
});
|
|
1143
|
+
});
|
|
1144
|
+
}
|
|
1145
|
+
async function extractZipSafe(zipPath, destDir) {
|
|
1146
|
+
return new Promise((resolve, reject) => {
|
|
1147
|
+
yauzl.open(zipPath, { lazyEntries: true }, (err, zipfile) => {
|
|
1148
|
+
if (err || !zipfile) {
|
|
1149
|
+
reject(err || new Error("Unable to open zip"));
|
|
1150
|
+
return;
|
|
1151
|
+
}
|
|
1152
|
+
zipfile.readEntry();
|
|
1153
|
+
zipfile.on("entry", (entry) => {
|
|
1154
|
+
const entryPath = entry.fileName.replace(/\\/g, "/");
|
|
1155
|
+
const targetPath = path2.join(destDir, entryPath);
|
|
1156
|
+
try {
|
|
1157
|
+
ensureWithinRoot(destDir, targetPath);
|
|
1158
|
+
} catch (error) {
|
|
1159
|
+
zipfile.close();
|
|
1160
|
+
reject(error);
|
|
1161
|
+
return;
|
|
1162
|
+
}
|
|
1163
|
+
if (/\/$/.test(entry.fileName)) {
|
|
1164
|
+
fs2.mkdirSync(targetPath, { recursive: true });
|
|
1165
|
+
zipfile.readEntry();
|
|
1166
|
+
return;
|
|
1167
|
+
}
|
|
1168
|
+
fs2.mkdirSync(path2.dirname(targetPath), { recursive: true });
|
|
1169
|
+
zipfile.openReadStream(entry, (streamErr, readStream) => {
|
|
1170
|
+
if (streamErr || !readStream) {
|
|
1171
|
+
zipfile.close();
|
|
1172
|
+
reject(streamErr || new Error("Unable to read zip entry"));
|
|
1173
|
+
return;
|
|
1174
|
+
}
|
|
1175
|
+
const rawMode = entry.externalFileAttributes ? entry.externalFileAttributes >>> 16 & 65535 : 0;
|
|
1176
|
+
const mode = rawMode > 0 ? rawMode : void 0;
|
|
1177
|
+
const writeStream = fs2.createWriteStream(targetPath);
|
|
1178
|
+
readStream.pipe(writeStream);
|
|
1179
|
+
writeStream.on("error", (writeErr) => {
|
|
1180
|
+
zipfile.close();
|
|
1181
|
+
reject(writeErr);
|
|
1182
|
+
});
|
|
1183
|
+
writeStream.on("close", () => {
|
|
1184
|
+
if (mode && mode <= 511) {
|
|
1185
|
+
try {
|
|
1186
|
+
fs2.chmodSync(targetPath, mode);
|
|
1187
|
+
} catch {
|
|
1188
|
+
}
|
|
1189
|
+
}
|
|
1190
|
+
zipfile.readEntry();
|
|
1191
|
+
});
|
|
1192
|
+
});
|
|
1193
|
+
});
|
|
1194
|
+
zipfile.on("end", () => {
|
|
1195
|
+
zipfile.close();
|
|
1196
|
+
resolve();
|
|
1197
|
+
});
|
|
1198
|
+
zipfile.on("error", (zipErr) => {
|
|
1199
|
+
zipfile.close();
|
|
1200
|
+
reject(zipErr);
|
|
1201
|
+
});
|
|
1202
|
+
});
|
|
1203
|
+
});
|
|
1204
|
+
}
|
|
1205
|
+
async function chromiumVersion(executablePath) {
|
|
1206
|
+
return new Promise((resolve, reject) => {
|
|
1207
|
+
const child = spawn(executablePath, ["--version"], { stdio: ["ignore", "pipe", "pipe"] });
|
|
1208
|
+
let output = "";
|
|
1209
|
+
child.stdout.on("data", (chunk) => output += chunk.toString());
|
|
1210
|
+
child.on("close", (code) => {
|
|
1211
|
+
if (code === 0) {
|
|
1212
|
+
resolve(output.trim());
|
|
1213
|
+
} else {
|
|
1214
|
+
reject(new Error(`Failed to get Chromium version: ${code}`));
|
|
1215
|
+
}
|
|
1216
|
+
});
|
|
1217
|
+
child.on("error", reject);
|
|
1218
|
+
});
|
|
1219
|
+
}
|
|
1220
|
+
function ensureExecutable(executablePath, platform) {
|
|
1221
|
+
if (platform === "win") {
|
|
1222
|
+
return;
|
|
1223
|
+
}
|
|
1224
|
+
try {
|
|
1225
|
+
const stat = fs2.statSync(executablePath);
|
|
1226
|
+
const isExecutable = (stat.mode & 73) !== 0;
|
|
1227
|
+
if (!isExecutable) {
|
|
1228
|
+
fs2.chmodSync(executablePath, 493);
|
|
1229
|
+
}
|
|
1230
|
+
} catch {
|
|
1231
|
+
}
|
|
1232
|
+
}
|
|
1233
|
+
|
|
1234
|
+
// src/browser/Revision.ts
|
|
1235
|
+
var PINNED_REVISION = "1567454";
|
|
1236
|
+
function resolveRevision(envRevision) {
|
|
1237
|
+
if (envRevision && envRevision.trim()) {
|
|
1238
|
+
return envRevision.trim();
|
|
1239
|
+
}
|
|
1240
|
+
return PINNED_REVISION;
|
|
1241
|
+
}
|
|
1242
|
+
|
|
1243
|
+
// src/browser/ChromiumManager.ts
|
|
1244
|
+
var ChromiumManager = class {
|
|
1245
|
+
logger;
|
|
1246
|
+
constructor(logger) {
|
|
1247
|
+
const envLevel = process.env.CHROMIUM_AUTOMATON_LOG_LEVEL ?? "info";
|
|
1248
|
+
this.logger = logger ?? new Logger(envLevel);
|
|
1249
|
+
}
|
|
1250
|
+
getLogger() {
|
|
1251
|
+
return this.logger;
|
|
1252
|
+
}
|
|
1253
|
+
async download(options = {}) {
|
|
1254
|
+
const platform = detectPlatform();
|
|
1255
|
+
const cacheRoot = this.resolveCacheRoot(platform);
|
|
1256
|
+
const overrideExecutable = process.env.CHROMIUM_AUTOMATON_EXECUTABLE_PATH;
|
|
1257
|
+
let revision = options.latest ? await fetchLatestRevision(platform) : resolveRevision(process.env.CHROMIUM_AUTOMATON_REVISION);
|
|
1258
|
+
let executablePath;
|
|
1259
|
+
let revisionDir = "";
|
|
1260
|
+
if (overrideExecutable) {
|
|
1261
|
+
executablePath = path3.resolve(overrideExecutable);
|
|
1262
|
+
} else {
|
|
1263
|
+
const downloaded = await ensureDownloaded({
|
|
1264
|
+
cacheRoot,
|
|
1265
|
+
platform,
|
|
1266
|
+
revision,
|
|
1267
|
+
logger: this.logger
|
|
1268
|
+
});
|
|
1269
|
+
executablePath = downloaded.executablePath;
|
|
1270
|
+
revisionDir = downloaded.revisionDir;
|
|
1271
|
+
}
|
|
1272
|
+
if (!fs3.existsSync(executablePath)) {
|
|
1273
|
+
throw new Error(`Chromium executable not found: ${executablePath}`);
|
|
1274
|
+
}
|
|
1275
|
+
const version = await chromiumVersion(executablePath);
|
|
1276
|
+
this.logger.info("Chromium cache root", cacheRoot);
|
|
1277
|
+
this.logger.info("Platform", platform);
|
|
1278
|
+
this.logger.info("Revision", revision);
|
|
1279
|
+
this.logger.info("Chromium version", version);
|
|
1280
|
+
return {
|
|
1281
|
+
cacheRoot,
|
|
1282
|
+
platform,
|
|
1283
|
+
revision,
|
|
1284
|
+
executablePath,
|
|
1285
|
+
revisionDir,
|
|
1286
|
+
chromiumVersion: version
|
|
1287
|
+
};
|
|
1288
|
+
}
|
|
1289
|
+
async launch(options = {}) {
|
|
1290
|
+
const logger = this.logger;
|
|
1291
|
+
if (options.logLevel) {
|
|
1292
|
+
logger.setLevel(options.logLevel);
|
|
1293
|
+
}
|
|
1294
|
+
const executablePath = options.executablePath || process.env.CHROMIUM_AUTOMATON_EXECUTABLE_PATH;
|
|
1295
|
+
let resolvedExecutable = executablePath;
|
|
1296
|
+
if (!resolvedExecutable) {
|
|
1297
|
+
const platform = detectPlatform();
|
|
1298
|
+
const cacheRoot = this.resolveCacheRoot(platform);
|
|
1299
|
+
const revision = resolveRevision(process.env.CHROMIUM_AUTOMATON_REVISION);
|
|
1300
|
+
const downloaded = await ensureDownloaded({
|
|
1301
|
+
cacheRoot,
|
|
1302
|
+
platform,
|
|
1303
|
+
revision,
|
|
1304
|
+
logger
|
|
1305
|
+
});
|
|
1306
|
+
resolvedExecutable = downloaded.executablePath;
|
|
1307
|
+
}
|
|
1308
|
+
if (!resolvedExecutable || !fs3.existsSync(resolvedExecutable)) {
|
|
1309
|
+
throw new Error(`Chromium executable not found: ${resolvedExecutable}`);
|
|
1310
|
+
}
|
|
1311
|
+
const stats = fs3.statSync(resolvedExecutable);
|
|
1312
|
+
if (!stats.isFile()) {
|
|
1313
|
+
throw new Error(`Chromium executable is not a file: ${resolvedExecutable}`);
|
|
1314
|
+
}
|
|
1315
|
+
ensureExecutable2(resolvedExecutable);
|
|
1316
|
+
const args = [
|
|
1317
|
+
"--remote-debugging-port=0",
|
|
1318
|
+
"--no-first-run",
|
|
1319
|
+
"--no-default-browser-check",
|
|
1320
|
+
"--disable-background-networking",
|
|
1321
|
+
"--disable-background-timer-throttling",
|
|
1322
|
+
"--disable-backgrounding-occluded-windows",
|
|
1323
|
+
"--disable-renderer-backgrounding"
|
|
1324
|
+
];
|
|
1325
|
+
if (process.platform === "linux") {
|
|
1326
|
+
args.push("--disable-crash-reporter", "--disable-crashpad");
|
|
1327
|
+
}
|
|
1328
|
+
if (options.headless ?? true) {
|
|
1329
|
+
args.push("--headless=new");
|
|
1330
|
+
}
|
|
1331
|
+
if (options.args) {
|
|
1332
|
+
args.push(...options.args);
|
|
1333
|
+
}
|
|
1334
|
+
logger.info("Launching Chromium", resolvedExecutable);
|
|
1335
|
+
const child = spawn2(resolvedExecutable, args, { stdio: ["ignore", "pipe", "pipe"] });
|
|
1336
|
+
const websocketUrl = await waitForWebSocketEndpoint(child, logger, options.timeoutMs ?? 3e4);
|
|
1337
|
+
const httpUrl = toHttpVersionUrl(websocketUrl);
|
|
1338
|
+
const wsEndpoint = await fetchWebSocketDebuggerUrl(httpUrl);
|
|
1339
|
+
const connection = new Connection(wsEndpoint, logger);
|
|
1340
|
+
const events = new AutomationEvents();
|
|
1341
|
+
const browser = new Browser(connection, child, logger, events);
|
|
1342
|
+
return browser;
|
|
1343
|
+
}
|
|
1344
|
+
resolveCacheRoot(platform) {
|
|
1345
|
+
const envRoot = process.env.CHROMIUM_AUTOMATON_CACHE_DIR;
|
|
1346
|
+
if (envRoot && envRoot.trim()) {
|
|
1347
|
+
return path3.resolve(envRoot.trim());
|
|
1348
|
+
}
|
|
1349
|
+
return defaultCacheRoot(platform);
|
|
1350
|
+
}
|
|
1351
|
+
};
|
|
1352
|
+
function ensureExecutable2(executablePath) {
|
|
1353
|
+
if (process.platform === "win32") {
|
|
1354
|
+
return;
|
|
1355
|
+
}
|
|
1356
|
+
try {
|
|
1357
|
+
const stat = fs3.statSync(executablePath);
|
|
1358
|
+
const isExecutable = (stat.mode & 73) !== 0;
|
|
1359
|
+
if (!isExecutable) {
|
|
1360
|
+
fs3.chmodSync(executablePath, 493);
|
|
1361
|
+
}
|
|
1362
|
+
} catch {
|
|
1363
|
+
}
|
|
1364
|
+
const dir = path3.dirname(executablePath);
|
|
1365
|
+
const helpers = [
|
|
1366
|
+
"chrome_crashpad_handler",
|
|
1367
|
+
"chrome_sandbox",
|
|
1368
|
+
"chrome-wrapper",
|
|
1369
|
+
"xdg-mime",
|
|
1370
|
+
"xdg-settings"
|
|
1371
|
+
];
|
|
1372
|
+
for (const name of helpers) {
|
|
1373
|
+
const helperPath = path3.join(dir, name);
|
|
1374
|
+
if (!fs3.existsSync(helperPath)) {
|
|
1375
|
+
continue;
|
|
1376
|
+
}
|
|
1377
|
+
try {
|
|
1378
|
+
const stat = fs3.statSync(helperPath);
|
|
1379
|
+
const isExecutable = (stat.mode & 73) !== 0;
|
|
1380
|
+
if (!isExecutable) {
|
|
1381
|
+
fs3.chmodSync(helperPath, 493);
|
|
1382
|
+
}
|
|
1383
|
+
} catch {
|
|
1384
|
+
}
|
|
1385
|
+
}
|
|
1386
|
+
}
|
|
1387
|
+
function waitForWebSocketEndpoint(child, logger, timeoutMs) {
|
|
1388
|
+
return new Promise((resolve, reject) => {
|
|
1389
|
+
const start = Date.now();
|
|
1390
|
+
const timeout = setTimeout(() => {
|
|
1391
|
+
reject(new Error("Timed out waiting for DevTools endpoint"));
|
|
1392
|
+
}, timeoutMs);
|
|
1393
|
+
const outputLines = [];
|
|
1394
|
+
const pushOutput = (data) => {
|
|
1395
|
+
const text = data.toString();
|
|
1396
|
+
for (const line of text.split(/\r?\n/)) {
|
|
1397
|
+
if (!line.trim()) continue;
|
|
1398
|
+
outputLines.push(line);
|
|
1399
|
+
if (outputLines.length > 50) {
|
|
1400
|
+
outputLines.shift();
|
|
1401
|
+
}
|
|
1402
|
+
}
|
|
1403
|
+
};
|
|
1404
|
+
const onData = (data) => {
|
|
1405
|
+
const text = data.toString();
|
|
1406
|
+
const match = text.match(/DevTools listening on (ws:\/\/[^\s]+)/);
|
|
1407
|
+
if (match) {
|
|
1408
|
+
clearTimeout(timeout);
|
|
1409
|
+
cleanup();
|
|
1410
|
+
logger.info("DevTools endpoint", match[1]);
|
|
1411
|
+
resolve(match[1]);
|
|
1412
|
+
}
|
|
1413
|
+
pushOutput(data);
|
|
1414
|
+
};
|
|
1415
|
+
const onExit = (code, signal) => {
|
|
1416
|
+
cleanup();
|
|
1417
|
+
const tail = outputLines.length ? `
|
|
1418
|
+
Chromium output:
|
|
1419
|
+
${outputLines.join("\n")}` : "";
|
|
1420
|
+
reject(new Error(`Chromium exited early with code ${code ?? "null"} signal ${signal ?? "null"}${tail}`));
|
|
1421
|
+
};
|
|
1422
|
+
const cleanup = () => {
|
|
1423
|
+
child.stdout?.off("data", onData);
|
|
1424
|
+
child.stderr?.off("data", onData);
|
|
1425
|
+
child.off("exit", onExit);
|
|
1426
|
+
};
|
|
1427
|
+
child.stdout?.on("data", onData);
|
|
1428
|
+
child.stderr?.on("data", onData);
|
|
1429
|
+
child.on("exit", onExit);
|
|
1430
|
+
if (Date.now() - start > timeoutMs) {
|
|
1431
|
+
cleanup();
|
|
1432
|
+
}
|
|
1433
|
+
});
|
|
1434
|
+
}
|
|
1435
|
+
function toHttpVersionUrl(wsUrl) {
|
|
1436
|
+
try {
|
|
1437
|
+
const url = new URL(wsUrl);
|
|
1438
|
+
const port = url.port || "9222";
|
|
1439
|
+
return `http://127.0.0.1:${port}/json/version`;
|
|
1440
|
+
} catch {
|
|
1441
|
+
throw new Error(`Invalid DevTools endpoint: ${wsUrl}`);
|
|
1442
|
+
}
|
|
1443
|
+
}
|
|
1444
|
+
function fetchWebSocketDebuggerUrl(versionUrl) {
|
|
1445
|
+
return new Promise((resolve, reject) => {
|
|
1446
|
+
http.get(versionUrl, (res) => {
|
|
1447
|
+
if (res.statusCode && res.statusCode >= 400) {
|
|
1448
|
+
reject(new Error(`Failed to fetch /json/version: ${res.statusCode}`));
|
|
1449
|
+
return;
|
|
1450
|
+
}
|
|
1451
|
+
let data = "";
|
|
1452
|
+
res.on("data", (chunk) => data += chunk.toString());
|
|
1453
|
+
res.on("end", () => {
|
|
1454
|
+
try {
|
|
1455
|
+
const parsed = JSON.parse(data);
|
|
1456
|
+
if (!parsed.webSocketDebuggerUrl) {
|
|
1457
|
+
reject(new Error("webSocketDebuggerUrl missing"));
|
|
1458
|
+
return;
|
|
1459
|
+
}
|
|
1460
|
+
resolve(parsed.webSocketDebuggerUrl);
|
|
1461
|
+
} catch (err) {
|
|
1462
|
+
reject(err);
|
|
1463
|
+
}
|
|
1464
|
+
});
|
|
1465
|
+
}).on("error", reject);
|
|
1466
|
+
});
|
|
1467
|
+
}
|
|
1468
|
+
|
|
1469
|
+
// src/cli.ts
|
|
1470
|
+
function printHelp() {
|
|
1471
|
+
console.log("chromium-automaton download [--latest]");
|
|
1472
|
+
}
|
|
1473
|
+
async function main() {
|
|
1474
|
+
const [, , command, ...rest] = process.argv;
|
|
1475
|
+
if (!command || command === "--help" || command === "-h") {
|
|
1476
|
+
printHelp();
|
|
1477
|
+
process.exit(0);
|
|
1478
|
+
}
|
|
1479
|
+
if (command !== "download") {
|
|
1480
|
+
console.error(`Unknown command: ${command}`);
|
|
1481
|
+
printHelp();
|
|
1482
|
+
process.exit(1);
|
|
1483
|
+
}
|
|
1484
|
+
const latest = rest.includes("--latest");
|
|
1485
|
+
const manager = new ChromiumManager();
|
|
1486
|
+
await manager.download({ latest });
|
|
1487
|
+
}
|
|
1488
|
+
main().catch((err) => {
|
|
1489
|
+
console.error(err instanceof Error ? err.message : String(err));
|
|
1490
|
+
process.exit(1);
|
|
1491
|
+
});
|
|
1492
|
+
//# sourceMappingURL=cli.js.map
|