@pyrokine/mcp-chrome 2.4.1 → 2.4.4
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 +213 -158
- package/README_zh.md +245 -187
- package/dist/anti-detection/behavior.js +1 -1
- package/dist/anti-detection/behavior.js.map +1 -1
- package/dist/anti-detection/injection.d.ts.map +1 -1
- package/dist/anti-detection/injection.js +2 -1
- package/dist/anti-detection/injection.js.map +1 -1
- package/dist/cdp/client.d.ts +2 -2
- package/dist/cdp/client.d.ts.map +1 -1
- package/dist/cdp/client.js +7 -4
- package/dist/cdp/client.js.map +1 -1
- package/dist/core/browser-driver.d.ts +3 -2
- package/dist/core/browser-driver.d.ts.map +1 -1
- package/dist/core/browser-driver.js +1 -0
- package/dist/core/browser-driver.js.map +1 -1
- package/dist/core/errors.d.ts +9 -0
- package/dist/core/errors.d.ts.map +1 -1
- package/dist/core/errors.js +16 -1
- package/dist/core/errors.js.map +1 -1
- package/dist/core/os-mouse.d.ts +65 -0
- package/dist/core/os-mouse.d.ts.map +1 -0
- package/dist/core/os-mouse.js +334 -0
- package/dist/core/os-mouse.js.map +1 -0
- package/dist/core/session.d.ts +14 -4
- package/dist/core/session.d.ts.map +1 -1
- package/dist/core/session.js +159 -54
- package/dist/core/session.js.map +1 -1
- package/dist/core/types.d.ts +10 -0
- package/dist/core/types.d.ts.map +1 -1
- package/dist/core/types.js +2 -0
- package/dist/core/types.js.map +1 -1
- package/dist/core/unified-session.d.ts +24 -3
- package/dist/core/unified-session.d.ts.map +1 -1
- package/dist/core/unified-session.js +143 -25
- package/dist/core/unified-session.js.map +1 -1
- package/dist/extension/bridge.d.ts +16 -0
- package/dist/extension/bridge.d.ts.map +1 -1
- package/dist/extension/bridge.js +27 -0
- package/dist/extension/bridge.js.map +1 -1
- package/dist/extension/http-server.d.ts.map +1 -1
- package/dist/extension/http-server.js +6 -1
- package/dist/extension/http-server.js.map +1 -1
- package/dist/tools/browse.d.ts +1 -1
- package/dist/tools/browse.d.ts.map +1 -1
- package/dist/tools/browse.js +72 -26
- package/dist/tools/browse.js.map +1 -1
- package/dist/tools/challenge.d.ts +153 -0
- package/dist/tools/challenge.d.ts.map +1 -0
- package/dist/tools/challenge.js +573 -0
- package/dist/tools/challenge.js.map +1 -0
- package/dist/tools/cookies.d.ts.map +1 -1
- package/dist/tools/cookies.js +8 -4
- package/dist/tools/cookies.js.map +1 -1
- package/dist/tools/diagnostics.d.ts +2 -2
- package/dist/tools/diagnostics.d.ts.map +1 -1
- package/dist/tools/diagnostics.js +26 -8
- package/dist/tools/diagnostics.js.map +1 -1
- package/dist/tools/evaluate.d.ts.map +1 -1
- package/dist/tools/evaluate.js +61 -9
- package/dist/tools/evaluate.js.map +1 -1
- package/dist/tools/extract.d.ts.map +1 -1
- package/dist/tools/extract.js +110 -51
- package/dist/tools/extract.js.map +1 -1
- package/dist/tools/input.d.ts.map +1 -1
- package/dist/tools/input.js +148 -22
- package/dist/tools/input.js.map +1 -1
- package/dist/tools/logs.js +2 -2
- package/dist/tools/logs.js.map +1 -1
- package/dist/tools/manage.d.ts.map +1 -1
- package/dist/tools/manage.js +20 -7
- package/dist/tools/manage.js.map +1 -1
- package/dist/tools/network-sanitizer.d.ts.map +1 -1
- package/dist/tools/network-sanitizer.js +1 -0
- package/dist/tools/network-sanitizer.js.map +1 -1
- package/dist/tools/png.js +5 -3
- package/dist/tools/png.js.map +1 -1
- package/dist/tools/post-condition.d.ts.map +1 -1
- package/dist/tools/post-condition.js +12 -3
- package/dist/tools/post-condition.js.map +1 -1
- package/dist/tools/target-diagnostics.d.ts +7 -0
- package/dist/tools/target-diagnostics.d.ts.map +1 -1
- package/dist/tools/target-diagnostics.js +18 -1
- package/dist/tools/target-diagnostics.js.map +1 -1
- package/dist/tools/wait.d.ts.map +1 -1
- package/dist/tools/wait.js +41 -39
- package/dist/tools/wait.js.map +1 -1
- package/extension/dist/assets/{index.ts-GG6dPUPX.js → index.ts-Bqcu2qiN.js} +286 -9
- package/extension/dist/manifest.json +1 -1
- package/extension/dist/service-worker-loader.js +1 -1
- package/extension/dist/src/popup/popup.html +44 -44
- package/package.json +8 -7
- package/dist/extension/native-host-installer.d.ts +0 -21
- package/dist/extension/native-host-installer.d.ts.map +0 -1
- package/dist/extension/native-host-installer.js +0 -147
- package/dist/extension/native-host-installer.js.map +0 -1
- package/dist/extension/socket-server.d.ts +0 -32
- package/dist/extension/socket-server.d.ts.map +0 -1
- package/dist/extension/socket-server.js +0 -177
- package/dist/extension/socket-server.js.map +0 -1
- package/dist/extension/ws-server.d.ts +0 -40
- package/dist/extension/ws-server.d.ts.map +0 -1
- package/dist/extension/ws-server.js +0 -246
- package/dist/extension/ws-server.js.map +0 -1
- package/dist/native-host/index.js +0 -280
- package/dist/native-host/mcp-chrome-host +0 -2
package/dist/core/session.js
CHANGED
|
@@ -54,6 +54,12 @@ function matchesUrlPattern(url, pattern) {
|
|
|
54
54
|
}
|
|
55
55
|
return true;
|
|
56
56
|
}
|
|
57
|
+
function isCloudflareChallenge(headers) {
|
|
58
|
+
if (!headers) {
|
|
59
|
+
return false;
|
|
60
|
+
}
|
|
61
|
+
return Object.entries(headers).some(([name, value]) => name.toLowerCase() === 'cf-mitigated' && String(value).toLowerCase() === 'challenge');
|
|
62
|
+
}
|
|
57
63
|
/**
|
|
58
64
|
* 会话管理器(单例)
|
|
59
65
|
*/
|
|
@@ -75,6 +81,11 @@ class SessionManager {
|
|
|
75
81
|
operationLock = Promise.resolve();
|
|
76
82
|
consoleLogs = [];
|
|
77
83
|
networkRequests = [];
|
|
84
|
+
nextConsoleLogSequence = 0;
|
|
85
|
+
nextNetworkRequestSequence = 0;
|
|
86
|
+
challengeRequired = false;
|
|
87
|
+
runtimeEnabled = false;
|
|
88
|
+
mainFrameId = null;
|
|
78
89
|
requestMap = new Map();
|
|
79
90
|
// 监听器安装标志(防止重复安装)
|
|
80
91
|
listenersInstalled = false;
|
|
@@ -247,7 +258,7 @@ class SessionManager {
|
|
|
247
258
|
}
|
|
248
259
|
else {
|
|
249
260
|
const eventName = wait === 'domcontentloaded' ? 'Page.domContentEventFired' : 'Page.loadEventFired';
|
|
250
|
-
await this.cdp.waitForEvent(eventName, undefined, timeout);
|
|
261
|
+
await this.cdp.waitForEvent(eventName, undefined, timeout, this.sessionId);
|
|
251
262
|
}
|
|
252
263
|
// 更新状态
|
|
253
264
|
await this.updateState();
|
|
@@ -315,7 +326,7 @@ class SessionManager {
|
|
|
315
326
|
async reload(ignoreCache = false, _waitUntil, timeout = DEFAULT_TIMEOUT) {
|
|
316
327
|
return this.withLock(async () => {
|
|
317
328
|
this.ensureSession();
|
|
318
|
-
const waitPromise = this.cdp.waitForEvent('Page.loadEventFired', undefined, timeout);
|
|
329
|
+
const waitPromise = this.cdp.waitForEvent('Page.loadEventFired', undefined, timeout, this.sessionId);
|
|
319
330
|
waitPromise.catch(() => { });
|
|
320
331
|
await this.send('Page.reload', { ignoreCache }, timeout);
|
|
321
332
|
await waitPromise;
|
|
@@ -639,9 +650,9 @@ class SessionManager {
|
|
|
639
650
|
}
|
|
640
651
|
// ==================== IBrowserDriver: 页面读取 ====================
|
|
641
652
|
/** Extension readPage 等价物:CDP 通过页面 DOM 构造 pageContent 和 interactiveElements */
|
|
642
|
-
async readPage(
|
|
653
|
+
async readPage(options) {
|
|
643
654
|
const state = await this.getPageState();
|
|
644
|
-
const interactiveElements = await this.collectInteractiveElements();
|
|
655
|
+
const interactiveElements = await this.collectInteractiveElements(options?.depth);
|
|
645
656
|
const lines = interactiveElements.map((e) => {
|
|
646
657
|
let line = e.role;
|
|
647
658
|
if (e.name) {
|
|
@@ -655,8 +666,10 @@ class SessionManager {
|
|
|
655
666
|
interactiveElements,
|
|
656
667
|
};
|
|
657
668
|
}
|
|
658
|
-
async collectInteractiveElements() {
|
|
669
|
+
async collectInteractiveElements(depth) {
|
|
670
|
+
const maxDepth = Number.isInteger(depth) && depth !== undefined && depth >= 0 ? depth : 15;
|
|
659
671
|
return this.evaluate(`(() => {
|
|
672
|
+
var maxDepth = ${maxDepth};
|
|
660
673
|
function selectorFor(el) {
|
|
661
674
|
if (!(el instanceof Element)) return '';
|
|
662
675
|
if (el.id) return '#' + CSS.escape(el.id);
|
|
@@ -665,11 +678,16 @@ class SessionManager {
|
|
|
665
678
|
while (current && current.nodeType === Node.ELEMENT_NODE && parts.length < 4) {
|
|
666
679
|
var part = current.tagName.toLowerCase();
|
|
667
680
|
if (current.classList && current.classList.length) {
|
|
668
|
-
part += '.' + Array.from(current.classList)
|
|
681
|
+
part += '.' + Array.from(current.classList)
|
|
682
|
+
.slice(0, 2)
|
|
683
|
+
.map(function(cls) { return CSS.escape(cls); })
|
|
684
|
+
.join('.');
|
|
669
685
|
}
|
|
670
686
|
var parent = current.parentElement;
|
|
671
687
|
if (parent) {
|
|
672
|
-
var siblings = Array.from(parent.children).filter(function(child) {
|
|
688
|
+
var siblings = Array.from(parent.children).filter(function(child) {
|
|
689
|
+
return child.tagName === current.tagName;
|
|
690
|
+
});
|
|
673
691
|
if (siblings.length > 1) part += ':nth-of-type(' + (siblings.indexOf(current) + 1) + ')';
|
|
674
692
|
}
|
|
675
693
|
parts.unshift(part);
|
|
@@ -677,6 +695,15 @@ class SessionManager {
|
|
|
677
695
|
}
|
|
678
696
|
return parts.join(' > ');
|
|
679
697
|
}
|
|
698
|
+
function depthFor(el) {
|
|
699
|
+
var depth = 0;
|
|
700
|
+
var current = el;
|
|
701
|
+
while (current.parentElement) {
|
|
702
|
+
depth += 1;
|
|
703
|
+
current = current.parentElement;
|
|
704
|
+
}
|
|
705
|
+
return depth;
|
|
706
|
+
}
|
|
680
707
|
function roleFor(el) {
|
|
681
708
|
var explicit = el.getAttribute('role');
|
|
682
709
|
if (explicit) return explicit;
|
|
@@ -684,7 +711,9 @@ class SessionManager {
|
|
|
684
711
|
var type = (el.getAttribute('type') || '').toLowerCase();
|
|
685
712
|
if (tag === 'button' || type === 'button' || type === 'submit' || type === 'reset') return 'button';
|
|
686
713
|
if (tag === 'a' && el.hasAttribute('href')) return 'link';
|
|
687
|
-
if (tag === 'textarea' || tag === 'input' && !['checkbox', 'radio', 'range'].includes(type))
|
|
714
|
+
if (tag === 'textarea' || (tag === 'input' && !['checkbox', 'radio', 'range'].includes(type))) {
|
|
715
|
+
return 'textbox';
|
|
716
|
+
}
|
|
688
717
|
if (type === 'checkbox') return 'checkbox';
|
|
689
718
|
if (type === 'radio') return 'radio';
|
|
690
719
|
if (tag === 'select') return 'combobox';
|
|
@@ -693,15 +722,28 @@ class SessionManager {
|
|
|
693
722
|
return tag;
|
|
694
723
|
}
|
|
695
724
|
function nameFor(el) {
|
|
696
|
-
return (
|
|
725
|
+
return (
|
|
726
|
+
el.getAttribute('aria-label') ||
|
|
727
|
+
el.getAttribute('title') ||
|
|
728
|
+
el.getAttribute('placeholder') ||
|
|
729
|
+
el.textContent ||
|
|
730
|
+
''
|
|
731
|
+
).replace(new RegExp('\\s+', 'g'), ' ').trim().slice(0, 160);
|
|
697
732
|
}
|
|
698
|
-
var selector =
|
|
699
|
-
|
|
733
|
+
var selector = [
|
|
734
|
+
'button,a[href],input,textarea,select,[role]',
|
|
735
|
+
'[contenteditable="true"],[tabindex]:not([tabindex="-1"])'
|
|
736
|
+
].join(',');
|
|
737
|
+
return Array.from(document.querySelectorAll(selector)).filter(function(el) {
|
|
738
|
+
return depthFor(el) <= maxDepth;
|
|
739
|
+
}).slice(0, 100).map(function(el, index) {
|
|
700
740
|
var rect = el.getBoundingClientRect();
|
|
701
741
|
var cx = rect.left + rect.width / 2;
|
|
702
742
|
var cy = rect.top + rect.height / 2;
|
|
703
743
|
var top = rect.width > 0 && rect.height > 0 ? document.elementFromPoint(cx, cy) : null;
|
|
704
|
-
var
|
|
744
|
+
var style = getComputedStyle(el);
|
|
745
|
+
var visible = rect.width > 0 && rect.height > 0 &&
|
|
746
|
+
style.visibility !== 'hidden' && style.display !== 'none';
|
|
705
747
|
return {
|
|
706
748
|
refId: 'cdp-' + index,
|
|
707
749
|
role: roleFor(el),
|
|
@@ -988,9 +1030,9 @@ class SessionManager {
|
|
|
988
1030
|
return Promise.reject(new DriverCapabilityError('CDP 模式 stealth 脚本在 connect/launch 时通过 stealth 参数自动注入,不支持后续手动注入'));
|
|
989
1031
|
}
|
|
990
1032
|
// ==================== IBrowserDriver: 日志启用 ====================
|
|
991
|
-
/**
|
|
992
|
-
consoleEnable() {
|
|
993
|
-
|
|
1033
|
+
/** Runtime.enable 会暴露 CDP,只在需要 console 事件时再开 */
|
|
1034
|
+
async consoleEnable() {
|
|
1035
|
+
await this.ensureRuntimeEnabled();
|
|
994
1036
|
}
|
|
995
1037
|
/** CDP 模式:Network 域已在 attach 时启用,no-op */
|
|
996
1038
|
networkEnable() {
|
|
@@ -1115,9 +1157,9 @@ class SessionManager {
|
|
|
1115
1157
|
}
|
|
1116
1158
|
async clearCookies(filter) {
|
|
1117
1159
|
this.ensureSession();
|
|
1118
|
-
// Driver
|
|
1119
|
-
if (!filter || (!filter.url && !filter.domain
|
|
1120
|
-
throw new Error('clearCookies 必须带 url
|
|
1160
|
+
// Driver 级护栏:必须按 URL 或域名限定范围,name 只能进一步缩小范围
|
|
1161
|
+
if (!filter || (!filter.url && !filter.domain)) {
|
|
1162
|
+
throw new Error('clearCookies 必须带 url 或 domain 过滤参数,name 只能在该范围内进一步过滤(防止跨站误清同名 cookie)');
|
|
1121
1163
|
}
|
|
1122
1164
|
const urls = filter.url ? [filter.url] : undefined;
|
|
1123
1165
|
const { cookies } = (await this.send('Network.getCookies', urls ? { urls } : {}));
|
|
@@ -1153,7 +1195,7 @@ class SessionManager {
|
|
|
1153
1195
|
const lp = pattern.toLowerCase();
|
|
1154
1196
|
logs = logs.filter((l) => l.text.toLowerCase().includes(lp));
|
|
1155
1197
|
}
|
|
1156
|
-
const result = logs
|
|
1198
|
+
const result = logs;
|
|
1157
1199
|
if (clear) {
|
|
1158
1200
|
this.consoleLogs = [];
|
|
1159
1201
|
}
|
|
@@ -1165,13 +1207,16 @@ class SessionManager {
|
|
|
1165
1207
|
if (urlPattern) {
|
|
1166
1208
|
requests = requests.filter((r) => matchesUrlPattern(String(r.url ?? ''), urlPattern));
|
|
1167
1209
|
}
|
|
1168
|
-
const result = requests
|
|
1210
|
+
const result = requests;
|
|
1169
1211
|
if (clear) {
|
|
1170
1212
|
this.networkRequests = [];
|
|
1171
1213
|
this.requestMap.clear();
|
|
1172
1214
|
}
|
|
1173
1215
|
return result;
|
|
1174
1216
|
}
|
|
1217
|
+
async getChallengeState() {
|
|
1218
|
+
return this.challengeRequired;
|
|
1219
|
+
}
|
|
1175
1220
|
/**
|
|
1176
1221
|
* 清除日志
|
|
1177
1222
|
*/
|
|
@@ -1311,9 +1356,7 @@ class SessionManager {
|
|
|
1311
1356
|
await this.cdp.send('Target.closeTarget', { targetId: id });
|
|
1312
1357
|
// 如果关闭的是当前页面,清除会话状态
|
|
1313
1358
|
if (id === this.currentTargetId) {
|
|
1314
|
-
this.
|
|
1315
|
-
this.currentTargetId = null;
|
|
1316
|
-
this.state = null;
|
|
1359
|
+
this.clearAttachedTargetState();
|
|
1317
1360
|
}
|
|
1318
1361
|
});
|
|
1319
1362
|
}
|
|
@@ -1341,6 +1384,19 @@ class SessionManager {
|
|
|
1341
1384
|
getState() {
|
|
1342
1385
|
return this.state;
|
|
1343
1386
|
}
|
|
1387
|
+
async getLiveState() {
|
|
1388
|
+
if (!this.currentTargetId) {
|
|
1389
|
+
return this.state;
|
|
1390
|
+
}
|
|
1391
|
+
this.ensureConnected();
|
|
1392
|
+
const { targetInfo } = await this.sendBrowserCommand('Target.getTargetInfo', { targetId: this.currentTargetId });
|
|
1393
|
+
this.state = {
|
|
1394
|
+
url: targetInfo.url ?? '',
|
|
1395
|
+
title: targetInfo.title ?? '',
|
|
1396
|
+
targetId: this.currentTargetId,
|
|
1397
|
+
};
|
|
1398
|
+
return this.state;
|
|
1399
|
+
}
|
|
1344
1400
|
/**
|
|
1345
1401
|
* 是否已连接
|
|
1346
1402
|
*/
|
|
@@ -1399,6 +1455,7 @@ class SessionManager {
|
|
|
1399
1455
|
*/
|
|
1400
1456
|
buildNetworkIdlePromise(cdp, timeout, idleTime) {
|
|
1401
1457
|
const localPendingRequests = new Set();
|
|
1458
|
+
const sessionId = this.sessionId;
|
|
1402
1459
|
return new Promise((resolve, reject) => {
|
|
1403
1460
|
let idleTimer = null;
|
|
1404
1461
|
let timeoutTimer = null;
|
|
@@ -1418,12 +1475,18 @@ class SessionManager {
|
|
|
1418
1475
|
}
|
|
1419
1476
|
}
|
|
1420
1477
|
};
|
|
1421
|
-
const onRequestStart = (params) => {
|
|
1478
|
+
const onRequestStart = (params, eventSessionId) => {
|
|
1479
|
+
if (eventSessionId !== sessionId) {
|
|
1480
|
+
return;
|
|
1481
|
+
}
|
|
1422
1482
|
const { requestId } = params;
|
|
1423
1483
|
localPendingRequests.add(requestId);
|
|
1424
1484
|
checkIdle();
|
|
1425
1485
|
};
|
|
1426
|
-
const onRequestEnd = (params) => {
|
|
1486
|
+
const onRequestEnd = (params, eventSessionId) => {
|
|
1487
|
+
if (eventSessionId !== sessionId) {
|
|
1488
|
+
return;
|
|
1489
|
+
}
|
|
1427
1490
|
const { requestId } = params;
|
|
1428
1491
|
localPendingRequests.delete(requestId);
|
|
1429
1492
|
checkIdle();
|
|
@@ -1475,6 +1538,7 @@ class SessionManager {
|
|
|
1475
1538
|
// 忽略分离错误
|
|
1476
1539
|
}
|
|
1477
1540
|
}
|
|
1541
|
+
this.clearAttachedTargetState();
|
|
1478
1542
|
// 附加到目标
|
|
1479
1543
|
const { sessionId } = (await this.cdp.send('Target.attachToTarget', {
|
|
1480
1544
|
targetId,
|
|
@@ -1501,6 +1565,19 @@ class SessionManager {
|
|
|
1501
1565
|
title: '',
|
|
1502
1566
|
};
|
|
1503
1567
|
}
|
|
1568
|
+
clearAttachedTargetState() {
|
|
1569
|
+
this.clearLogs();
|
|
1570
|
+
this.challengeRequired = false;
|
|
1571
|
+
this.runtimeEnabled = false;
|
|
1572
|
+
this.mainFrameId = null;
|
|
1573
|
+
this.modifiers = 0;
|
|
1574
|
+
this.sessionId = null;
|
|
1575
|
+
this.currentTargetId = null;
|
|
1576
|
+
this.state = null;
|
|
1577
|
+
}
|
|
1578
|
+
isCurrentSessionEvent(eventSessionId) {
|
|
1579
|
+
return this.sessionId !== null && eventSessionId === this.sessionId;
|
|
1580
|
+
}
|
|
1504
1581
|
/**
|
|
1505
1582
|
* 重置所有状态(同步,不加锁)
|
|
1506
1583
|
*
|
|
@@ -1516,12 +1593,8 @@ class SessionManager {
|
|
|
1516
1593
|
this.launcher.close();
|
|
1517
1594
|
this.launcher = null;
|
|
1518
1595
|
}
|
|
1519
|
-
this.
|
|
1520
|
-
this.modifiers = 0;
|
|
1596
|
+
this.clearAttachedTargetState();
|
|
1521
1597
|
this.behaviorSimulator.setCurrentPosition({ x: 0, y: 0 });
|
|
1522
|
-
this.sessionId = null;
|
|
1523
|
-
this.currentTargetId = null;
|
|
1524
|
-
this.state = null;
|
|
1525
1598
|
this.listenersInstalled = false;
|
|
1526
1599
|
this.connectedPort = 0;
|
|
1527
1600
|
}
|
|
@@ -1546,13 +1619,14 @@ class SessionManager {
|
|
|
1546
1619
|
* 等待多个事件中的任一个触发
|
|
1547
1620
|
*
|
|
1548
1621
|
* 用于同时监听跨文档导航 (loadEventFired) 和同文档导航 (navigatedWithinDocument),
|
|
1549
|
-
*
|
|
1622
|
+
* 任意一个事件触发后清理所有监听器和超时定时器
|
|
1550
1623
|
* close() 时通过 'disconnected' 信号立即 reject,不必等 timer 超时
|
|
1551
1624
|
*/
|
|
1552
1625
|
waitForAnyEvent(events, timeout) {
|
|
1553
1626
|
// 捕获当前 cdp 引用,防止 close() 并发置 null 导致回调崩溃
|
|
1554
1627
|
const cdp = this.cdp;
|
|
1555
|
-
|
|
1628
|
+
const sessionId = this.sessionId;
|
|
1629
|
+
if (!cdp || !sessionId) {
|
|
1556
1630
|
return Promise.reject(new Error('CDP 连接已关闭'));
|
|
1557
1631
|
}
|
|
1558
1632
|
return new Promise((resolve, reject) => {
|
|
@@ -1574,7 +1648,10 @@ class SessionManager {
|
|
|
1574
1648
|
};
|
|
1575
1649
|
cdp.once('disconnected', onDisconnected);
|
|
1576
1650
|
for (const event of events) {
|
|
1577
|
-
const listener = () => {
|
|
1651
|
+
const listener = (_params, eventSessionId) => {
|
|
1652
|
+
if (eventSessionId !== sessionId) {
|
|
1653
|
+
return;
|
|
1654
|
+
}
|
|
1578
1655
|
cleanup();
|
|
1579
1656
|
resolve();
|
|
1580
1657
|
};
|
|
@@ -1591,20 +1668,17 @@ class SessionManager {
|
|
|
1591
1668
|
await Promise.all([
|
|
1592
1669
|
this.send('Page.enable'),
|
|
1593
1670
|
this.send('DOM.enable'),
|
|
1594
|
-
this.send('Runtime.enable'),
|
|
1595
1671
|
this.send('Network.enable'),
|
|
1596
1672
|
this.send('Log.enable'),
|
|
1597
1673
|
]);
|
|
1598
|
-
|
|
1674
|
+
const { frameTree } = await this.send('Page.getFrameTree');
|
|
1675
|
+
this.mainFrameId = frameTree.frame.id;
|
|
1676
|
+
// 根据 stealth 模式注入反检测脚本。不在当前文档 Runtime.evaluate,避免干扰 Cloudflare JS Challenge
|
|
1599
1677
|
if (this.stealthMode !== 'off') {
|
|
1600
1678
|
const script = getAntiDetectionScript(this.stealthMode);
|
|
1601
1679
|
await this.send('Page.addScriptToEvaluateOnNewDocument', {
|
|
1602
1680
|
source: script,
|
|
1603
1681
|
});
|
|
1604
|
-
// 对当前页面立即执行反检测脚本
|
|
1605
|
-
await this.send('Runtime.evaluate', {
|
|
1606
|
-
expression: script,
|
|
1607
|
-
});
|
|
1608
1682
|
}
|
|
1609
1683
|
// 监听事件
|
|
1610
1684
|
this.setupEventListeners();
|
|
@@ -1619,10 +1693,23 @@ class SessionManager {
|
|
|
1619
1693
|
return;
|
|
1620
1694
|
}
|
|
1621
1695
|
this.listenersInstalled = true;
|
|
1696
|
+
this.cdp.onEvent('Page.frameNavigated', (params, eventSessionId) => {
|
|
1697
|
+
if (!this.isCurrentSessionEvent(eventSessionId)) {
|
|
1698
|
+
return;
|
|
1699
|
+
}
|
|
1700
|
+
const frame = params.frame;
|
|
1701
|
+
if (frame?.id && !frame.parentId) {
|
|
1702
|
+
this.mainFrameId = frame.id;
|
|
1703
|
+
}
|
|
1704
|
+
});
|
|
1622
1705
|
// 控制台日志
|
|
1623
|
-
this.cdp.onEvent('Runtime.consoleAPICalled', (params) => {
|
|
1706
|
+
this.cdp.onEvent('Runtime.consoleAPICalled', (params, eventSessionId) => {
|
|
1707
|
+
if (!this.isCurrentSessionEvent(eventSessionId)) {
|
|
1708
|
+
return;
|
|
1709
|
+
}
|
|
1624
1710
|
const p = params;
|
|
1625
1711
|
this.consoleLogs.push({
|
|
1712
|
+
sequence: ++this.nextConsoleLogSequence,
|
|
1626
1713
|
level: p.type,
|
|
1627
1714
|
text: p.args.map((a) => a.value ?? a.description ?? '').join(' '),
|
|
1628
1715
|
timestamp: Math.round(p.timestamp), // Runtime.Timestamp 已是 epoch 毫秒
|
|
@@ -1635,24 +1722,38 @@ class SessionManager {
|
|
|
1635
1722
|
}
|
|
1636
1723
|
});
|
|
1637
1724
|
// 网络请求
|
|
1638
|
-
this.cdp.onEvent('Network.requestWillBeSent', (params) => {
|
|
1725
|
+
this.cdp.onEvent('Network.requestWillBeSent', (params, eventSessionId) => {
|
|
1726
|
+
if (!this.isCurrentSessionEvent(eventSessionId)) {
|
|
1727
|
+
return;
|
|
1728
|
+
}
|
|
1639
1729
|
const p = params;
|
|
1640
1730
|
this.requestMap.set(p.requestId, {
|
|
1641
1731
|
url: p.request.url,
|
|
1642
1732
|
method: p.request.method,
|
|
1643
1733
|
type: p.type,
|
|
1644
1734
|
timestamp: Math.round(p.wallTime * 1000), // wallTime 是 epoch 秒 → epoch 毫秒
|
|
1735
|
+
frameId: p.frameId,
|
|
1645
1736
|
_monotonic: p.timestamp, // MonotonicTime 用于 duration 计算
|
|
1646
1737
|
});
|
|
1647
1738
|
});
|
|
1648
|
-
this.cdp.onEvent('Network.responseReceived', (params) => {
|
|
1739
|
+
this.cdp.onEvent('Network.responseReceived', (params, eventSessionId) => {
|
|
1740
|
+
if (!this.isCurrentSessionEvent(eventSessionId)) {
|
|
1741
|
+
return;
|
|
1742
|
+
}
|
|
1649
1743
|
const p = params;
|
|
1650
1744
|
const request = this.requestMap.get(p.requestId);
|
|
1651
1745
|
if (request) {
|
|
1652
|
-
const { _monotonic, ...requestData } = request;
|
|
1746
|
+
const { _monotonic, frameId, ...requestData } = request;
|
|
1747
|
+
const challenge = isCloudflareChallenge(p.response.headers);
|
|
1748
|
+
if (requestData.type.toLowerCase() === 'document' && frameId === this.mainFrameId) {
|
|
1749
|
+
// 仅新的主文档响应可以解除已知 Challenge,清空日志不能解除该状态
|
|
1750
|
+
this.challengeRequired = challenge;
|
|
1751
|
+
}
|
|
1653
1752
|
this.networkRequests.push({
|
|
1753
|
+
sequence: ++this.nextNetworkRequestSequence,
|
|
1654
1754
|
...requestData,
|
|
1655
1755
|
status: p.response.status,
|
|
1756
|
+
challenge,
|
|
1656
1757
|
duration: Math.round((p.timestamp - _monotonic) * 1000),
|
|
1657
1758
|
});
|
|
1658
1759
|
// 环形缓冲区:批量裁剪到 800 条,均摊 O(n) 开销
|
|
@@ -1663,12 +1764,17 @@ class SessionManager {
|
|
|
1663
1764
|
}
|
|
1664
1765
|
});
|
|
1665
1766
|
// 网络请求失败时写入日志再清理 requestMap,防止 diagnostics 丢失失败请求
|
|
1666
|
-
this.cdp.onEvent('Network.loadingFailed', (params) => {
|
|
1767
|
+
this.cdp.onEvent('Network.loadingFailed', (params, eventSessionId) => {
|
|
1768
|
+
if (!this.isCurrentSessionEvent(eventSessionId)) {
|
|
1769
|
+
return;
|
|
1770
|
+
}
|
|
1667
1771
|
const p = params;
|
|
1668
1772
|
const request = this.requestMap.get(p.requestId);
|
|
1669
1773
|
if (request) {
|
|
1670
1774
|
const { _monotonic, ...requestData } = request;
|
|
1775
|
+
delete requestData.frameId;
|
|
1671
1776
|
this.networkRequests.push({
|
|
1777
|
+
sequence: ++this.nextNetworkRequestSequence,
|
|
1672
1778
|
...requestData,
|
|
1673
1779
|
type: p.type ?? requestData.type,
|
|
1674
1780
|
errorText: p.errorText,
|
|
@@ -1685,16 +1791,15 @@ class SessionManager {
|
|
|
1685
1791
|
* 更新页面状态
|
|
1686
1792
|
*/
|
|
1687
1793
|
async updateState() {
|
|
1688
|
-
|
|
1689
|
-
|
|
1690
|
-
|
|
1691
|
-
|
|
1692
|
-
|
|
1693
|
-
|
|
1694
|
-
|
|
1695
|
-
|
|
1696
|
-
|
|
1697
|
-
};
|
|
1794
|
+
await this.getLiveState();
|
|
1795
|
+
}
|
|
1796
|
+
async ensureRuntimeEnabled() {
|
|
1797
|
+
if (this.runtimeEnabled) {
|
|
1798
|
+
return;
|
|
1799
|
+
}
|
|
1800
|
+
this.ensureSession();
|
|
1801
|
+
await this.send('Runtime.enable');
|
|
1802
|
+
this.runtimeEnabled = true;
|
|
1698
1803
|
}
|
|
1699
1804
|
/**
|
|
1700
1805
|
* 确保已连接
|