@pyrokine/mcp-chrome 3.0.0 → 3.1.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/README.md +721 -303
- package/README_zh.md +750 -292
- package/dist/cdp/client.d.ts +7 -3
- package/dist/cdp/client.d.ts.map +1 -1
- package/dist/cdp/client.js +252 -63
- package/dist/cdp/client.js.map +1 -1
- package/dist/cdp/launcher.d.ts +1 -1
- package/dist/cdp/launcher.d.ts.map +1 -1
- package/dist/cdp/launcher.js +33 -12
- package/dist/cdp/launcher.js.map +1 -1
- package/dist/core/auto-wait.d.ts.map +1 -1
- package/dist/core/auto-wait.js +88 -52
- package/dist/core/auto-wait.js.map +1 -1
- package/dist/core/browser-driver.d.ts +76 -18
- package/dist/core/browser-driver.d.ts.map +1 -1
- package/dist/core/browser-driver.js.map +1 -1
- package/dist/core/browser-event-schema.d.ts +287 -0
- package/dist/core/browser-event-schema.d.ts.map +1 -0
- package/dist/core/browser-event-schema.js +104 -0
- package/dist/core/browser-event-schema.js.map +1 -0
- package/dist/core/browser-events.d.ts +99 -0
- package/dist/core/browser-events.d.ts.map +1 -0
- package/dist/core/browser-events.js +1312 -0
- package/dist/core/browser-events.js.map +1 -0
- package/dist/core/challenge-page.d.ts +39 -0
- package/dist/core/challenge-page.d.ts.map +1 -0
- package/dist/core/challenge-page.js +226 -0
- package/dist/core/challenge-page.js.map +1 -0
- package/dist/core/dom-helpers.d.ts +28 -0
- package/dist/core/dom-helpers.d.ts.map +1 -0
- package/dist/core/dom-helpers.js +311 -0
- package/dist/core/dom-helpers.js.map +1 -0
- package/dist/core/errors.d.ts +10 -4
- package/dist/core/errors.d.ts.map +1 -1
- package/dist/core/errors.js +63 -13
- package/dist/core/errors.js.map +1 -1
- package/dist/core/file-upload.d.ts +22 -0
- package/dist/core/file-upload.d.ts.map +1 -0
- package/dist/core/file-upload.js +194 -0
- package/dist/core/file-upload.js.map +1 -0
- package/dist/core/locator.d.ts +17 -2
- package/dist/core/locator.d.ts.map +1 -1
- package/dist/core/locator.js +156 -212
- package/dist/core/locator.js.map +1 -1
- package/dist/core/os-mouse.d.ts +11 -0
- package/dist/core/os-mouse.d.ts.map +1 -1
- package/dist/core/os-mouse.js +102 -29
- package/dist/core/os-mouse.js.map +1 -1
- package/dist/core/retry.d.ts.map +1 -1
- package/dist/core/retry.js +4 -1
- package/dist/core/retry.js.map +1 -1
- package/dist/core/session.d.ts +65 -14
- package/dist/core/session.d.ts.map +1 -1
- package/dist/core/session.js +1089 -273
- package/dist/core/session.js.map +1 -1
- package/dist/core/types.d.ts +5 -1
- package/dist/core/types.d.ts.map +1 -1
- package/dist/core/types.js +16 -1
- package/dist/core/types.js.map +1 -1
- package/dist/core/unified-session.d.ts +50 -41
- package/dist/core/unified-session.d.ts.map +1 -1
- package/dist/core/unified-session.js +938 -285
- package/dist/core/unified-session.js.map +1 -1
- package/dist/core/utils.d.ts +5 -4
- package/dist/core/utils.d.ts.map +1 -1
- package/dist/core/utils.js +73 -18
- package/dist/core/utils.js.map +1 -1
- package/dist/extension/bridge.d.ts +50 -19
- package/dist/extension/bridge.d.ts.map +1 -1
- package/dist/extension/bridge.js +289 -57
- package/dist/extension/bridge.js.map +1 -1
- package/dist/extension/http-server.d.ts +7 -2
- package/dist/extension/http-server.d.ts.map +1 -1
- package/dist/extension/http-server.js +219 -48
- package/dist/extension/http-server.js.map +1 -1
- package/dist/index.js +14 -1
- package/dist/index.js.map +1 -1
- package/dist/tools/browse.d.ts.map +1 -1
- package/dist/tools/browse.js +66 -32
- package/dist/tools/browse.js.map +1 -1
- package/dist/tools/browser-event-output.d.ts +7 -0
- package/dist/tools/browser-event-output.d.ts.map +1 -0
- package/dist/tools/browser-event-output.js +19 -0
- package/dist/tools/browser-event-output.js.map +1 -0
- package/dist/tools/challenge.d.ts +17 -5
- package/dist/tools/challenge.d.ts.map +1 -1
- package/dist/tools/challenge.js +113 -195
- package/dist/tools/challenge.js.map +1 -1
- package/dist/tools/cookies.d.ts.map +1 -1
- package/dist/tools/cookies.js +33 -8
- package/dist/tools/cookies.js.map +1 -1
- package/dist/tools/diagnostics.d.ts.map +1 -1
- package/dist/tools/diagnostics.js +14 -0
- package/dist/tools/diagnostics.js.map +1 -1
- package/dist/tools/evaluate.d.ts +6 -5
- package/dist/tools/evaluate.d.ts.map +1 -1
- package/dist/tools/evaluate.js +102 -52
- package/dist/tools/evaluate.js.map +1 -1
- package/dist/tools/extract.d.ts.map +1 -1
- package/dist/tools/extract.js +178 -124
- package/dist/tools/extract.js.map +1 -1
- package/dist/tools/input.d.ts.map +1 -1
- package/dist/tools/input.js +623 -271
- package/dist/tools/input.js.map +1 -1
- package/dist/tools/logs.d.ts.map +1 -1
- package/dist/tools/logs.js +50 -11
- package/dist/tools/logs.js.map +1 -1
- package/dist/tools/manage.d.ts.map +1 -1
- package/dist/tools/manage.js +127 -46
- package/dist/tools/manage.js.map +1 -1
- package/dist/tools/network-sanitizer.d.ts +1 -0
- package/dist/tools/network-sanitizer.d.ts.map +1 -1
- package/dist/tools/network-sanitizer.js +30 -1
- package/dist/tools/network-sanitizer.js.map +1 -1
- package/dist/tools/page-context.d.ts +4 -3
- package/dist/tools/page-context.d.ts.map +1 -1
- package/dist/tools/page-context.js +56 -14
- package/dist/tools/page-context.js.map +1 -1
- package/dist/tools/post-condition.d.ts +1 -1
- package/dist/tools/post-condition.d.ts.map +1 -1
- package/dist/tools/post-condition.js +153 -47
- package/dist/tools/post-condition.js.map +1 -1
- package/dist/tools/schema.d.ts +7 -4
- package/dist/tools/schema.d.ts.map +1 -1
- package/dist/tools/schema.js +32 -122
- package/dist/tools/schema.js.map +1 -1
- package/dist/tools/target-diagnostics.d.ts +10 -2
- package/dist/tools/target-diagnostics.d.ts.map +1 -1
- package/dist/tools/target-diagnostics.js +74 -21
- package/dist/tools/target-diagnostics.js.map +1 -1
- package/dist/tools/wait.d.ts.map +1 -1
- package/dist/tools/wait.js +108 -42
- package/dist/tools/wait.js.map +1 -1
- package/extension/dist/assets/{content.ts-C4imZ6A9.js → content.ts-BAMOcF3R.js} +1 -1
- package/extension/dist/assets/{content.ts-loader-CnJuCGhl.js → content.ts-loader-4F9a1EWZ.js} +1 -1
- package/extension/dist/assets/index.ts-B79g2ikq.js +19820 -0
- package/extension/dist/assets/{page-scripts-CMIY1zhd.js → page-scripts-CSxn58h1.js} +300 -410
- package/extension/dist/assets/{popup.html-CgtWL6Py.js → popup.html-DwwHo00N.js} +8 -6
- package/extension/dist/manifest.json +4 -4
- package/extension/dist/service-worker-loader.js +1 -1
- package/extension/dist/src/popup/popup.html +3 -3
- package/package.json +15 -7
- package/scripts/start-chrome.ps1 +9 -1
- package/extension/dist/assets/index.ts-GeHNZBml.js +0 -8972
|
@@ -6,15 +6,18 @@
|
|
|
6
6
|
* 2. CDP 模式:通过 Chrome DevTools Protocol 操作(Fallback)
|
|
7
7
|
*/
|
|
8
8
|
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
9
|
+
import { randomUUID } from 'node:crypto';
|
|
10
|
+
import { eventRefTarget } from './browser-events.js';
|
|
11
|
+
import { setTimeout as delay } from 'node:timers/promises';
|
|
12
|
+
import { Script } from 'node:vm';
|
|
9
13
|
import { ExtensionBridge } from '../extension/index.js';
|
|
10
14
|
import { DriverCapabilityError, } from './browser-driver.js';
|
|
11
|
-
import { OsMouseError, PagePreconditionError, setErrorPageContext, StructuredToolError, TargetNotFoundError, TimeoutError, } from './errors.js';
|
|
15
|
+
import { isNativeExecutionResultUnknown, getErrorSource, getErrorPageContext, OsMouseError, PagePreconditionError, setErrorPageContext, setErrorSource, StructuredToolError, TargetNotFoundError, TimeoutError, } from './errors.js';
|
|
12
16
|
import { isExtensionDisconnected } from './extension-errors.js';
|
|
13
17
|
import { asViewportMetrics, osLeftClick, resolveTurnstileOsClickPoint, } from './os-mouse.js';
|
|
14
18
|
import { getKeyDefinition, getSession as getCdpSession } from './session.js';
|
|
15
|
-
import { DEFAULT_TIMEOUT } from './types.js';
|
|
19
|
+
import { DEFAULT_TIMEOUT, EvaluateResultTooLargeError, extractCdpValue, formatCdpException, isKnownNonSerializableRemoteObject, MODIFIER_KEYS, NonSerializableEvaluateResultError, } from './types.js';
|
|
16
20
|
import { remainingNavigationTimeout, waitForNavigationQueue, withNavigationBudget } from './utils.js';
|
|
17
|
-
import { EvaluateResultTooLargeError, extractCdpValue, formatCdpException, isKnownNonSerializableRemoteObject, MODIFIER_KEYS, NonSerializableEvaluateResultError, } from './types.js';
|
|
18
21
|
class UnifiedSessionManager {
|
|
19
22
|
static instance;
|
|
20
23
|
static CONNECTION_COOLDOWN = 30000; // 连接失败后 30 秒内不重试
|
|
@@ -25,6 +28,7 @@ class UnifiedSessionManager {
|
|
|
25
28
|
extensionBridge = null;
|
|
26
29
|
inputMode = 'precise'; // 默认使用 precise 模式,可绕过 CSP 限制
|
|
27
30
|
currentMousePosition = { x: 0, y: 0 }; // 跟踪鼠标位置
|
|
31
|
+
mouseFrameContext;
|
|
28
32
|
/** 当前按下的修饰键位掩码 */
|
|
29
33
|
modifiers = 0;
|
|
30
34
|
/** 当前按下的所有键(用于 Puppeteer 风格的 rawKeyDown/autoRepeat 长按重复) */
|
|
@@ -40,7 +44,9 @@ class UnifiedSessionManager {
|
|
|
40
44
|
currentFrameOffset = null; // iframe 在主页面的偏移量(withFrame 期间有效)
|
|
41
45
|
lastEvaluationMetadata;
|
|
42
46
|
pageMetadata = new Map();
|
|
43
|
-
constructor() {
|
|
47
|
+
constructor() {
|
|
48
|
+
getCdpSession().requestContext = () => this.tabLockContext.getStore();
|
|
49
|
+
}
|
|
44
50
|
static getInstance() {
|
|
45
51
|
if (!UnifiedSessionManager.instance) {
|
|
46
52
|
UnifiedSessionManager.instance = new UnifiedSessionManager();
|
|
@@ -54,7 +60,7 @@ class UnifiedSessionManager {
|
|
|
54
60
|
if (this.extensionBridge) {
|
|
55
61
|
return; // 已经启动
|
|
56
62
|
}
|
|
57
|
-
this.extensionBridge = new ExtensionBridge();
|
|
63
|
+
this.extensionBridge = new ExtensionBridge({ requestContext: () => this.tabLockContext.getStore() });
|
|
58
64
|
try {
|
|
59
65
|
await this.extensionBridge.start();
|
|
60
66
|
console.error(`[MCP] Extension HTTP server listening on port ${this.extensionBridge.getPort()}`);
|
|
@@ -68,6 +74,10 @@ class UnifiedSessionManager {
|
|
|
68
74
|
* 获取当前连接模式
|
|
69
75
|
*/
|
|
70
76
|
getMode() {
|
|
77
|
+
const scopedMode = this.tabLockContext.getStore()?.mode;
|
|
78
|
+
if (scopedMode) {
|
|
79
|
+
return scopedMode;
|
|
80
|
+
}
|
|
71
81
|
if (this.activeMode === 'extension' && this.extensionBridge?.isConnected()) {
|
|
72
82
|
return 'extension';
|
|
73
83
|
}
|
|
@@ -86,9 +96,11 @@ class UnifiedSessionManager {
|
|
|
86
96
|
this.activeMode = null;
|
|
87
97
|
return 'none';
|
|
88
98
|
}
|
|
89
|
-
setActiveMode(mode, cdpExplicitlySelected = false) {
|
|
90
|
-
this.
|
|
91
|
-
|
|
99
|
+
async setActiveMode(mode, cdpExplicitlySelected = false) {
|
|
100
|
+
await this.withTabLock(async () => {
|
|
101
|
+
this.activeMode = mode;
|
|
102
|
+
this.cdpExplicitlySelected = mode === 'cdp' && cdpExplicitlySelected;
|
|
103
|
+
});
|
|
92
104
|
}
|
|
93
105
|
/**
|
|
94
106
|
* 获取当前输入模式
|
|
@@ -108,6 +120,7 @@ class UnifiedSessionManager {
|
|
|
108
120
|
}
|
|
109
121
|
getPageMetadata(targetId) {
|
|
110
122
|
return (this.pageMetadata.get(this.metadataKey(targetId)) ?? {
|
|
123
|
+
origin: 'unknown',
|
|
111
124
|
createdBy: 'unknown',
|
|
112
125
|
createdAt: null,
|
|
113
126
|
lastUsedAt: null,
|
|
@@ -115,6 +128,15 @@ class UnifiedSessionManager {
|
|
|
115
128
|
}
|
|
116
129
|
async setPageMetadata(targetId, updates) {
|
|
117
130
|
await this.getPageSnapshot(targetId);
|
|
131
|
+
if (this.getMode() === 'extension') {
|
|
132
|
+
const driver = await this.getDriver();
|
|
133
|
+
if (!driver.setPageMetadata) {
|
|
134
|
+
throw new DriverCapabilityError('Extension 不支持持久页面备注,请成套更新 Server 和 Extension');
|
|
135
|
+
}
|
|
136
|
+
const metadata = await driver.setPageMetadata(targetId, updates);
|
|
137
|
+
this.pageMetadata.set(this.metadataKey(targetId), metadata);
|
|
138
|
+
return metadata;
|
|
139
|
+
}
|
|
118
140
|
const metadata = { ...this.getPageMetadata(targetId) };
|
|
119
141
|
if (updates.taskHint !== undefined) {
|
|
120
142
|
if (updates.taskHint === null) {
|
|
@@ -184,7 +206,19 @@ class UnifiedSessionManager {
|
|
|
184
206
|
* 启动浏览器(CDP 模式)或等待 Extension 连接
|
|
185
207
|
*/
|
|
186
208
|
async launch(options = {}) {
|
|
187
|
-
|
|
209
|
+
this.assertRequestActive();
|
|
210
|
+
const explicitProcess = [
|
|
211
|
+
options.port,
|
|
212
|
+
options.executablePath,
|
|
213
|
+
options.headless,
|
|
214
|
+
options.userDataDir,
|
|
215
|
+
options.incognito,
|
|
216
|
+
].some((value) => value !== undefined);
|
|
217
|
+
if (explicitProcess && getCdpSession().isConnected()) {
|
|
218
|
+
throw new DriverCapabilityError('显式启动参数要求新的独立浏览器;当前已有 CDP 连接,未关闭或替换现有浏览器');
|
|
219
|
+
}
|
|
220
|
+
const extensionReady = explicitProcess || this.cdpExplicitlySelected ? false : await this.ensureExtensionConnected(options.timeout);
|
|
221
|
+
this.assertRequestActive();
|
|
188
222
|
// 优先检查 Extension 是否已连接,如果断开则等待重连(受 timeout 约束)
|
|
189
223
|
if (extensionReady) {
|
|
190
224
|
// newPage 会设置 currentTabId,需要加锁
|
|
@@ -194,6 +228,9 @@ class UnifiedSessionManager {
|
|
|
194
228
|
this.activeMode = 'extension';
|
|
195
229
|
this.cdpExplicitlySelected = false;
|
|
196
230
|
this.recordPageUse(result.targetId, true);
|
|
231
|
+
if (result.metadata) {
|
|
232
|
+
this.rememberPageMetadata(result.targetId, result.metadata);
|
|
233
|
+
}
|
|
197
234
|
return {
|
|
198
235
|
targetId: result.targetId,
|
|
199
236
|
type: result.type ?? 'page',
|
|
@@ -206,7 +243,7 @@ class UnifiedSessionManager {
|
|
|
206
243
|
// Fallback 到 CDP 模式
|
|
207
244
|
const target = await getCdpSession().launch(options);
|
|
208
245
|
this.activeMode = 'cdp';
|
|
209
|
-
this.cdpExplicitlySelected =
|
|
246
|
+
this.cdpExplicitlySelected = explicitProcess;
|
|
210
247
|
this.recordPageUse(target.targetId, target.targetCreated ?? target.reused !== true);
|
|
211
248
|
return {
|
|
212
249
|
...target,
|
|
@@ -216,20 +253,25 @@ class UnifiedSessionManager {
|
|
|
216
253
|
/**
|
|
217
254
|
* 列出所有页面
|
|
218
255
|
*/
|
|
219
|
-
async listTargets(timeout) {
|
|
256
|
+
async listTargets(timeout, options) {
|
|
220
257
|
const mode = this.getMode();
|
|
221
258
|
if (mode === 'none') {
|
|
222
259
|
return [];
|
|
223
260
|
}
|
|
224
261
|
const deadline = timeout === undefined ? undefined : Date.now() + timeout;
|
|
225
262
|
const driver = await this.getDriver(timeout);
|
|
226
|
-
const targets = await driver.listTargets(deadline === undefined ? undefined : remainingNavigationTimeout(deadline));
|
|
263
|
+
const targets = await driver.listTargets(deadline === undefined ? undefined : remainingNavigationTimeout(deadline), options);
|
|
227
264
|
const liveKeys = new Set(targets.map((t) => `${mode}:${t.targetId ?? String(t.id)}`));
|
|
228
265
|
for (const key of this.pageMetadata.keys()) {
|
|
229
266
|
if (key.startsWith(`${mode}:`) && !liveKeys.has(key)) {
|
|
230
267
|
this.pageMetadata.delete(key);
|
|
231
268
|
}
|
|
232
269
|
}
|
|
270
|
+
for (const target of targets) {
|
|
271
|
+
if (target.metadata) {
|
|
272
|
+
this.rememberPageMetadata(target.targetId ?? String(target.id), target.metadata);
|
|
273
|
+
}
|
|
274
|
+
}
|
|
233
275
|
return targets.map(({ id, ...target }) => ({
|
|
234
276
|
...target,
|
|
235
277
|
targetId: target.targetId ?? String(id),
|
|
@@ -239,7 +281,7 @@ class UnifiedSessionManager {
|
|
|
239
281
|
metadata: this.getPageMetadata(target.targetId ?? String(id)),
|
|
240
282
|
}));
|
|
241
283
|
}
|
|
242
|
-
async listTopology(timeout) {
|
|
284
|
+
async listTopology(timeout, options) {
|
|
243
285
|
const mode = this.getMode();
|
|
244
286
|
if (mode === 'none') {
|
|
245
287
|
return null;
|
|
@@ -249,7 +291,13 @@ class UnifiedSessionManager {
|
|
|
249
291
|
if (!driver.listWindows) {
|
|
250
292
|
return null;
|
|
251
293
|
}
|
|
252
|
-
|
|
294
|
+
const topology = await driver.listWindows(deadline === undefined ? undefined : remainingNavigationTimeout(deadline), options);
|
|
295
|
+
for (const tab of topology.windows.flatMap((window) => window.tabs)) {
|
|
296
|
+
if (tab.metadata) {
|
|
297
|
+
this.rememberPageMetadata(tab.targetId ?? String(tab.id), tab.metadata);
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
return topology;
|
|
253
301
|
}
|
|
254
302
|
async setReadAccess(targetId, allow) {
|
|
255
303
|
const driver = await this.getDriver();
|
|
@@ -330,11 +378,10 @@ class UnifiedSessionManager {
|
|
|
330
378
|
}
|
|
331
379
|
async activatePageWithAffected(targetId) {
|
|
332
380
|
const driver = await this.getDriver();
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
throw new DriverCapabilityError('activatePage 受控 affected 返回仅支持 Extension 模式');
|
|
381
|
+
if (!driver.activatePageWithAffected) {
|
|
382
|
+
throw new DriverCapabilityError('当前浏览器驱动不支持激活页面状态返回');
|
|
336
383
|
}
|
|
337
|
-
return this.withTabLock(() =>
|
|
384
|
+
return this.withTabLock(() => driver.activatePageWithAffected(targetId));
|
|
338
385
|
}
|
|
339
386
|
async focusWindow(windowId) {
|
|
340
387
|
const driver = await this.getDriver();
|
|
@@ -435,48 +482,73 @@ class UnifiedSessionManager {
|
|
|
435
482
|
*/
|
|
436
483
|
async click(refId) {
|
|
437
484
|
const driver = await this.getDriver();
|
|
438
|
-
await driver.click(refId);
|
|
485
|
+
await driver.click(refId, undefined, this.modifiers);
|
|
439
486
|
}
|
|
440
487
|
/**
|
|
441
488
|
* 带 actionability 检查的点击(Extension 模式)
|
|
442
489
|
*
|
|
443
490
|
* 返回结构化结果,让调用方知道操作是否真正生效
|
|
444
491
|
*/
|
|
445
|
-
async actionableClick(refId, force = false, button = 'left', clickCount = 1) {
|
|
492
|
+
async actionableClick(refId, force = false, button = 'left', clickCount = 1, point) {
|
|
446
493
|
const driver = await this.getDriver();
|
|
447
|
-
if (
|
|
448
|
-
|
|
494
|
+
if (point) {
|
|
495
|
+
this.currentMousePosition = { x: point.x, y: point.y };
|
|
496
|
+
this.rememberMouseFrame();
|
|
497
|
+
}
|
|
498
|
+
if (this.inputMode === 'stealth' || force) {
|
|
499
|
+
if (button === 'left' && clickCount === 1) {
|
|
500
|
+
return driver.actionableClick(refId, force, undefined, this.modifiers);
|
|
501
|
+
}
|
|
449
502
|
}
|
|
450
503
|
if (!force) {
|
|
451
504
|
const result = await driver.checkActionability(refId);
|
|
452
|
-
if (!result.success)
|
|
505
|
+
if (!result.success) {
|
|
453
506
|
return result;
|
|
507
|
+
}
|
|
508
|
+
if (result.clickPoint) {
|
|
509
|
+
this.currentMousePosition = { ...result.clickPoint };
|
|
510
|
+
this.rememberMouseFrame();
|
|
511
|
+
}
|
|
454
512
|
}
|
|
455
513
|
if (force) {
|
|
456
514
|
const { x, y } = this.currentMousePosition;
|
|
457
|
-
await driver.stealthClick(x, y, button, clickCount, refId);
|
|
515
|
+
await driver.stealthClick(x, y, button, clickCount, refId, undefined, this.modifiers);
|
|
458
516
|
}
|
|
459
517
|
else {
|
|
460
|
-
|
|
518
|
+
try {
|
|
519
|
+
await this.mouseClick(button, clickCount, refId);
|
|
520
|
+
}
|
|
521
|
+
catch (error) {
|
|
522
|
+
const code = error?.code;
|
|
523
|
+
if (code !== 'NATIVE_INPUT_UNAVAILABLE' ||
|
|
524
|
+
!this.isExtensionConnected() ||
|
|
525
|
+
button !== 'left' ||
|
|
526
|
+
clickCount !== 1 ||
|
|
527
|
+
this.modifiers !== 0) {
|
|
528
|
+
throw error;
|
|
529
|
+
}
|
|
530
|
+
const result = await driver.actionableClick(refId, false, undefined, this.modifiers);
|
|
531
|
+
return { ...result, warning: '后台元素单击使用合成事件,isTrusted=false,未激活标签页' };
|
|
532
|
+
}
|
|
461
533
|
}
|
|
462
534
|
return { success: true };
|
|
463
535
|
}
|
|
464
536
|
/**
|
|
465
|
-
* dispatch 模式输入(
|
|
537
|
+
* dispatch 模式输入(Extension MAIN 世界,兼容 React/Vue 受控组件)
|
|
466
538
|
*/
|
|
467
539
|
async dispatchInput(refId, text) {
|
|
468
540
|
const driver = await this.getDriver();
|
|
469
541
|
return driver.dispatchInput(refId, text);
|
|
470
542
|
}
|
|
471
543
|
/**
|
|
472
|
-
* HTML5 drag/drop(
|
|
544
|
+
* HTML5 drag/drop(Extension MAIN 世界,与 refId 创建侧共享 __mcpElementMap)
|
|
473
545
|
*/
|
|
474
546
|
async dragAndDrop(srcRefId, dstRefId) {
|
|
475
547
|
const driver = await this.getDriver();
|
|
476
548
|
return driver.dragAndDrop(srcRefId, dstRefId);
|
|
477
549
|
}
|
|
478
550
|
/**
|
|
479
|
-
* 获取元素 computed style(ISOLATED
|
|
551
|
+
* 获取元素 computed style(Extension 受控页为 MAIN,只读 documentId 授权为 ISOLATED)
|
|
480
552
|
*/
|
|
481
553
|
async getComputedStyle(refId, prop) {
|
|
482
554
|
const driver = await this.getDriver();
|
|
@@ -516,7 +588,7 @@ class UnifiedSessionManager {
|
|
|
516
588
|
* @param _retried 内部重试标记,外部不应传入
|
|
517
589
|
*/
|
|
518
590
|
async evaluate(code, mode, timeout, args, options = {}, _retried = false) {
|
|
519
|
-
|
|
591
|
+
this.assertRequestActive();
|
|
520
592
|
this.lastEvaluationMetadata = undefined;
|
|
521
593
|
const effectiveMode = mode ?? this.inputMode;
|
|
522
594
|
const hasArgs = args !== undefined;
|
|
@@ -527,53 +599,34 @@ class UnifiedSessionManager {
|
|
|
527
599
|
expression = `(${code})(${argsStr})`;
|
|
528
600
|
}
|
|
529
601
|
const cdpScript = hasArgs ? code : expression;
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
if (!this.isExtensionConnected()) {
|
|
534
|
-
this.assertCdpFallbackAllowed();
|
|
535
|
-
return this.evaluateViaCdp(cdpScript, args, timeout);
|
|
536
|
-
}
|
|
602
|
+
if (!_retried && !hasArgs && /\breturn\b/.test(code)) {
|
|
603
|
+
try {
|
|
604
|
+
new Script(code);
|
|
537
605
|
}
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
return this.evaluateViaCdp(cdpScript, args, timeout);
|
|
606
|
+
catch (error) {
|
|
607
|
+
if (error instanceof SyntaxError && /Illegal return statement/.test(error.message)) {
|
|
608
|
+
return this.evaluate(`(() => { ${code} })()`, mode, timeout, undefined, options, true);
|
|
542
609
|
}
|
|
543
610
|
}
|
|
544
|
-
// Extension 路径
|
|
545
|
-
if (effectiveMode === 'precise') {
|
|
546
|
-
return this.evaluateViaExtensionPrecise(code, expression, args, timeout, options);
|
|
547
|
-
}
|
|
548
|
-
return this.evaluateViaExtensionStealth(expression, timeout);
|
|
549
611
|
}
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
if (!_retried && (isExtensionDisconnected(err) || msg.includes('not connected'))) {
|
|
556
|
-
if (remainingTimeout !== undefined && remainingTimeout <= 0) {
|
|
557
|
-
throw err;
|
|
558
|
-
}
|
|
559
|
-
await new Promise((r) => setTimeout(r, 2000));
|
|
560
|
-
if (this.isExtensionConnected()) {
|
|
561
|
-
return this.evaluate(code, mode, remainingTimeout, args, options, true);
|
|
562
|
-
}
|
|
563
|
-
// Extension 重连失败,尝试 CDP fallback
|
|
564
|
-
if (!this.requireExtension) {
|
|
565
|
-
return this.evaluateViaCdp(cdpScript, args, remainingTimeout);
|
|
566
|
-
}
|
|
612
|
+
if (timeout !== undefined) {
|
|
613
|
+
// 轮询上下文:快速失败,端到端预算受控
|
|
614
|
+
if (!this.isExtensionConnected()) {
|
|
615
|
+
this.assertCdpFallbackAllowed();
|
|
616
|
+
return await this.evaluateViaCdp(cdpScript, args, timeout, options);
|
|
567
617
|
}
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
}
|
|
618
|
+
}
|
|
619
|
+
else {
|
|
620
|
+
// 非轮询上下文:允许等待重连;连接失败时回退 CDP
|
|
621
|
+
if (!(await this.ensureExtensionConnected())) {
|
|
622
|
+
return await this.evaluateViaCdp(cdpScript, args, timeout, options);
|
|
574
623
|
}
|
|
575
|
-
throw err;
|
|
576
624
|
}
|
|
625
|
+
// Extension 路径
|
|
626
|
+
if (effectiveMode === 'precise') {
|
|
627
|
+
return await this.evaluateViaExtensionPrecise(code, expression, args, timeout, options);
|
|
628
|
+
}
|
|
629
|
+
return await this.evaluateViaExtensionStealth(expression, timeout, options.userAction);
|
|
577
630
|
}
|
|
578
631
|
/**
|
|
579
632
|
* 获取页面文本
|
|
@@ -669,22 +722,26 @@ class UnifiedSessionManager {
|
|
|
669
722
|
* @param text 文本内容
|
|
670
723
|
* @param xpath XPath 表达式
|
|
671
724
|
* @param timeout 端到端预算(毫秒),包含连接等待和请求超时,传给 bridge.find → sendCommand
|
|
725
|
+
* @param semanticOrMatchIndex 语义名称条件,或兼容旧调用的匹配序号
|
|
726
|
+
* @param matchIndex 匹配序号,从 0 开始,指定时只返回该位置
|
|
672
727
|
*/
|
|
673
|
-
async find(selector, text, xpath, timeout, matchIndex) {
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
728
|
+
async find(selector, text, xpath, timeout, semanticOrMatchIndex, matchIndex) {
|
|
729
|
+
const semantic = typeof semanticOrMatchIndex === 'object' ? semanticOrMatchIndex : undefined;
|
|
730
|
+
matchIndex = typeof semanticOrMatchIndex === 'number' ? semanticOrMatchIndex : matchIndex;
|
|
731
|
+
this.assertRequestActive();
|
|
677
732
|
if (timeout !== undefined) {
|
|
678
733
|
// 轮询上下文:快速失败,端到端预算受控
|
|
679
734
|
if (this.isExtensionConnected()) {
|
|
680
|
-
return this.extensionBridge.find(selector, text, xpath, timeout);
|
|
735
|
+
return this.extensionBridge.find(selector, text, xpath, timeout, semantic, matchIndex);
|
|
681
736
|
}
|
|
682
737
|
this.assertCdpFallbackAllowed();
|
|
683
|
-
return getCdpSession().find(selector, text, xpath, timeout);
|
|
738
|
+
return getCdpSession().find(selector, text, xpath, timeout, semantic, matchIndex);
|
|
684
739
|
}
|
|
685
740
|
// 非轮询上下文:允许等待重连,断连时 fallback CDP
|
|
686
741
|
const driver = await this.getDriver();
|
|
687
|
-
return this.withExtensionRetry(() => driver
|
|
742
|
+
return this.withExtensionRetry(() => driver === this.extensionBridge
|
|
743
|
+
? this.extensionBridge.find(selector, text, xpath, undefined, semantic, matchIndex)
|
|
744
|
+
: getCdpSession().find(selector, text, xpath, undefined, semantic, matchIndex), () => getCdpSession().find(selector, text, xpath, undefined, semantic, matchIndex));
|
|
688
745
|
}
|
|
689
746
|
/**
|
|
690
747
|
* 获取元素属性
|
|
@@ -710,8 +767,8 @@ class UnifiedSessionManager {
|
|
|
710
767
|
const driver = await this.getDriver();
|
|
711
768
|
await driver.setCookie(params);
|
|
712
769
|
}
|
|
713
|
-
async deleteCookie(url, name) {
|
|
714
|
-
return (await this.getDriver()).deleteCookie(url, name);
|
|
770
|
+
async deleteCookie(url, name, scope) {
|
|
771
|
+
return (await this.getDriver()).deleteCookie(url, name, scope);
|
|
715
772
|
}
|
|
716
773
|
async clearCookies(filter) {
|
|
717
774
|
if (!filter || (!filter.url && !filter.domain)) {
|
|
@@ -728,6 +785,12 @@ class UnifiedSessionManager {
|
|
|
728
785
|
const op = async () => {
|
|
729
786
|
const result = await driver.newPage(url, timeout, options);
|
|
730
787
|
this.recordPageUse(result.targetId, true);
|
|
788
|
+
if (result.metadata) {
|
|
789
|
+
this.rememberPageMetadata(result.targetId, result.metadata);
|
|
790
|
+
}
|
|
791
|
+
else if (!isExt && (options?.taskHint !== undefined || options?.note !== undefined)) {
|
|
792
|
+
await this.setPageMetadata(result.targetId, options);
|
|
793
|
+
}
|
|
731
794
|
return {
|
|
732
795
|
...result,
|
|
733
796
|
targetId: result.targetId,
|
|
@@ -794,8 +857,258 @@ class UnifiedSessionManager {
|
|
|
794
857
|
*
|
|
795
858
|
* 同一请求内的嵌套调用可重入,恢复状态只保存在当前请求的 AsyncLocalStorage 中
|
|
796
859
|
*/
|
|
797
|
-
|
|
860
|
+
assertRequestActive(scope = this.tabLockContext.getStore()) {
|
|
861
|
+
if (scope?.budgetError) {
|
|
862
|
+
throw scope.budgetError;
|
|
863
|
+
}
|
|
864
|
+
scope?.signal?.throwIfAborted();
|
|
865
|
+
if (scope?.deadline !== undefined && Date.now() >= scope.deadline) {
|
|
866
|
+
throw setErrorSource(new StructuredToolError('TIMEOUT', 'Request timeout before dispatch', '请检查页面状态后再提交操作', {
|
|
867
|
+
actionExecuted: false,
|
|
868
|
+
actionStatus: 'not_started',
|
|
869
|
+
retryable: false,
|
|
870
|
+
}), 'budget');
|
|
871
|
+
}
|
|
872
|
+
}
|
|
873
|
+
async withUserAction(kind, fn, restoreTargetAfterPreparationFailure = false) {
|
|
874
|
+
return this.withTabLock(async () => {
|
|
875
|
+
const scope = this.tabLockContext.getStore();
|
|
876
|
+
const previous = scope.userAction;
|
|
877
|
+
const action = {
|
|
878
|
+
kind,
|
|
879
|
+
dispatched: false,
|
|
880
|
+
phase: 'preparation',
|
|
881
|
+
};
|
|
882
|
+
scope.userAction = action;
|
|
883
|
+
try {
|
|
884
|
+
return await fn();
|
|
885
|
+
}
|
|
886
|
+
catch (error) {
|
|
887
|
+
if (restoreTargetAfterPreparationFailure && !action.dispatched && action.phase === 'preparation') {
|
|
888
|
+
scope.restoreTargetAfterPreparationFailure = true;
|
|
889
|
+
}
|
|
890
|
+
throw this.annotateUserActionError(error);
|
|
891
|
+
}
|
|
892
|
+
finally {
|
|
893
|
+
if (previous?.kind === kind && action.dispatched) {
|
|
894
|
+
previous.dispatched = true;
|
|
895
|
+
}
|
|
896
|
+
scope.userAction = previous;
|
|
897
|
+
}
|
|
898
|
+
});
|
|
899
|
+
}
|
|
900
|
+
annotateUserActionError(error) {
|
|
901
|
+
const scope = this.tabLockContext.getStore();
|
|
902
|
+
if (scope && error instanceof Error && getErrorSource(error) === 'budget') {
|
|
903
|
+
scope.budgetError ??= error;
|
|
904
|
+
}
|
|
905
|
+
const action = scope?.userAction;
|
|
906
|
+
const uncertain = getErrorSource(error) !== 'page' && isNativeExecutionResultUnknown(error);
|
|
907
|
+
const outputFailure = getErrorSource(error) !== 'page' &&
|
|
908
|
+
error instanceof StructuredToolError &&
|
|
909
|
+
error.code === 'NON_SERIALIZABLE_EVALUATE_RESULT' &&
|
|
910
|
+
getErrorPageContext(error)?.contextPhase === 'preparation';
|
|
911
|
+
const preparing = action &&
|
|
912
|
+
!action.dispatched &&
|
|
913
|
+
(uncertain ||
|
|
914
|
+
outputFailure ||
|
|
915
|
+
getErrorPageContext(error)?.keyboardFocusPreparation === true ||
|
|
916
|
+
getErrorPageContext(error)?.frameResolutionPreparation === true ||
|
|
917
|
+
action.phase === 'precondition' ||
|
|
918
|
+
error instanceof PagePreconditionError);
|
|
919
|
+
return setErrorPageContext(error, {
|
|
920
|
+
contextPhase: preparing
|
|
921
|
+
? error instanceof PagePreconditionError
|
|
922
|
+
? 'precondition'
|
|
923
|
+
: action.phase
|
|
924
|
+
: 'action',
|
|
925
|
+
});
|
|
926
|
+
}
|
|
927
|
+
async prepareKeyboardFocus(timeout) {
|
|
928
|
+
if (this.getMode() !== 'extension' || this.inputMode !== 'precise') {
|
|
929
|
+
return undefined;
|
|
930
|
+
}
|
|
931
|
+
const scope = this.tabLockContext.getStore();
|
|
932
|
+
if (scope.keyboardFocus) {
|
|
933
|
+
return undefined;
|
|
934
|
+
}
|
|
935
|
+
const bridge = this.extensionBridge;
|
|
936
|
+
const targetId = bridge.getCurrentTargetId();
|
|
937
|
+
if (!targetId) {
|
|
938
|
+
throw new TargetNotFoundError('没有当前原生按键目标');
|
|
939
|
+
}
|
|
940
|
+
const focus = { id: randomUUID(), targetId };
|
|
941
|
+
scope.keyboardFocus = focus;
|
|
942
|
+
try {
|
|
943
|
+
this.assertRequestActive();
|
|
944
|
+
const result = await bridge.acquireKeyboardFocus(focus.id, timeout);
|
|
945
|
+
if (result.changed) {
|
|
946
|
+
focus.warning = '原生按键期间文档临时报告 visible;操作结束后恢复原设置,实际标签页与窗口焦点保持';
|
|
947
|
+
}
|
|
948
|
+
this.assertRequestActive();
|
|
949
|
+
return focus.warning;
|
|
950
|
+
}
|
|
951
|
+
catch (error) {
|
|
952
|
+
throw setErrorPageContext(error, { keyboardFocusPreparation: true });
|
|
953
|
+
}
|
|
954
|
+
}
|
|
955
|
+
async withInputCleanup(fn) {
|
|
956
|
+
const scope = this.tabLockContext.getStore();
|
|
957
|
+
const keysBefore = new Set(this.pressedKeys);
|
|
958
|
+
const driver = this.getMode() === 'extension' ? this.extensionBridge : getCdpSession();
|
|
959
|
+
const targetId = driver?.getCurrentTargetId();
|
|
960
|
+
const frameId = driver?.getCurrentFrameId?.();
|
|
961
|
+
const documentId = this.extensionBridge === driver ? this.extensionBridge?.getCurrentFrameDocumentId?.() : undefined;
|
|
962
|
+
const keys = new Set();
|
|
963
|
+
scope.inputKeys = keys;
|
|
964
|
+
const cleanup = async (releaseUnknown = false) => {
|
|
965
|
+
if (!releaseUnknown &&
|
|
966
|
+
!scope.signal?.aborted &&
|
|
967
|
+
!scope.budgetError &&
|
|
968
|
+
(scope.deadline === undefined || Date.now() < scope.deadline)) {
|
|
969
|
+
return;
|
|
970
|
+
}
|
|
971
|
+
scope.inputCleanup = undefined;
|
|
972
|
+
if (![...keys].some((key) => !keysBefore.has(key))) {
|
|
973
|
+
return;
|
|
974
|
+
}
|
|
975
|
+
const cleanupDeadline = Date.now() + 1000;
|
|
976
|
+
try {
|
|
977
|
+
await this.tabLockContext.run({
|
|
978
|
+
...scope,
|
|
979
|
+
signal: undefined,
|
|
980
|
+
deadline: cleanupDeadline,
|
|
981
|
+
inputCleanup: undefined,
|
|
982
|
+
userAction: undefined,
|
|
983
|
+
budgetError: undefined,
|
|
984
|
+
pendingPageCondition: null,
|
|
985
|
+
}, async () => {
|
|
986
|
+
const bridge = driver === this.extensionBridge ? this.extensionBridge : null;
|
|
987
|
+
const previousTarget = driver?.getCurrentTargetId();
|
|
988
|
+
const previousFrame = driver?.getCurrentFrameId?.();
|
|
989
|
+
const previousDocument = bridge?.getCurrentFrameDocumentId?.();
|
|
990
|
+
const inputContext = !bridge && targetId && (targetId !== previousTarget || frameId !== previousFrame)
|
|
991
|
+
? { targetId, frameId }
|
|
992
|
+
: undefined;
|
|
993
|
+
try {
|
|
994
|
+
if (bridge && targetId) {
|
|
995
|
+
bridge.setCurrentTargetId(targetId);
|
|
996
|
+
if (frameId !== undefined) {
|
|
997
|
+
bridge.setCurrentFrameId(frameId, documentId);
|
|
998
|
+
}
|
|
999
|
+
}
|
|
1000
|
+
for (const key of keys) {
|
|
1001
|
+
if (keysBefore.has(key)) {
|
|
1002
|
+
continue;
|
|
1003
|
+
}
|
|
1004
|
+
try {
|
|
1005
|
+
await this.keyUp(key, remainingNavigationTimeout(cleanupDeadline), inputContext);
|
|
1006
|
+
}
|
|
1007
|
+
catch (error) {
|
|
1008
|
+
console.warn('[MCP] Cancelled input key release failed:', key, error);
|
|
1009
|
+
}
|
|
1010
|
+
}
|
|
1011
|
+
}
|
|
1012
|
+
finally {
|
|
1013
|
+
if (bridge && previousTarget !== undefined) {
|
|
1014
|
+
bridge.setCurrentTargetId(previousTarget);
|
|
1015
|
+
if (previousFrame !== undefined) {
|
|
1016
|
+
bridge.setCurrentFrameId(previousFrame, previousDocument);
|
|
1017
|
+
}
|
|
1018
|
+
}
|
|
1019
|
+
}
|
|
1020
|
+
});
|
|
1021
|
+
}
|
|
1022
|
+
catch (error) {
|
|
1023
|
+
console.warn('[MCP] Cancelled input scope cleanup failed:', error);
|
|
1024
|
+
}
|
|
1025
|
+
};
|
|
1026
|
+
scope.inputCleanup = cleanup;
|
|
1027
|
+
let failed = false;
|
|
1028
|
+
let primaryError;
|
|
1029
|
+
let cleanupError;
|
|
1030
|
+
let value;
|
|
1031
|
+
try {
|
|
1032
|
+
value = await fn();
|
|
1033
|
+
}
|
|
1034
|
+
catch (error) {
|
|
1035
|
+
failed = true;
|
|
1036
|
+
primaryError = error;
|
|
1037
|
+
if (scope.keyboardFocus?.warning) {
|
|
1038
|
+
setErrorPageContext(error, { keyboardFocusWarning: scope.keyboardFocus.warning });
|
|
1039
|
+
}
|
|
1040
|
+
throw error;
|
|
1041
|
+
}
|
|
1042
|
+
finally {
|
|
1043
|
+
await cleanup(failed && isNativeExecutionResultUnknown(primaryError));
|
|
1044
|
+
const focus = scope.keyboardFocus;
|
|
1045
|
+
if (focus && driver === this.extensionBridge) {
|
|
1046
|
+
scope.inputCleanup = undefined;
|
|
1047
|
+
const releaseKeys = failed ||
|
|
1048
|
+
Boolean(scope.signal?.aborted || scope.budgetError) ||
|
|
1049
|
+
(scope.deadline !== undefined && Date.now() >= scope.deadline);
|
|
1050
|
+
let result;
|
|
1051
|
+
try {
|
|
1052
|
+
result = await this.tabLockContext.run({
|
|
1053
|
+
...scope,
|
|
1054
|
+
signal: undefined,
|
|
1055
|
+
deadline: Date.now() + 4000,
|
|
1056
|
+
budgetError: undefined,
|
|
1057
|
+
userAction: undefined,
|
|
1058
|
+
pendingPageCondition: null,
|
|
1059
|
+
keyboardFocus: undefined,
|
|
1060
|
+
inputCleanup: undefined,
|
|
1061
|
+
}, () => this.extensionBridge.releaseKeyboardFocus(focus, releaseKeys));
|
|
1062
|
+
if (releaseKeys && result.restored) {
|
|
1063
|
+
for (const key of keys) {
|
|
1064
|
+
if (!keysBefore.has(key)) {
|
|
1065
|
+
this.pressedKeys.delete(key);
|
|
1066
|
+
this.modifiers &= ~(MODIFIER_KEYS[key] ?? 0);
|
|
1067
|
+
}
|
|
1068
|
+
}
|
|
1069
|
+
}
|
|
1070
|
+
}
|
|
1071
|
+
catch (error) {
|
|
1072
|
+
result = { restored: false, errors: [error instanceof Error ? error.message : String(error)] };
|
|
1073
|
+
}
|
|
1074
|
+
finally {
|
|
1075
|
+
scope.keyboardFocus = undefined;
|
|
1076
|
+
}
|
|
1077
|
+
if (!result.restored) {
|
|
1078
|
+
if (failed) {
|
|
1079
|
+
setErrorPageContext(primaryError, { keyboardFocusCleanup: result });
|
|
1080
|
+
}
|
|
1081
|
+
else {
|
|
1082
|
+
cleanupError = new StructuredToolError('KEYBOARD_FOCUS_CLEANUP_FAILED', '原生按键已执行,焦点状态恢复未完成', '请读取当前页面与 Extension 日志', {
|
|
1083
|
+
failureStage: 'cleanup',
|
|
1084
|
+
retryable: false,
|
|
1085
|
+
keyboardFocusCleanup: result,
|
|
1086
|
+
});
|
|
1087
|
+
}
|
|
1088
|
+
}
|
|
1089
|
+
}
|
|
1090
|
+
}
|
|
1091
|
+
if (cleanupError) {
|
|
1092
|
+
throw cleanupError;
|
|
1093
|
+
}
|
|
1094
|
+
return value;
|
|
1095
|
+
}
|
|
1096
|
+
async wait(ms) {
|
|
1097
|
+
this.assertRequestActive();
|
|
1098
|
+
await delay(ms, undefined, { signal: this.tabLockContext.getStore()?.signal });
|
|
1099
|
+
this.assertRequestActive();
|
|
1100
|
+
}
|
|
1101
|
+
async withMetadataContext(fn, deadline, signal) {
|
|
1102
|
+
const mode = this.getMode();
|
|
1103
|
+
return this.tabLockContext.run({ session: this, deadline, signal, mode: mode === 'none' ? undefined : mode }, async () => {
|
|
1104
|
+
this.assertRequestActive();
|
|
1105
|
+
return fn();
|
|
1106
|
+
});
|
|
1107
|
+
}
|
|
1108
|
+
async withTabLock(fn, deadline, signal) {
|
|
1109
|
+
signal?.throwIfAborted();
|
|
798
1110
|
if (this.tabLockContext.getStore()?.session === this) {
|
|
1111
|
+
this.assertRequestActive();
|
|
799
1112
|
if (deadline !== undefined) {
|
|
800
1113
|
remainingNavigationTimeout(deadline);
|
|
801
1114
|
}
|
|
@@ -808,9 +1121,28 @@ class UnifiedSessionManager {
|
|
|
808
1121
|
});
|
|
809
1122
|
let acquired = false;
|
|
810
1123
|
try {
|
|
811
|
-
await waitForNavigationQueue(previousLock, deadline);
|
|
1124
|
+
await waitForNavigationQueue(previousLock, deadline, signal);
|
|
812
1125
|
acquired = true;
|
|
813
|
-
|
|
1126
|
+
signal?.throwIfAborted();
|
|
1127
|
+
return await this.tabLockContext.run({ session: this, deadline, signal }, async () => {
|
|
1128
|
+
const scope = this.tabLockContext.getStore();
|
|
1129
|
+
try {
|
|
1130
|
+
return await fn();
|
|
1131
|
+
}
|
|
1132
|
+
finally {
|
|
1133
|
+
try {
|
|
1134
|
+
if (scope.signal?.aborted ||
|
|
1135
|
+
scope.budgetError ||
|
|
1136
|
+
(scope.deadline !== undefined && Date.now() >= scope.deadline)) {
|
|
1137
|
+
await scope.inputCleanup?.();
|
|
1138
|
+
}
|
|
1139
|
+
}
|
|
1140
|
+
finally {
|
|
1141
|
+
scope.inputKeys = undefined;
|
|
1142
|
+
scope.inputCleanup = undefined;
|
|
1143
|
+
}
|
|
1144
|
+
}
|
|
1145
|
+
});
|
|
814
1146
|
}
|
|
815
1147
|
finally {
|
|
816
1148
|
if (acquired) {
|
|
@@ -834,6 +1166,10 @@ class UnifiedSessionManager {
|
|
|
834
1166
|
return this.withTabLock(async () => {
|
|
835
1167
|
const remaining = () => (deadline === undefined ? undefined : remainingNavigationTimeout(deadline));
|
|
836
1168
|
const mode = this.getMode();
|
|
1169
|
+
const scope = this.tabLockContext.getStore();
|
|
1170
|
+
if (mode !== 'none') {
|
|
1171
|
+
scope.mode = mode;
|
|
1172
|
+
}
|
|
837
1173
|
if (!targetId) {
|
|
838
1174
|
const current = this.getCurrentTargetId();
|
|
839
1175
|
if (current) {
|
|
@@ -892,49 +1228,79 @@ class UnifiedSessionManager {
|
|
|
892
1228
|
temporaryTargetId: targetId,
|
|
893
1229
|
currentTargetId: null,
|
|
894
1230
|
};
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
1231
|
+
const independentCleanup = (scope.restoreTargetAfterPreparationFailure === true ||
|
|
1232
|
+
scope.restoreTargetAfterLookup === true) &&
|
|
1233
|
+
(scope.signal?.aborted === true ||
|
|
1234
|
+
scope.budgetError !== undefined ||
|
|
1235
|
+
(scope.deadline !== undefined && Date.now() >= scope.deadline));
|
|
1236
|
+
scope.restoreTargetAfterPreparationFailure = undefined;
|
|
1237
|
+
scope.restoreTargetAfterLookup = undefined;
|
|
1238
|
+
const cleanupDeadline = independentCleanup ? Date.now() + 1000 : deadline;
|
|
1239
|
+
const cleanupRemaining = () => cleanupDeadline === undefined ? undefined : remainingNavigationTimeout(cleanupDeadline);
|
|
1240
|
+
await this.tabLockContext.run(independentCleanup
|
|
1241
|
+
? {
|
|
1242
|
+
...scope,
|
|
1243
|
+
signal: undefined,
|
|
1244
|
+
deadline: cleanupDeadline,
|
|
1245
|
+
budgetError: undefined,
|
|
1246
|
+
userAction: undefined,
|
|
1247
|
+
pendingPageCondition: null,
|
|
1248
|
+
keyboardFocus: undefined,
|
|
1249
|
+
inputCleanup: undefined,
|
|
906
1250
|
}
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
restoration.status = 'failed';
|
|
910
|
-
restoration.error = error instanceof Error ? error.message : String(error);
|
|
911
|
-
}
|
|
912
|
-
if (clearTarget) {
|
|
1251
|
+
: scope, async () => {
|
|
1252
|
+
let clearTarget = true;
|
|
913
1253
|
try {
|
|
914
|
-
if (
|
|
915
|
-
driver.
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
1254
|
+
if (previousId) {
|
|
1255
|
+
const targets = await driver.listTargets(cleanupRemaining());
|
|
1256
|
+
if (targets.some((target) => (target.targetId ?? String(target.id)) === previousId)) {
|
|
1257
|
+
await driver.selectPage(previousId, cleanupRemaining());
|
|
1258
|
+
clearTarget = false;
|
|
1259
|
+
}
|
|
1260
|
+
else {
|
|
1261
|
+
restoration.status = 'previous_target_closed';
|
|
1262
|
+
}
|
|
919
1263
|
}
|
|
920
1264
|
}
|
|
921
1265
|
catch (error) {
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
1266
|
+
restoration.status = 'failed';
|
|
1267
|
+
restoration.error = error instanceof Error ? error.message : String(error);
|
|
1268
|
+
}
|
|
1269
|
+
if (clearTarget) {
|
|
1270
|
+
try {
|
|
1271
|
+
if (mode === 'extension') {
|
|
1272
|
+
driver.setCurrentTargetId(null);
|
|
1273
|
+
}
|
|
1274
|
+
else {
|
|
1275
|
+
await getCdpSession().deselectPage(cleanupDeadline === undefined
|
|
1276
|
+
? undefined
|
|
1277
|
+
: Math.max(0, cleanupDeadline - Date.now()));
|
|
1278
|
+
}
|
|
925
1279
|
}
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
restoration.error
|
|
1280
|
+
catch (error) {
|
|
1281
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
1282
|
+
if (restoration.error) {
|
|
1283
|
+
restoration.cleanupError = message;
|
|
1284
|
+
}
|
|
1285
|
+
else {
|
|
1286
|
+
restoration.status = 'failed';
|
|
1287
|
+
restoration.error = message;
|
|
1288
|
+
}
|
|
929
1289
|
}
|
|
930
1290
|
}
|
|
931
|
-
}
|
|
1291
|
+
});
|
|
932
1292
|
restoration.currentTargetId = driver.getCurrentTargetId();
|
|
933
|
-
|
|
1293
|
+
scope.targetRestoration = restoration;
|
|
934
1294
|
}
|
|
935
1295
|
}
|
|
936
1296
|
}, deadline);
|
|
937
1297
|
}
|
|
1298
|
+
preserveTargetAfterLookup() {
|
|
1299
|
+
const scope = this.tabLockContext.getStore();
|
|
1300
|
+
if (scope) {
|
|
1301
|
+
scope.restoreTargetAfterLookup = true;
|
|
1302
|
+
}
|
|
1303
|
+
}
|
|
938
1304
|
getTargetRestoration() {
|
|
939
1305
|
return this.tabLockContext.getStore()?.targetRestoration;
|
|
940
1306
|
}
|
|
@@ -951,57 +1317,81 @@ class UnifiedSessionManager {
|
|
|
951
1317
|
? await this.extensionBridge.getPageSnapshot(id, timeout)
|
|
952
1318
|
: await getCdpSession().getPageSnapshot(id, timeout);
|
|
953
1319
|
this.recordPageUse(id);
|
|
1320
|
+
const nativeMetadata = snapshot.metadata;
|
|
1321
|
+
if (nativeMetadata) {
|
|
1322
|
+
this.rememberPageMetadata(id, nativeMetadata);
|
|
1323
|
+
}
|
|
954
1324
|
return snapshot;
|
|
955
1325
|
}
|
|
956
1326
|
async withPageCondition(condition, fn, deadline) {
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
const mode = this.getMode();
|
|
962
|
-
const targetId = condition.targetId ?? this.getCurrentTargetId();
|
|
963
|
-
if (mode === 'none' || !targetId) {
|
|
964
|
-
throw new DriverCapabilityError('没有操作目标,请先选择页面');
|
|
965
|
-
}
|
|
966
|
-
const driver = mode === 'extension' ? this.extensionBridge : getCdpSession();
|
|
967
|
-
const previousRequireExtension = this.requireExtension;
|
|
968
|
-
const previousCdpSelection = this.cdpExplicitlySelected;
|
|
969
|
-
const previousMode = this.activeMode;
|
|
970
|
-
if (mode === 'extension') {
|
|
971
|
-
this.requireExtension = true;
|
|
972
|
-
}
|
|
973
|
-
else {
|
|
974
|
-
this.cdpExplicitlySelected = true;
|
|
1327
|
+
let contextPhase = 'action';
|
|
1328
|
+
try {
|
|
1329
|
+
if (condition.expectedUrl === undefined && condition.expectedRevision === undefined) {
|
|
1330
|
+
return await fn();
|
|
975
1331
|
}
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
1332
|
+
return await this.withTabLock(async () => {
|
|
1333
|
+
contextPhase = 'precondition';
|
|
1334
|
+
const userAction = this.tabLockContext.getStore()?.userAction;
|
|
1335
|
+
if (userAction) {
|
|
1336
|
+
userAction.phase = 'precondition';
|
|
980
1337
|
}
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
1338
|
+
const mode = this.getMode();
|
|
1339
|
+
const targetId = condition.targetId ?? this.getCurrentTargetId();
|
|
1340
|
+
if (mode === 'none' || !targetId) {
|
|
1341
|
+
throw new DriverCapabilityError('没有操作目标,请先选择页面');
|
|
1342
|
+
}
|
|
1343
|
+
const driver = mode === 'extension' ? this.extensionBridge : getCdpSession();
|
|
1344
|
+
const previousRequireExtension = this.requireExtension;
|
|
1345
|
+
const previousCdpSelection = this.cdpExplicitlySelected;
|
|
1346
|
+
const previousMode = this.activeMode;
|
|
1347
|
+
if (mode === 'extension') {
|
|
1348
|
+
this.requireExtension = true;
|
|
1349
|
+
}
|
|
1350
|
+
else {
|
|
1351
|
+
this.cdpExplicitlySelected = true;
|
|
1352
|
+
}
|
|
1353
|
+
try {
|
|
1354
|
+
const snapshot = await driver.getPageSnapshot(targetId, deadline === undefined ? undefined : remainingNavigationTimeout(deadline));
|
|
1355
|
+
if (snapshot.revision === null) {
|
|
1356
|
+
throw new StructuredToolError('DRIVER_CAPABILITY_ERROR', '当前 Extension 不支持原子页面条件,未执行操作', '更新 Extension 后再使用 expectedUrl 或 expectedRevision', {
|
|
1357
|
+
targetId,
|
|
1358
|
+
actionExecuted: false,
|
|
1359
|
+
actionStatus: 'not_started',
|
|
1360
|
+
failureStage: 'precondition',
|
|
1361
|
+
});
|
|
1362
|
+
}
|
|
1363
|
+
if ((condition.expectedUrl !== undefined && snapshot.url !== condition.expectedUrl) ||
|
|
1364
|
+
(condition.expectedRevision !== undefined && snapshot.revision !== condition.expectedRevision)) {
|
|
1365
|
+
throw new PagePreconditionError({
|
|
1366
|
+
targetId,
|
|
1367
|
+
expectedUrl: condition.expectedUrl ?? null,
|
|
1368
|
+
expectedRevision: condition.expectedRevision ?? null,
|
|
1369
|
+
actualUrl: snapshot.url,
|
|
1370
|
+
actualRevision: snapshot.revision,
|
|
1371
|
+
});
|
|
1372
|
+
}
|
|
1373
|
+
driver.setPageCondition({
|
|
984
1374
|
targetId,
|
|
985
|
-
expectedUrl: condition.expectedUrl
|
|
986
|
-
expectedRevision: condition.expectedRevision
|
|
987
|
-
actualUrl: snapshot.url,
|
|
988
|
-
actualRevision: snapshot.revision,
|
|
1375
|
+
expectedUrl: condition.expectedUrl,
|
|
1376
|
+
expectedRevision: condition.expectedRevision,
|
|
989
1377
|
});
|
|
1378
|
+
contextPhase = 'action';
|
|
1379
|
+
if (userAction) {
|
|
1380
|
+
userAction.phase = 'preparation';
|
|
1381
|
+
}
|
|
1382
|
+
return await fn();
|
|
990
1383
|
}
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
}
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
this.activeMode = previousMode;
|
|
1003
|
-
}
|
|
1004
|
-
}, deadline);
|
|
1384
|
+
finally {
|
|
1385
|
+
driver.setPageCondition(null);
|
|
1386
|
+
this.requireExtension = previousRequireExtension;
|
|
1387
|
+
this.cdpExplicitlySelected = previousCdpSelection;
|
|
1388
|
+
this.activeMode = previousMode;
|
|
1389
|
+
}
|
|
1390
|
+
}, deadline);
|
|
1391
|
+
}
|
|
1392
|
+
catch (error) {
|
|
1393
|
+
throw setErrorPageContext(error, { contextPhase });
|
|
1394
|
+
}
|
|
1005
1395
|
}
|
|
1006
1396
|
/**
|
|
1007
1397
|
* 临时切换操作目标 iframe,执行完后恢复
|
|
@@ -1012,38 +1402,91 @@ class UnifiedSessionManager {
|
|
|
1012
1402
|
* 与 withTabId 配合使用时,应嵌套在 withTabId 内部:
|
|
1013
1403
|
* withTabId(tabId, () => withFrame(frame, () => { ... }))
|
|
1014
1404
|
*/
|
|
1015
|
-
async withFrame(frame, fn) {
|
|
1405
|
+
async withFrame(frame, fn, deadline) {
|
|
1016
1406
|
if (frame === undefined) {
|
|
1017
1407
|
return fn();
|
|
1018
1408
|
}
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
const
|
|
1023
|
-
|
|
1409
|
+
const scope = this.tabLockContext.getStore();
|
|
1410
|
+
deadline = deadline === undefined ? scope?.deadline : Math.min(deadline, scope?.deadline ?? deadline);
|
|
1411
|
+
const driver = await this.getDriver(deadline === undefined ? undefined : remainingNavigationTimeout(deadline));
|
|
1412
|
+
const { frameId, offset, frameContext } = await driver
|
|
1413
|
+
.resolveFrame(frame, deadline === undefined ? undefined : remainingNavigationTimeout(deadline))
|
|
1414
|
+
.catch((error) => {
|
|
1415
|
+
if (!scope?.userAction?.dispatched) {
|
|
1416
|
+
if (scope) {
|
|
1417
|
+
scope.restoreTargetAfterPreparationFailure = true;
|
|
1418
|
+
}
|
|
1419
|
+
throw setErrorPageContext(error, { contextPhase: 'preparation', frameResolutionPreparation: true });
|
|
1420
|
+
}
|
|
1421
|
+
throw error;
|
|
1422
|
+
});
|
|
1024
1423
|
const previousFrameId = driver.getCurrentFrameId();
|
|
1025
|
-
const
|
|
1026
|
-
const
|
|
1027
|
-
const
|
|
1028
|
-
|
|
1424
|
+
const extension = driver === this.extensionBridge ? this.extensionBridge : undefined;
|
|
1425
|
+
const previousDocument = extension?.getCurrentFrameDocumentId?.();
|
|
1426
|
+
const previousOffset = this.currentFrameOffset;
|
|
1427
|
+
if (extension) {
|
|
1428
|
+
extension.setCurrentFrameId(frameId, typeof frameContext?.documentId === 'string' ? frameContext.documentId : undefined);
|
|
1429
|
+
}
|
|
1430
|
+
else {
|
|
1431
|
+
driver.setCurrentFrameId(frameId);
|
|
1432
|
+
}
|
|
1029
1433
|
this.currentFrameOffset = offset;
|
|
1030
|
-
this.requireExtension = true;
|
|
1031
|
-
this.activeMode = 'extension';
|
|
1032
1434
|
try {
|
|
1033
1435
|
return await fn();
|
|
1034
1436
|
}
|
|
1035
1437
|
catch (error) {
|
|
1036
|
-
throw setErrorPageContext(error, {
|
|
1037
|
-
frameContext: { selection: frame, frameId, ...frameContext },
|
|
1038
|
-
});
|
|
1438
|
+
throw setErrorPageContext(error, { frameContext: { selection: frame, frameId, ...frameContext } });
|
|
1039
1439
|
}
|
|
1040
1440
|
finally {
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1441
|
+
this.currentFrameOffset = previousOffset;
|
|
1442
|
+
if (extension) {
|
|
1443
|
+
extension.setCurrentFrameId(previousFrameId, previousDocument);
|
|
1444
|
+
}
|
|
1445
|
+
else {
|
|
1446
|
+
driver.setCurrentFrameId(previousFrameId);
|
|
1447
|
+
}
|
|
1448
|
+
}
|
|
1449
|
+
}
|
|
1450
|
+
hasBrowserEventObservation(targetId) {
|
|
1451
|
+
return this.getMode() === 'cdp' || this.extensionBridge?.hasBrowserEventObservation(targetId) === true;
|
|
1452
|
+
}
|
|
1453
|
+
async browserEvents(action, command, targetId, timeout = DEFAULT_TIMEOUT, signal, deadline = Date.now() + timeout) {
|
|
1454
|
+
const refTarget = command.ref ? eventRefTarget(command.ref) : undefined;
|
|
1455
|
+
const mode = refTarget?.backend ?? this.getMode();
|
|
1456
|
+
if (targetId && refTarget && targetId !== refTarget.targetId) {
|
|
1457
|
+
throw new DriverCapabilityError('Browser event ref target mismatch');
|
|
1458
|
+
}
|
|
1459
|
+
const driver = mode === 'extension' ? this.extensionBridge : mode === 'cdp' ? getCdpSession() : null;
|
|
1460
|
+
if (!driver) {
|
|
1461
|
+
throw new DriverCapabilityError('Browser event backend is not connected');
|
|
1046
1462
|
}
|
|
1463
|
+
const id = refTarget?.targetId ?? targetId ?? driver.getCurrentTargetId() ?? undefined;
|
|
1464
|
+
const scope = this.tabLockContext.getStore();
|
|
1465
|
+
return this.tabLockContext.run({
|
|
1466
|
+
session: this,
|
|
1467
|
+
signal: signal ?? scope?.signal,
|
|
1468
|
+
deadline: Math.min(scope?.deadline ?? Infinity, deadline),
|
|
1469
|
+
}, () => driver.browserEvents(action, command, id, timeout));
|
|
1470
|
+
}
|
|
1471
|
+
async browserFetchCommand(method, params, targetId, signal) {
|
|
1472
|
+
const mode = this.getMode();
|
|
1473
|
+
const driver = mode === 'extension' ? this.extensionBridge : mode === 'cdp' ? getCdpSession() : null;
|
|
1474
|
+
if (!driver?.browserFetchCommand) {
|
|
1475
|
+
throw new DriverCapabilityError('Fetch control backend unavailable');
|
|
1476
|
+
}
|
|
1477
|
+
const scope = this.tabLockContext.getStore();
|
|
1478
|
+
return this.tabLockContext.run({
|
|
1479
|
+
...scope,
|
|
1480
|
+
session: this,
|
|
1481
|
+
signal: signal ?? scope?.signal,
|
|
1482
|
+
deadline: Math.min(scope?.deadline ?? Infinity, Date.now() + DEFAULT_TIMEOUT),
|
|
1483
|
+
}, () => driver.browserFetchCommand(method, params, targetId));
|
|
1484
|
+
}
|
|
1485
|
+
async suspendBrowserEventsForChallenge() {
|
|
1486
|
+
const driver = await this.getDriver();
|
|
1487
|
+
return driver.suspendBrowserEventsForChallenge
|
|
1488
|
+
? driver.suspendBrowserEventsForChallenge()
|
|
1489
|
+
: { suspended: false, reason: 'backend capability unavailable' };
|
|
1047
1490
|
}
|
|
1048
1491
|
/**
|
|
1049
1492
|
* 获取当前状态
|
|
@@ -1055,15 +1498,15 @@ class UnifiedSessionManager {
|
|
|
1055
1498
|
}
|
|
1056
1499
|
return getCdpSession().getState();
|
|
1057
1500
|
}
|
|
1058
|
-
async getLiveState() {
|
|
1501
|
+
async getLiveState(timeout) {
|
|
1059
1502
|
if (this.getMode() !== 'extension') {
|
|
1060
|
-
return getCdpSession().getLiveState();
|
|
1503
|
+
return getCdpSession().getLiveState(timeout);
|
|
1061
1504
|
}
|
|
1062
1505
|
const targetId = this.extensionBridge.getCurrentTargetId();
|
|
1063
1506
|
if (!targetId) {
|
|
1064
1507
|
return null;
|
|
1065
1508
|
}
|
|
1066
|
-
const targets = await this.extensionBridge.listTargets();
|
|
1509
|
+
const targets = await this.extensionBridge.listTargets(timeout);
|
|
1067
1510
|
const target = targets.find((item) => item.targetId === targetId);
|
|
1068
1511
|
if (!target) {
|
|
1069
1512
|
return null;
|
|
@@ -1087,24 +1530,27 @@ class UnifiedSessionManager {
|
|
|
1087
1530
|
/**
|
|
1088
1531
|
* 按下键盘按键
|
|
1089
1532
|
*/
|
|
1090
|
-
async keyDown(key, commands) {
|
|
1091
|
-
const
|
|
1533
|
+
async keyDown(key, commands, timeout) {
|
|
1534
|
+
const deadline = timeout === undefined ? undefined : Date.now() + timeout;
|
|
1535
|
+
const driver = await this.getDriver(timeout);
|
|
1536
|
+
const remaining = () => (deadline === undefined ? undefined : remainingNavigationTimeout(deadline));
|
|
1092
1537
|
const isExt = this.isExtensionConnected();
|
|
1093
1538
|
// 预检查:stealth + commands 必然抛错,要在任何状态变更前抛出,避免 modifiers 污染
|
|
1094
1539
|
if (isExt && this.inputMode === 'stealth' && commands && commands.length > 0) {
|
|
1095
1540
|
throw new Error('commands 参数不支持 stealth 输入模式,请先调用 manage action=inputMode inputMode=precise 切换后重试');
|
|
1096
1541
|
}
|
|
1097
1542
|
// Puppeteer 风格:连续 keyDown 同 key → rawKeyDown + autoRepeat(长按重复)
|
|
1543
|
+
this.tabLockContext.getStore()?.inputKeys?.add(key);
|
|
1098
1544
|
const isRepeat = this.pressedKeys.has(key);
|
|
1099
1545
|
const nextModifiers = MODIFIER_KEYS[key] ? this.modifiers | MODIFIER_KEYS[key] : this.modifiers;
|
|
1100
1546
|
if (!isExt) {
|
|
1101
|
-
await getCdpSession().keyDown(key, commands, { rawKeyDown: isRepeat, autoRepeat: isRepeat });
|
|
1547
|
+
await getCdpSession().keyDown(key, commands, { rawKeyDown: isRepeat, autoRepeat: isRepeat }, remaining());
|
|
1102
1548
|
this.modifiers = nextModifiers;
|
|
1103
1549
|
this.pressedKeys.add(key);
|
|
1104
1550
|
return;
|
|
1105
1551
|
}
|
|
1106
1552
|
if (isExt && this.inputMode === 'stealth') {
|
|
1107
|
-
await driver.stealthKey(key, 'down', this.getModifierNames(nextModifiers));
|
|
1553
|
+
await driver.stealthKey(key, 'down', this.getModifierNames(nextModifiers), remaining());
|
|
1108
1554
|
this.modifiers = nextModifiers;
|
|
1109
1555
|
this.pressedKeys.add(key);
|
|
1110
1556
|
return;
|
|
@@ -1118,22 +1564,29 @@ class UnifiedSessionManager {
|
|
|
1118
1564
|
modifiers: nextModifiers,
|
|
1119
1565
|
commands,
|
|
1120
1566
|
autoRepeat: isRepeat || undefined,
|
|
1121
|
-
});
|
|
1567
|
+
}, remaining());
|
|
1122
1568
|
this.modifiers = nextModifiers;
|
|
1123
1569
|
this.pressedKeys.add(key);
|
|
1124
1570
|
}
|
|
1125
1571
|
/**
|
|
1126
1572
|
* 释放键盘按键
|
|
1127
1573
|
*/
|
|
1128
|
-
async keyUp(key) {
|
|
1129
|
-
const
|
|
1574
|
+
async keyUp(key, timeout, inputContext) {
|
|
1575
|
+
const deadline = timeout === undefined ? undefined : Date.now() + timeout;
|
|
1576
|
+
const driver = await this.getDriver(timeout);
|
|
1577
|
+
const remaining = () => (deadline === undefined ? undefined : remainingNavigationTimeout(deadline));
|
|
1130
1578
|
const isExt = this.isExtensionConnected();
|
|
1131
1579
|
const nextModifiers = MODIFIER_KEYS[key] ? this.modifiers & ~MODIFIER_KEYS[key] : this.modifiers;
|
|
1132
1580
|
if (!isExt) {
|
|
1133
|
-
|
|
1581
|
+
if (inputContext) {
|
|
1582
|
+
await getCdpSession().keyUp(key, remaining(), { ...inputContext, modifiers: nextModifiers });
|
|
1583
|
+
}
|
|
1584
|
+
else {
|
|
1585
|
+
await getCdpSession().keyUp(key, remaining());
|
|
1586
|
+
}
|
|
1134
1587
|
}
|
|
1135
1588
|
else if (this.inputMode === 'stealth') {
|
|
1136
|
-
await driver.stealthKey(key, 'up', this.getModifierNames(nextModifiers));
|
|
1589
|
+
await driver.stealthKey(key, 'up', this.getModifierNames(nextModifiers), remaining());
|
|
1137
1590
|
}
|
|
1138
1591
|
else {
|
|
1139
1592
|
const def = getKeyDefinition(key);
|
|
@@ -1142,10 +1595,11 @@ class UnifiedSessionManager {
|
|
|
1142
1595
|
code: def.code,
|
|
1143
1596
|
windowsVirtualKeyCode: def.keyCode,
|
|
1144
1597
|
modifiers: nextModifiers,
|
|
1145
|
-
});
|
|
1598
|
+
}, remaining());
|
|
1146
1599
|
}
|
|
1147
1600
|
this.modifiers = nextModifiers;
|
|
1148
1601
|
this.pressedKeys.delete(key);
|
|
1602
|
+
this.tabLockContext.getStore()?.inputKeys?.delete(key);
|
|
1149
1603
|
}
|
|
1150
1604
|
/**
|
|
1151
1605
|
* 输入文本
|
|
@@ -1172,50 +1626,59 @@ class UnifiedSessionManager {
|
|
|
1172
1626
|
if (remaining !== undefined && remaining <= 0) {
|
|
1173
1627
|
throw new TimeoutError('input timeout before mouse movement');
|
|
1174
1628
|
}
|
|
1175
|
-
this.currentMousePosition = { x, y }; // 更新位置
|
|
1176
1629
|
const isExt = this.isExtensionConnected();
|
|
1630
|
+
if (!isExt) {
|
|
1631
|
+
;
|
|
1632
|
+
({ x, y } = await getCdpSession().mapFramePoint({ x, y }, remaining));
|
|
1633
|
+
}
|
|
1177
1634
|
if (isExt && this.inputMode === 'stealth') {
|
|
1178
|
-
await driver.stealthMouse('mousemove', x, y, undefined, remaining);
|
|
1635
|
+
await driver.stealthMouse('mousemove', x, y, undefined, remaining, this.modifiers);
|
|
1179
1636
|
}
|
|
1180
1637
|
else {
|
|
1181
1638
|
await driver.inputMouse('mouseMoved', x, y, { modifiers: this.modifiers }, remaining);
|
|
1182
1639
|
}
|
|
1640
|
+
this.currentMousePosition = { x, y };
|
|
1641
|
+
this.rememberMouseFrame();
|
|
1183
1642
|
}
|
|
1184
1643
|
/**
|
|
1185
1644
|
* 鼠标按下
|
|
1186
1645
|
*/
|
|
1187
1646
|
async mouseDown(button = 'left', clickCount = 1) {
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
|
|
1647
|
+
return this.withMouseFrame(async () => {
|
|
1648
|
+
const { x, y } = this.currentMousePosition;
|
|
1649
|
+
const driver = await this.getDriver();
|
|
1650
|
+
const isExt = this.isExtensionConnected();
|
|
1651
|
+
if (isExt && this.inputMode === 'stealth') {
|
|
1652
|
+
await driver.stealthMouse('mousedown', x, y, button, undefined, this.modifiers);
|
|
1653
|
+
}
|
|
1654
|
+
else {
|
|
1655
|
+
await driver.inputMouse('mousePressed', x, y, {
|
|
1656
|
+
button,
|
|
1657
|
+
clickCount,
|
|
1658
|
+
modifiers: this.modifiers,
|
|
1659
|
+
});
|
|
1660
|
+
}
|
|
1661
|
+
});
|
|
1201
1662
|
}
|
|
1202
1663
|
/**
|
|
1203
1664
|
* 鼠标释放
|
|
1204
1665
|
*/
|
|
1205
1666
|
async mouseUp(button = 'left', clickCount = 1) {
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
|
|
1667
|
+
return this.withMouseFrame(async () => {
|
|
1668
|
+
const { x, y } = this.currentMousePosition;
|
|
1669
|
+
const driver = await this.getDriver();
|
|
1670
|
+
const isExt = this.isExtensionConnected();
|
|
1671
|
+
if (isExt && this.inputMode === 'stealth') {
|
|
1672
|
+
await driver.stealthMouse('mouseup', x, y, button, undefined, this.modifiers);
|
|
1673
|
+
}
|
|
1674
|
+
else {
|
|
1675
|
+
await driver.inputMouse('mouseReleased', x, y, {
|
|
1676
|
+
button,
|
|
1677
|
+
clickCount,
|
|
1678
|
+
modifiers: this.modifiers,
|
|
1679
|
+
});
|
|
1680
|
+
}
|
|
1681
|
+
});
|
|
1219
1682
|
}
|
|
1220
1683
|
/**
|
|
1221
1684
|
* 鼠标点击(mousedown + mouseup + click 三合一)
|
|
@@ -1224,50 +1687,71 @@ class UnifiedSessionManager {
|
|
|
1224
1687
|
* precise / CDP 模式:mouseDown + mouseUp,浏览器自动合成原生 click 事件
|
|
1225
1688
|
*/
|
|
1226
1689
|
async mouseClick(button = 'left', clickCount = 1, refId) {
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
|
|
1235
|
-
|
|
1236
|
-
|
|
1237
|
-
|
|
1238
|
-
|
|
1239
|
-
|
|
1240
|
-
|
|
1690
|
+
return this.withMouseFrame(async () => {
|
|
1691
|
+
const driver = await this.getDriver();
|
|
1692
|
+
const isExt = this.isExtensionConnected();
|
|
1693
|
+
if (isExt && (this.inputMode === 'stealth' || button === 'right')) {
|
|
1694
|
+
const { x, y } = this.currentMousePosition;
|
|
1695
|
+
// stealth 通过单次脚本注入完成所有 mouse/click/dblclick/contextmenu 事件
|
|
1696
|
+
// refId 透传:嵌套 iframe overlay 场景下绕过 elementFromPoint 命中外层 IFRAME 的问题
|
|
1697
|
+
await driver.stealthClick(x, y, button, clickCount, refId, undefined, this.modifiers);
|
|
1698
|
+
return;
|
|
1699
|
+
}
|
|
1700
|
+
// CDP 每次 mousePressed/mouseReleased 递增 clickCount,让浏览器合成 dblclick/tripleclick
|
|
1701
|
+
for (let i = 1; i <= clickCount; i++) {
|
|
1702
|
+
let pressRejected = false;
|
|
1703
|
+
try {
|
|
1704
|
+
await this.mouseDown(button, i);
|
|
1705
|
+
}
|
|
1706
|
+
catch (error) {
|
|
1707
|
+
pressRejected = error instanceof StructuredToolError && error.context.actionExecuted === false;
|
|
1708
|
+
throw error;
|
|
1709
|
+
}
|
|
1710
|
+
finally {
|
|
1711
|
+
if (!pressRejected) {
|
|
1712
|
+
const { x, y } = this.currentMousePosition;
|
|
1713
|
+
await driver.inputMouse('mouseReleased', x, y, {
|
|
1714
|
+
button,
|
|
1715
|
+
clickCount: i,
|
|
1716
|
+
modifiers: this.modifiers,
|
|
1717
|
+
});
|
|
1718
|
+
}
|
|
1719
|
+
}
|
|
1720
|
+
}
|
|
1721
|
+
});
|
|
1241
1722
|
}
|
|
1242
1723
|
/**
|
|
1243
1724
|
* Turnstile 点击:同页控件走页面鼠标;Cloudflare iframe 只在已聚焦窗口的活动受控 tab 上使用系统鼠标
|
|
1244
1725
|
*/
|
|
1245
1726
|
// noinspection JSUnusedGlobalSymbols — resolveChallenge 通过 ChallengeHost 结构类型调用
|
|
1246
|
-
async clickTurnstile(snapshot, timeout) {
|
|
1247
|
-
const
|
|
1248
|
-
|
|
1727
|
+
async clickTurnstile(snapshot, timeout, signal, refreshSnapshot) {
|
|
1728
|
+
const scope = this.tabLockContext.getStore();
|
|
1729
|
+
signal ??= scope?.signal;
|
|
1730
|
+
signal?.throwIfAborted();
|
|
1731
|
+
const deadline = timeout === undefined ? scope?.deadline : Math.min(Date.now() + timeout, scope?.deadline ?? Infinity);
|
|
1732
|
+
const remaining = () => {
|
|
1733
|
+
signal?.throwIfAborted();
|
|
1734
|
+
return deadline === undefined ? undefined : remainingNavigationTimeout(deadline);
|
|
1735
|
+
};
|
|
1249
1736
|
if (snapshot.clickSource !== 'iframe') {
|
|
1250
1737
|
const point = snapshot.clickPoint;
|
|
1251
1738
|
if (!point) {
|
|
1252
1739
|
throw new OsMouseError('TURNSTILE_OS_CLICK_UNAVAILABLE', '页面未返回可点击的 Turnstile 坐标', '重新检查页面状态后再重试;交互式验证码无法自动完成');
|
|
1253
1740
|
}
|
|
1254
|
-
const driver = await withNavigationBudget(() => this.getDriver(remaining()), deadline);
|
|
1741
|
+
const driver = await withNavigationBudget(() => this.getDriver(remaining()), deadline, signal);
|
|
1255
1742
|
if (this.isExtensionConnected()) {
|
|
1256
|
-
await withNavigationBudget(() => driver.stealthClick(point.x, point.y, 'left', 1, undefined, remaining()), deadline);
|
|
1743
|
+
await withNavigationBudget(() => driver.stealthClick(point.x, point.y, 'left', 1, undefined, remaining()), deadline, signal);
|
|
1257
1744
|
return;
|
|
1258
1745
|
}
|
|
1259
|
-
const
|
|
1260
|
-
const clickPoint = frameOffset && this.inputMode !== 'stealth'
|
|
1261
|
-
? { x: point.x + frameOffset.x, y: point.y + frameOffset.y }
|
|
1262
|
-
: point;
|
|
1746
|
+
const clickPoint = await withNavigationBudget(() => getCdpSession().mapFramePoint(point, remaining()), deadline, signal);
|
|
1263
1747
|
this.currentMousePosition = clickPoint;
|
|
1264
|
-
await withNavigationBudget(() => driver.inputMouse('mouseMoved', clickPoint.x, clickPoint.y, { modifiers: this.modifiers }), deadline);
|
|
1748
|
+
await withNavigationBudget(() => driver.inputMouse('mouseMoved', clickPoint.x, clickPoint.y, { modifiers: this.modifiers }), deadline, signal);
|
|
1265
1749
|
await withNavigationBudget(() => getCdpSession().inputMouse('mousePressed', clickPoint.x, clickPoint.y, {
|
|
1266
1750
|
button: 'left',
|
|
1267
1751
|
clickCount: 1,
|
|
1268
1752
|
modifiers: this.modifiers,
|
|
1269
1753
|
releaseAfterPress: true,
|
|
1270
|
-
}, remaining()), deadline);
|
|
1754
|
+
}, remaining()), deadline, signal);
|
|
1271
1755
|
return;
|
|
1272
1756
|
}
|
|
1273
1757
|
if (this.getMode() !== 'extension' || !this.extensionBridge) {
|
|
@@ -1277,7 +1761,7 @@ class UnifiedSessionManager {
|
|
|
1277
1761
|
if (!targetId) {
|
|
1278
1762
|
throw new OsMouseError('TURNSTILE_OS_CLICK_UNAVAILABLE', '没有当前受控页面,无法使用系统鼠标点击 Turnstile', '先 browse attach 到受控测试页,把该页放到已经聚焦的窗口并保持为活动 tab 后重试');
|
|
1279
1763
|
}
|
|
1280
|
-
const topology = await this.listTopology(remaining());
|
|
1764
|
+
const topology = await withNavigationBudget(() => this.listTopology(remaining()), deadline, signal);
|
|
1281
1765
|
const windowInfo = topology?.windows.find((item) => item.tabs.some((tab) => String(tab.id) === targetId || tab.targetId === targetId));
|
|
1282
1766
|
const tab = windowInfo?.tabs.find((item) => String(item.id) === targetId || item.targetId === targetId);
|
|
1283
1767
|
if (!windowInfo || !tab) {
|
|
@@ -1293,29 +1777,96 @@ class UnifiedSessionManager {
|
|
|
1293
1777
|
if (tab.active !== true && (activeTabId === undefined || String(activeTabId) !== targetId)) {
|
|
1294
1778
|
throw new OsMouseError('TURNSTILE_OS_CLICK_UNAVAILABLE', '受控测试页不是当前窗口的活动 tab,拒绝系统鼠标点击', '先把受控测试页设为该窗口的活动 tab,且窗口已经聚焦后再重试');
|
|
1295
1779
|
}
|
|
1296
|
-
await withNavigationBudget(() => this.extensionBridge.debuggerDetach(undefined, remaining()), deadline);
|
|
1297
|
-
const
|
|
1298
|
-
|
|
1299
|
-
const
|
|
1300
|
-
|
|
1301
|
-
|
|
1302
|
-
|
|
1303
|
-
|
|
1304
|
-
|
|
1305
|
-
|
|
1306
|
-
|
|
1307
|
-
|
|
1308
|
-
|
|
1309
|
-
|
|
1310
|
-
|
|
1311
|
-
|
|
1312
|
-
|
|
1313
|
-
|
|
1314
|
-
|
|
1315
|
-
|
|
1316
|
-
|
|
1317
|
-
|
|
1318
|
-
|
|
1780
|
+
await withNavigationBudget(() => this.extensionBridge.debuggerDetach(undefined, remaining()), deadline, signal);
|
|
1781
|
+
const pointerId = process.platform === 'linux' ? randomUUID() : undefined;
|
|
1782
|
+
let pointerDocumentId;
|
|
1783
|
+
const resolvePoint = async (refresh, probe) => {
|
|
1784
|
+
const current = refresh && refreshSnapshot
|
|
1785
|
+
? await withNavigationBudget(() => refreshSnapshot(remaining()), deadline, signal)
|
|
1786
|
+
: snapshot;
|
|
1787
|
+
if (!current.clickPoint ||
|
|
1788
|
+
current.clickSource !== 'iframe' ||
|
|
1789
|
+
current.clickTargetKind !== snapshot.clickTargetKind ||
|
|
1790
|
+
current.url !== snapshot.url) {
|
|
1791
|
+
throw new OsMouseError('TURNSTILE_OS_CLICK_UNAVAILABLE', '系统鼠标按下前,页面或验证控件已变化', '重新检查页面验证状态后再重试');
|
|
1792
|
+
}
|
|
1793
|
+
const viewport = await withNavigationBudget(() => this.readViewportMetrics(0), deadline, signal);
|
|
1794
|
+
const refreshedTopology = await withNavigationBudget(() => this.listTopology(remaining()), deadline, signal);
|
|
1795
|
+
const refreshedWindow = refreshedTopology?.windows.find((item) => item.tabs.some((candidate) => String(candidate.id) === targetId || candidate.targetId === targetId));
|
|
1796
|
+
const refreshedTab = refreshedWindow?.tabs.find((candidate) => String(candidate.id) === targetId || candidate.targetId === targetId);
|
|
1797
|
+
const refreshedActiveTabId = refreshedWindow?.activeTabId;
|
|
1798
|
+
if (!refreshedWindow?.focused ||
|
|
1799
|
+
refreshedTab?.managed !== true ||
|
|
1800
|
+
(refreshedTab.active !== true &&
|
|
1801
|
+
(refreshedActiveTabId === undefined || String(refreshedActiveTabId) !== targetId))) {
|
|
1802
|
+
throw new OsMouseError('TURNSTILE_OS_CLICK_UNAVAILABLE', '系统鼠标点击前,受控测试页的窗口或活动 tab 已变化', '重新聚焦受控测试页并保持为活动 tab 后重试');
|
|
1803
|
+
}
|
|
1804
|
+
const frameOffset = this.currentFrameOffset;
|
|
1805
|
+
const clickPoint = frameOffset
|
|
1806
|
+
? { x: current.clickPoint.x + frameOffset.x, y: current.clickPoint.y + frameOffset.y }
|
|
1807
|
+
: current.clickPoint;
|
|
1808
|
+
const estimate = await withNavigationBudget(() => resolveTurnstileOsClickPoint({ ...current, clickPoint, screenPoint: null, viewport }, { deadline, signal }), deadline, signal);
|
|
1809
|
+
if (!pointerId || !probe) {
|
|
1810
|
+
return estimate;
|
|
1811
|
+
}
|
|
1812
|
+
const request = await probe();
|
|
1813
|
+
const origin = (await withNavigationBudget(() => this.extensionBridge.getViewportMetrics(0, { operation: 'read', id: pointerId, documentId: pointerDocumentId, ...request }, remaining()), deadline, signal));
|
|
1814
|
+
if (typeof origin.x !== 'number' ||
|
|
1815
|
+
!Number.isFinite(origin.x) ||
|
|
1816
|
+
typeof origin.y !== 'number' ||
|
|
1817
|
+
!Number.isFinite(origin.y)) {
|
|
1818
|
+
throw new OsMouseError('TURNSTILE_OS_CLICK_UNAVAILABLE', '未取得可信原生指针原点', '重新检查当前受控页面');
|
|
1819
|
+
}
|
|
1820
|
+
return {
|
|
1821
|
+
x: Math.round(origin.x + clickPoint.x * viewport.devicePixelRatio),
|
|
1822
|
+
y: Math.round(origin.y + clickPoint.y * viewport.devicePixelRatio),
|
|
1823
|
+
};
|
|
1824
|
+
};
|
|
1825
|
+
let clickError;
|
|
1826
|
+
try {
|
|
1827
|
+
if (pointerId) {
|
|
1828
|
+
const started = (await withNavigationBudget(() => this.extensionBridge.getViewportMetrics(0, { operation: 'start', id: pointerId }, remaining()), deadline, signal));
|
|
1829
|
+
pointerDocumentId = started.documentId;
|
|
1830
|
+
if (!pointerDocumentId) {
|
|
1831
|
+
throw new OsMouseError('TURNSTILE_OS_CLICK_UNAVAILABLE', '原生指针观察器未取得文档身份', '重新检查当前受控页面');
|
|
1832
|
+
}
|
|
1833
|
+
}
|
|
1834
|
+
const screen = await resolvePoint(false);
|
|
1835
|
+
// 请求取消后仍须等待必要的鼠标释放完成,再结束 tab 锁
|
|
1836
|
+
await osLeftClick(screen.x, screen.y, {
|
|
1837
|
+
deadline,
|
|
1838
|
+
signal,
|
|
1839
|
+
refreshPoint: (probe) => resolvePoint(true, probe),
|
|
1840
|
+
});
|
|
1841
|
+
}
|
|
1842
|
+
catch (error) {
|
|
1843
|
+
clickError = { error };
|
|
1844
|
+
}
|
|
1845
|
+
if (pointerId) {
|
|
1846
|
+
try {
|
|
1847
|
+
await this.tabLockContext.run({
|
|
1848
|
+
...scope,
|
|
1849
|
+
session: this,
|
|
1850
|
+
signal: undefined,
|
|
1851
|
+
deadline: Date.now() + 5000,
|
|
1852
|
+
budgetError: undefined,
|
|
1853
|
+
userAction: undefined,
|
|
1854
|
+
pendingPageCondition: null,
|
|
1855
|
+
keyboardFocus: undefined,
|
|
1856
|
+
inputCleanup: undefined,
|
|
1857
|
+
}, () => this.extensionBridge.getViewportMetrics(0, { operation: 'stop', id: pointerId }, 5000));
|
|
1858
|
+
}
|
|
1859
|
+
catch (error) {
|
|
1860
|
+
console.warn('[MCP] Native pointer observer cleanup failed:', error);
|
|
1861
|
+
if (clickError?.error instanceof Error) {
|
|
1862
|
+
Object.assign(clickError.error, { pointerCleanupError: error });
|
|
1863
|
+
}
|
|
1864
|
+
clickError ??= { error };
|
|
1865
|
+
}
|
|
1866
|
+
}
|
|
1867
|
+
if (clickError) {
|
|
1868
|
+
throw clickError.error;
|
|
1869
|
+
}
|
|
1319
1870
|
}
|
|
1320
1871
|
// noinspection JSUnusedGlobalSymbols — resolveChallenge 通过 ChallengeHost 结构类型调用
|
|
1321
1872
|
async inspectChallenge(options, timeout) {
|
|
@@ -1323,16 +1874,41 @@ class UnifiedSessionManager {
|
|
|
1323
1874
|
throw new Error('challenge_inspect 仅在 Extension 模式可用');
|
|
1324
1875
|
}
|
|
1325
1876
|
const deadline = timeout === undefined ? undefined : Date.now() + timeout;
|
|
1326
|
-
await withNavigationBudget(() => this.
|
|
1327
|
-
|
|
1877
|
+
const state = await withNavigationBudget(() => this.getLiveState(), deadline);
|
|
1878
|
+
const title = (state?.title ?? '').trim().toLowerCase().replace(/…/g, '...');
|
|
1879
|
+
const challengeTitle = options.titleNeedles.some((needle) => title.includes(needle.toLowerCase()));
|
|
1880
|
+
const detach = () => withNavigationBudget(() => this.extensionBridge.debuggerDetach(undefined, deadline === undefined ? undefined : remainingNavigationTimeout(deadline)), deadline);
|
|
1881
|
+
if (challengeTitle) {
|
|
1882
|
+
await detach();
|
|
1883
|
+
}
|
|
1884
|
+
const snapshot = await this.extensionBridge.inspectChallenge(options, deadline === undefined ? undefined : remainingNavigationTimeout(deadline));
|
|
1885
|
+
if (!challengeTitle && snapshot && typeof snapshot === 'object') {
|
|
1886
|
+
const evidence = snapshot;
|
|
1887
|
+
if (evidence.turnstilePresent ||
|
|
1888
|
+
(!evidence.originResponsePending &&
|
|
1889
|
+
((evidence.selectorHits?.length ?? 0) > 0 || (evidence.textHits?.length ?? 0) > 0))) {
|
|
1890
|
+
await detach();
|
|
1891
|
+
}
|
|
1892
|
+
}
|
|
1893
|
+
return snapshot;
|
|
1328
1894
|
}
|
|
1329
1895
|
/**
|
|
1330
1896
|
* 鼠标滚轮
|
|
1331
1897
|
*/
|
|
1332
|
-
async mouseWheel(deltaX, deltaY) {
|
|
1333
|
-
|
|
1334
|
-
const
|
|
1335
|
-
|
|
1898
|
+
async mouseWheel(deltaX, deltaY, timeout, point) {
|
|
1899
|
+
this.assertRequestActive();
|
|
1900
|
+
const deadline = timeout === undefined ? undefined : Date.now() + timeout;
|
|
1901
|
+
const remaining = () => (deadline === undefined ? undefined : remainingNavigationTimeout(deadline));
|
|
1902
|
+
const driver = await this.getDriver(remaining());
|
|
1903
|
+
this.assertRequestActive();
|
|
1904
|
+
const { x, y } = point ?? this.currentMousePosition;
|
|
1905
|
+
const dispatch = () => driver.inputMouse('mouseWheel', x, y, { deltaX, deltaY, modifiers: this.modifiers }, remaining());
|
|
1906
|
+
if (point) {
|
|
1907
|
+
await dispatch();
|
|
1908
|
+
}
|
|
1909
|
+
else {
|
|
1910
|
+
await this.withMouseFrame(dispatch);
|
|
1911
|
+
}
|
|
1336
1912
|
}
|
|
1337
1913
|
/**
|
|
1338
1914
|
* 注入反检测脚本
|
|
@@ -1434,17 +2010,29 @@ class UnifiedSessionManager {
|
|
|
1434
2010
|
metadataKey(targetId) {
|
|
1435
2011
|
return `${this.getMode()}:${targetId}`;
|
|
1436
2012
|
}
|
|
2013
|
+
rememberPageMetadata(targetId, metadata) {
|
|
2014
|
+
const lastUsedAt = this.getPageMetadata(targetId).lastUsedAt;
|
|
2015
|
+
this.pageMetadata.set(this.metadataKey(targetId), {
|
|
2016
|
+
...metadata,
|
|
2017
|
+
lastUsedAt: lastUsedAt && lastUsedAt > (metadata.lastUsedAt ?? '') ? lastUsedAt : metadata.lastUsedAt,
|
|
2018
|
+
});
|
|
2019
|
+
}
|
|
1437
2020
|
recordPageUse(targetId, created = false) {
|
|
1438
2021
|
const metadata = this.getPageMetadata(targetId);
|
|
1439
2022
|
this.pageMetadata.set(this.metadataKey(targetId), {
|
|
1440
2023
|
...metadata,
|
|
2024
|
+
origin: created ? 'created' : metadata.origin,
|
|
1441
2025
|
createdBy: created ? 'mcp-chrome' : metadata.createdBy,
|
|
1442
2026
|
createdAt: created ? new Date().toISOString() : metadata.createdAt,
|
|
1443
2027
|
lastUsedAt: new Date().toISOString(),
|
|
1444
2028
|
});
|
|
1445
2029
|
}
|
|
1446
|
-
evaluateViaCdp(script, args, timeout) {
|
|
1447
|
-
|
|
2030
|
+
evaluateViaCdp(script, args, timeout, options) {
|
|
2031
|
+
const session = getCdpSession();
|
|
2032
|
+
if (session.getCurrentFrameId() !== 0 && options.staleContextRetry === 'readOnly') {
|
|
2033
|
+
return Promise.reject(new StructuredToolError('DRIVER_CAPABILITY_ERROR', 'CDP iframe 暂不支持 readOnly context 重试,脚本未执行', '使用 Extension precise 执行无副作用脚本,或显式使用 never 并独立核对结果', { actionExecuted: false, actionStatus: 'not_started', failureStage: 'context', retryable: false }));
|
|
2034
|
+
}
|
|
2035
|
+
return session.evaluate(script, args, timeout, options.userAction);
|
|
1448
2036
|
}
|
|
1449
2037
|
async evaluateViaExtensionPrecise(code, expression, args, timeout, options = {}) {
|
|
1450
2038
|
const hasArgs = args !== undefined;
|
|
@@ -1461,7 +2049,7 @@ class UnifiedSessionManager {
|
|
|
1461
2049
|
const argsStr = args.map((a) => JSON.stringify(a)).join(', ');
|
|
1462
2050
|
iframeExpression = `(${code})(${argsStr})`;
|
|
1463
2051
|
}
|
|
1464
|
-
const result = (await this.extensionBridge.evaluateInFrame(currentFrameId, iframeExpression, timeout, staleContextRetry));
|
|
2052
|
+
const result = (await this.extensionBridge.evaluateInFrame(currentFrameId, iframeExpression, timeout, staleContextRetry, options.userAction));
|
|
1465
2053
|
if (result.frameContext) {
|
|
1466
2054
|
this.lastEvaluationMetadata = {
|
|
1467
2055
|
retryAttempted: result.retryAttempted === true,
|
|
@@ -1480,14 +2068,14 @@ class UnifiedSessionManager {
|
|
|
1480
2068
|
if (timeout !== undefined) {
|
|
1481
2069
|
params.timeout = timeout;
|
|
1482
2070
|
}
|
|
1483
|
-
const result = (await this.extensionBridge.debuggerSend('Runtime.evaluate', params, undefined, timeout));
|
|
2071
|
+
const result = (await this.extensionBridge.debuggerSend('Runtime.evaluate', params, undefined, timeout, options.userAction));
|
|
1484
2072
|
if (result.exceptionDetails) {
|
|
1485
|
-
throw new Error(formatCdpException(result.exceptionDetails));
|
|
2073
|
+
throw setErrorSource(new Error(formatCdpException(result.exceptionDetails)), 'page');
|
|
1486
2074
|
}
|
|
1487
2075
|
return this.materializeCdpResult(result.result, timeout, options.fileOutput);
|
|
1488
2076
|
}
|
|
1489
|
-
async evaluateViaExtensionStealth(expression, timeout) {
|
|
1490
|
-
return (await this.extensionBridge.evaluate(expression, undefined, timeout));
|
|
2077
|
+
async evaluateViaExtensionStealth(expression, timeout, userAction) {
|
|
2078
|
+
return (await this.extensionBridge.evaluate(expression, undefined, timeout, userAction));
|
|
1491
2079
|
}
|
|
1492
2080
|
async readViewportMetrics(frameId) {
|
|
1493
2081
|
let raw;
|
|
@@ -1516,7 +2104,8 @@ class UnifiedSessionManager {
|
|
|
1516
2104
|
}
|
|
1517
2105
|
catch (err) {
|
|
1518
2106
|
const msg = err instanceof Error ? err.message : String(err);
|
|
1519
|
-
if (
|
|
2107
|
+
if (!isNativeExecutionResultUnknown(err) &&
|
|
2108
|
+
(isExtensionDisconnected(err) || msg.includes('not connected'))) {
|
|
1520
2109
|
// 等待 Extension 重连
|
|
1521
2110
|
await new Promise((r) => setTimeout(r, 2000));
|
|
1522
2111
|
if (this.isExtensionConnected()) {
|
|
@@ -1553,7 +2142,7 @@ class UnifiedSessionManager {
|
|
|
1553
2142
|
*/
|
|
1554
2143
|
checkCdpResult(result) {
|
|
1555
2144
|
if (result.exceptionDetails) {
|
|
1556
|
-
throw new Error(formatCdpException(result.exceptionDetails));
|
|
2145
|
+
throw setErrorSource(new Error(formatCdpException(result.exceptionDetails)), 'page');
|
|
1557
2146
|
}
|
|
1558
2147
|
return extractCdpValue(result.result);
|
|
1559
2148
|
}
|
|
@@ -1592,13 +2181,27 @@ class UnifiedSessionManager {
|
|
|
1592
2181
|
generatePreview: false,
|
|
1593
2182
|
}, undefined, timeout));
|
|
1594
2183
|
if (properties.exceptionDetails) {
|
|
1595
|
-
throw new Error(formatCdpException(properties.exceptionDetails));
|
|
2184
|
+
throw setErrorSource(new Error(formatCdpException(properties.exceptionDetails)), 'page');
|
|
1596
2185
|
}
|
|
1597
2186
|
const descriptors = properties.result ?? [];
|
|
1598
2187
|
state.nodes += descriptors.length;
|
|
1599
2188
|
this.assertMaterializeBudget(state, depth, result);
|
|
1600
2189
|
if (result.subtype === 'array') {
|
|
1601
|
-
const
|
|
2190
|
+
const length = descriptors.find((descriptor) => descriptor.name === 'length')?.value?.value;
|
|
2191
|
+
if (typeof length !== 'number' ||
|
|
2192
|
+
!Number.isInteger(length) ||
|
|
2193
|
+
length < 0 ||
|
|
2194
|
+
length > UnifiedSessionManager.CDP_MATERIALIZE_MAX_NODES) {
|
|
2195
|
+
this.throwMaterializeLimit(state, depth, result);
|
|
2196
|
+
}
|
|
2197
|
+
const populated = descriptors.filter((descriptor) => /^\d+$/.test(descriptor.name) &&
|
|
2198
|
+
Number(descriptor.name) < length &&
|
|
2199
|
+
descriptor.value).length;
|
|
2200
|
+
const holes = length - populated;
|
|
2201
|
+
state.nodes += holes;
|
|
2202
|
+
state.chars += holes * 5;
|
|
2203
|
+
this.assertMaterializeBudget(state, depth, result);
|
|
2204
|
+
const array = new Array(length);
|
|
1602
2205
|
for (const descriptor of descriptors) {
|
|
1603
2206
|
if (!descriptor.value || !/^\d+$/.test(descriptor.name)) {
|
|
1604
2207
|
continue;
|
|
@@ -1618,7 +2221,12 @@ class UnifiedSessionManager {
|
|
|
1618
2221
|
continue;
|
|
1619
2222
|
}
|
|
1620
2223
|
this.trackMaterializedPropertyName(descriptor.name, state, depth, result);
|
|
1621
|
-
object
|
|
2224
|
+
Object.defineProperty(object, descriptor.name, {
|
|
2225
|
+
value: await this.materializePropertyValue(descriptor.value, timeout, state, depth + 1),
|
|
2226
|
+
enumerable: true,
|
|
2227
|
+
configurable: true,
|
|
2228
|
+
writable: true,
|
|
2229
|
+
});
|
|
1622
2230
|
}
|
|
1623
2231
|
return object;
|
|
1624
2232
|
}
|
|
@@ -1717,6 +2325,20 @@ class UnifiedSessionManager {
|
|
|
1717
2325
|
* 避免工具 timeout 被连接等待吞掉,不传则使用默认 30s
|
|
1718
2326
|
*/
|
|
1719
2327
|
async getDriver(timeout) {
|
|
2328
|
+
this.assertRequestActive();
|
|
2329
|
+
const scopedMode = this.tabLockContext.getStore()?.mode;
|
|
2330
|
+
if (scopedMode === 'extension') {
|
|
2331
|
+
if (!this.extensionBridge?.isConnected()) {
|
|
2332
|
+
throw new DriverCapabilityError('Extension 已断开,当前请求保留原后端,未改用 CDP');
|
|
2333
|
+
}
|
|
2334
|
+
return this.extensionBridge;
|
|
2335
|
+
}
|
|
2336
|
+
if (scopedMode === 'cdp') {
|
|
2337
|
+
if (!getCdpSession().isConnected()) {
|
|
2338
|
+
throw new DriverCapabilityError('CDP 已断开,当前请求保留原后端,未改用 Extension');
|
|
2339
|
+
}
|
|
2340
|
+
return getCdpSession();
|
|
2341
|
+
}
|
|
1720
2342
|
if (this.activeMode === 'cdp' && this.cdpExplicitlySelected && getCdpSession().isConnected()) {
|
|
1721
2343
|
this.assertCdpFallbackAllowed();
|
|
1722
2344
|
return getCdpSession();
|
|
@@ -1731,6 +2353,7 @@ class UnifiedSessionManager {
|
|
|
1731
2353
|
return getCdpSession();
|
|
1732
2354
|
}
|
|
1733
2355
|
async ensureExtensionConnected(maxWait) {
|
|
2356
|
+
this.assertRequestActive();
|
|
1734
2357
|
if (!this.extensionBridge) {
|
|
1735
2358
|
return this.assertCdpFallbackAllowed();
|
|
1736
2359
|
}
|
|
@@ -1759,7 +2382,9 @@ class UnifiedSessionManager {
|
|
|
1759
2382
|
}
|
|
1760
2383
|
console.error(`[MCP] Waiting for Chrome Extension connection (${waitTimeout}ms timeout)...`);
|
|
1761
2384
|
console.error('[MCP] Please ensure Chrome is running with MCP Chrome extension installed.');
|
|
1762
|
-
const
|
|
2385
|
+
const scope = this.tabLockContext.getStore();
|
|
2386
|
+
const connected = await withNavigationBudget(() => this.extensionBridge.waitForConnection(waitTimeout), scope?.deadline, scope?.signal);
|
|
2387
|
+
this.assertRequestActive();
|
|
1763
2388
|
if (connected) {
|
|
1764
2389
|
console.error('[MCP] Chrome Extension connected successfully');
|
|
1765
2390
|
this.lastConnectionFailure = 0;
|
|
@@ -1799,9 +2424,9 @@ class UnifiedSessionManager {
|
|
|
1799
2424
|
if (timeout !== undefined) {
|
|
1800
2425
|
params.timeout = timeout;
|
|
1801
2426
|
}
|
|
1802
|
-
const result = (await this.extensionBridge.debuggerSend('Runtime.callFunctionOn', params, undefined, timeout));
|
|
2427
|
+
const result = (await this.extensionBridge.debuggerSend('Runtime.callFunctionOn', params, undefined, timeout, options.userAction));
|
|
1803
2428
|
if (result.exceptionDetails) {
|
|
1804
|
-
throw new Error(formatCdpException(result.exceptionDetails));
|
|
2429
|
+
throw setErrorSource(new Error(formatCdpException(result.exceptionDetails)), 'page');
|
|
1805
2430
|
}
|
|
1806
2431
|
return await this.materializeCdpResult(result.result, timeout, options.fileOutput);
|
|
1807
2432
|
}
|
|
@@ -1811,6 +2436,34 @@ class UnifiedSessionManager {
|
|
|
1811
2436
|
}).catch(() => { });
|
|
1812
2437
|
}
|
|
1813
2438
|
}
|
|
2439
|
+
async withMouseFrame(fn) {
|
|
2440
|
+
const bridge = this.isExtensionConnected() ? this.extensionBridge : null;
|
|
2441
|
+
const original = this.mouseFrameContext?.targetId === this.getCurrentTargetId() ? this.mouseFrameContext : undefined;
|
|
2442
|
+
if (!bridge || !original) {
|
|
2443
|
+
return fn();
|
|
2444
|
+
}
|
|
2445
|
+
const frameId = bridge.getCurrentFrameId();
|
|
2446
|
+
const documentId = bridge.getCurrentFrameDocumentId();
|
|
2447
|
+
bridge.setCurrentFrameId(original.frameId, original.documentId);
|
|
2448
|
+
try {
|
|
2449
|
+
return await fn();
|
|
2450
|
+
}
|
|
2451
|
+
finally {
|
|
2452
|
+
bridge.setCurrentFrameId(frameId, documentId);
|
|
2453
|
+
}
|
|
2454
|
+
}
|
|
2455
|
+
rememberMouseFrame() {
|
|
2456
|
+
const bridge = this.isExtensionConnected() ? this.extensionBridge : null;
|
|
2457
|
+
const targetId = bridge?.getCurrentTargetId();
|
|
2458
|
+
this.mouseFrameContext =
|
|
2459
|
+
bridge && targetId
|
|
2460
|
+
? {
|
|
2461
|
+
targetId,
|
|
2462
|
+
frameId: bridge.getCurrentFrameId(),
|
|
2463
|
+
documentId: bridge.getCurrentFrameDocumentId(),
|
|
2464
|
+
}
|
|
2465
|
+
: undefined;
|
|
2466
|
+
}
|
|
1814
2467
|
}
|
|
1815
2468
|
/**
|
|
1816
2469
|
* 获取统一会话管理器实例
|