@pyrokine/mcp-chrome 3.0.1 → 3.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1060 -432
- package/README_zh.md +936 -349
- package/dist/cdp/client.d.ts +8 -3
- package/dist/cdp/client.d.ts.map +1 -1
- package/dist/cdp/client.js +260 -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 +73 -15
- 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 +105 -0
- package/dist/core/browser-events.d.ts.map +1 -0
- package/dist/core/browser-events.js +1482 -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 +231 -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/error-sanitizer.d.ts.map +1 -1
- package/dist/core/error-sanitizer.js +2 -1
- package/dist/core/error-sanitizer.js.map +1 -1
- package/dist/core/errors.d.ts +4 -0
- package/dist/core/errors.d.ts.map +1 -1
- package/dist/core/errors.js +57 -9
- 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 +11 -2
- package/dist/core/locator.d.ts.map +1 -1
- package/dist/core/locator.js +135 -206
- 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 +104 -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 +62 -12
- package/dist/core/session.d.ts.map +1 -1
- package/dist/core/session.js +1174 -304
- 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 +48 -37
- package/dist/core/unified-session.d.ts.map +1 -1
- package/dist/core/unified-session.js +982 -294
- package/dist/core/unified-session.js.map +1 -1
- package/dist/core/utils.d.ts +8 -4
- package/dist/core/utils.d.ts.map +1 -1
- package/dist/core/utils.js +76 -18
- package/dist/core/utils.js.map +1 -1
- package/dist/extension/bridge.d.ts +48 -17
- package/dist/extension/bridge.d.ts.map +1 -1
- package/dist/extension/bridge.js +288 -46
- package/dist/extension/bridge.js.map +1 -1
- package/dist/extension/http-server.d.ts +8 -2
- package/dist/extension/http-server.d.ts.map +1 -1
- package/dist/extension/http-server.js +211 -49
- package/dist/extension/http-server.js.map +1 -1
- package/dist/index.js +15 -1
- package/dist/index.js.map +1 -1
- package/dist/tools/browse.d.ts.map +1 -1
- package/dist/tools/browse.js +56 -28
- 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 +18 -5
- package/dist/tools/challenge.d.ts.map +1 -1
- package/dist/tools/challenge.js +217 -200
- package/dist/tools/challenge.js.map +1 -1
- package/dist/tools/cookies.d.ts.map +1 -1
- package/dist/tools/cookies.js +34 -8
- package/dist/tools/cookies.js.map +1 -1
- package/dist/tools/diagnostics.d.ts.map +1 -1
- package/dist/tools/diagnostics.js +15 -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 +94 -52
- package/dist/tools/evaluate.js.map +1 -1
- package/dist/tools/extract.d.ts.map +1 -1
- package/dist/tools/extract.js +293 -153
- package/dist/tools/extract.js.map +1 -1
- package/dist/tools/input.d.ts.map +1 -1
- package/dist/tools/input.js +747 -282
- 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 +123 -18
- package/dist/tools/manage.js.map +1 -1
- package/dist/tools/network-sanitizer.d.ts +2 -0
- package/dist/tools/network-sanitizer.d.ts.map +1 -1
- package/dist/tools/network-sanitizer.js +53 -10
- 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 +57 -14
- package/dist/tools/page-context.js.map +1 -1
- package/dist/tools/png.d.ts.map +1 -1
- package/dist/tools/png.js +12 -3
- package/dist/tools/png.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 +163 -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 +3 -3
- package/dist/tools/target-diagnostics.d.ts.map +1 -1
- package/dist/tools/target-diagnostics.js +64 -34
- package/dist/tools/target-diagnostics.js.map +1 -1
- package/dist/tools/wait.d.ts.map +1 -1
- package/dist/tools/wait.js +94 -40
- package/dist/tools/wait.js.map +1 -1
- package/extension/dist/assets/{content.ts-qe6IB5DK.js → content.ts-C9QgtUYe.js} +1 -1
- package/extension/dist/assets/{content.ts-loader-DUWbDHRS.js → content.ts-loader-yqB_P8vu.js} +1 -1
- package/extension/dist/assets/index.ts-DBFNh8mZ.js +20275 -0
- package/extension/dist/assets/{page-scripts-D508WLkW.js → page-scripts-BjqQ0zXt.js} +258 -367
- package/extension/dist/assets/{popup.html-B-eVq48O.js → popup.html-DwwHo00N.js} +2 -2
- 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 +9 -7
- package/scripts/start-chrome.ps1 +9 -1
- package/extension/dist/assets/index.ts-D7_LCUoV.js +0 -9319
|
@@ -6,15 +6,19 @@
|
|
|
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 { setTimeout as delay } from 'node:timers/promises';
|
|
11
|
+
import { Script } from 'node:vm';
|
|
9
12
|
import { ExtensionBridge } from '../extension/index.js';
|
|
10
13
|
import { DriverCapabilityError, } from './browser-driver.js';
|
|
11
|
-
import {
|
|
14
|
+
import { eventRefTarget } from './browser-events.js';
|
|
15
|
+
import { inspectChallengePage } from './challenge-page.js';
|
|
16
|
+
import { getErrorPageContext, getErrorSource, isNativeExecutionResultUnknown, OsMouseError, PagePreconditionError, setErrorPageContext, setErrorSource, StructuredToolError, TargetNotFoundError, TimeoutError, } from './errors.js';
|
|
12
17
|
import { isExtensionDisconnected } from './extension-errors.js';
|
|
13
18
|
import { asViewportMetrics, osLeftClick, resolveTurnstileOsClickPoint, } from './os-mouse.js';
|
|
14
19
|
import { getKeyDefinition, getSession as getCdpSession } from './session.js';
|
|
15
|
-
import { DEFAULT_TIMEOUT } from './types.js';
|
|
20
|
+
import { DEFAULT_TIMEOUT, EvaluateResultTooLargeError, extractCdpValue, formatCdpException, isKnownNonSerializableRemoteObject, MODIFIER_KEYS, NonSerializableEvaluateResultError, } from './types.js';
|
|
16
21
|
import { remainingNavigationTimeout, waitForNavigationQueue, withNavigationBudget } from './utils.js';
|
|
17
|
-
import { EvaluateResultTooLargeError, extractCdpValue, formatCdpException, isKnownNonSerializableRemoteObject, MODIFIER_KEYS, NonSerializableEvaluateResultError, } from './types.js';
|
|
18
22
|
class UnifiedSessionManager {
|
|
19
23
|
static instance;
|
|
20
24
|
static CONNECTION_COOLDOWN = 30000; // 连接失败后 30 秒内不重试
|
|
@@ -25,6 +29,7 @@ class UnifiedSessionManager {
|
|
|
25
29
|
extensionBridge = null;
|
|
26
30
|
inputMode = 'precise'; // 默认使用 precise 模式,可绕过 CSP 限制
|
|
27
31
|
currentMousePosition = { x: 0, y: 0 }; // 跟踪鼠标位置
|
|
32
|
+
mouseFrameContext;
|
|
28
33
|
/** 当前按下的修饰键位掩码 */
|
|
29
34
|
modifiers = 0;
|
|
30
35
|
/** 当前按下的所有键(用于 Puppeteer 风格的 rawKeyDown/autoRepeat 长按重复) */
|
|
@@ -40,7 +45,9 @@ class UnifiedSessionManager {
|
|
|
40
45
|
currentFrameOffset = null; // iframe 在主页面的偏移量(withFrame 期间有效)
|
|
41
46
|
lastEvaluationMetadata;
|
|
42
47
|
pageMetadata = new Map();
|
|
43
|
-
constructor() {
|
|
48
|
+
constructor() {
|
|
49
|
+
getCdpSession().requestContext = () => this.tabLockContext.getStore();
|
|
50
|
+
}
|
|
44
51
|
static getInstance() {
|
|
45
52
|
if (!UnifiedSessionManager.instance) {
|
|
46
53
|
UnifiedSessionManager.instance = new UnifiedSessionManager();
|
|
@@ -54,7 +61,7 @@ class UnifiedSessionManager {
|
|
|
54
61
|
if (this.extensionBridge) {
|
|
55
62
|
return; // 已经启动
|
|
56
63
|
}
|
|
57
|
-
this.extensionBridge = new ExtensionBridge();
|
|
64
|
+
this.extensionBridge = new ExtensionBridge({ requestContext: () => this.tabLockContext.getStore() });
|
|
58
65
|
try {
|
|
59
66
|
await this.extensionBridge.start();
|
|
60
67
|
console.error(`[MCP] Extension HTTP server listening on port ${this.extensionBridge.getPort()}`);
|
|
@@ -114,6 +121,7 @@ class UnifiedSessionManager {
|
|
|
114
121
|
}
|
|
115
122
|
getPageMetadata(targetId) {
|
|
116
123
|
return (this.pageMetadata.get(this.metadataKey(targetId)) ?? {
|
|
124
|
+
origin: 'unknown',
|
|
117
125
|
createdBy: 'unknown',
|
|
118
126
|
createdAt: null,
|
|
119
127
|
lastUsedAt: null,
|
|
@@ -121,6 +129,15 @@ class UnifiedSessionManager {
|
|
|
121
129
|
}
|
|
122
130
|
async setPageMetadata(targetId, updates) {
|
|
123
131
|
await this.getPageSnapshot(targetId);
|
|
132
|
+
if (this.getMode() === 'extension') {
|
|
133
|
+
const driver = await this.getDriver();
|
|
134
|
+
if (!driver.setPageMetadata) {
|
|
135
|
+
throw new DriverCapabilityError('Extension 不支持持久页面备注,请成套更新 Server 和 Extension');
|
|
136
|
+
}
|
|
137
|
+
const metadata = await driver.setPageMetadata(targetId, updates);
|
|
138
|
+
this.pageMetadata.set(this.metadataKey(targetId), metadata);
|
|
139
|
+
return metadata;
|
|
140
|
+
}
|
|
124
141
|
const metadata = { ...this.getPageMetadata(targetId) };
|
|
125
142
|
if (updates.taskHint !== undefined) {
|
|
126
143
|
if (updates.taskHint === null) {
|
|
@@ -147,6 +164,25 @@ class UnifiedSessionManager {
|
|
|
147
164
|
getFrameOffset() {
|
|
148
165
|
return this.currentFrameOffset;
|
|
149
166
|
}
|
|
167
|
+
async getPageScrollOffset(timeout) {
|
|
168
|
+
const deadline = Math.min(Date.now() + (timeout ?? DEFAULT_TIMEOUT), this.tabLockContext.getStore()?.deadline ?? Infinity);
|
|
169
|
+
const driver = await this.getDriver(remainingNavigationTimeout(deadline));
|
|
170
|
+
const previousFrame = driver.getCurrentFrameId();
|
|
171
|
+
const extension = driver === this.extensionBridge ? this.extensionBridge : undefined;
|
|
172
|
+
const previousDocument = extension?.getCurrentFrameDocumentId();
|
|
173
|
+
driver.setCurrentFrameId(0);
|
|
174
|
+
try {
|
|
175
|
+
return await this.evaluate('(() => ({ x: window.scrollX || 0, y: window.scrollY || 0 }))()', undefined, remainingNavigationTimeout(deadline));
|
|
176
|
+
}
|
|
177
|
+
finally {
|
|
178
|
+
if (extension) {
|
|
179
|
+
extension.setCurrentFrameId(previousFrame, previousDocument);
|
|
180
|
+
}
|
|
181
|
+
else {
|
|
182
|
+
driver.setCurrentFrameId(previousFrame);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
}
|
|
150
186
|
consumeLastEvaluationMetadata() {
|
|
151
187
|
const metadata = this.lastEvaluationMetadata;
|
|
152
188
|
this.lastEvaluationMetadata = undefined;
|
|
@@ -190,7 +226,19 @@ class UnifiedSessionManager {
|
|
|
190
226
|
* 启动浏览器(CDP 模式)或等待 Extension 连接
|
|
191
227
|
*/
|
|
192
228
|
async launch(options = {}) {
|
|
193
|
-
|
|
229
|
+
this.assertRequestActive();
|
|
230
|
+
const explicitProcess = [
|
|
231
|
+
options.port,
|
|
232
|
+
options.executablePath,
|
|
233
|
+
options.headless,
|
|
234
|
+
options.userDataDir,
|
|
235
|
+
options.incognito,
|
|
236
|
+
].some((value) => value !== undefined);
|
|
237
|
+
if (explicitProcess && getCdpSession().isConnected()) {
|
|
238
|
+
throw new DriverCapabilityError('显式启动参数要求新的独立浏览器;当前已有 CDP 连接,未关闭或替换现有浏览器');
|
|
239
|
+
}
|
|
240
|
+
const extensionReady = explicitProcess || this.cdpExplicitlySelected ? false : await this.ensureExtensionConnected(options.timeout);
|
|
241
|
+
this.assertRequestActive();
|
|
194
242
|
// 优先检查 Extension 是否已连接,如果断开则等待重连(受 timeout 约束)
|
|
195
243
|
if (extensionReady) {
|
|
196
244
|
// newPage 会设置 currentTabId,需要加锁
|
|
@@ -200,6 +248,9 @@ class UnifiedSessionManager {
|
|
|
200
248
|
this.activeMode = 'extension';
|
|
201
249
|
this.cdpExplicitlySelected = false;
|
|
202
250
|
this.recordPageUse(result.targetId, true);
|
|
251
|
+
if (result.metadata) {
|
|
252
|
+
this.rememberPageMetadata(result.targetId, result.metadata);
|
|
253
|
+
}
|
|
203
254
|
return {
|
|
204
255
|
targetId: result.targetId,
|
|
205
256
|
type: result.type ?? 'page',
|
|
@@ -212,7 +263,7 @@ class UnifiedSessionManager {
|
|
|
212
263
|
// Fallback 到 CDP 模式
|
|
213
264
|
const target = await getCdpSession().launch(options);
|
|
214
265
|
this.activeMode = 'cdp';
|
|
215
|
-
this.cdpExplicitlySelected =
|
|
266
|
+
this.cdpExplicitlySelected = explicitProcess;
|
|
216
267
|
this.recordPageUse(target.targetId, target.targetCreated ?? target.reused !== true);
|
|
217
268
|
return {
|
|
218
269
|
...target,
|
|
@@ -222,20 +273,25 @@ class UnifiedSessionManager {
|
|
|
222
273
|
/**
|
|
223
274
|
* 列出所有页面
|
|
224
275
|
*/
|
|
225
|
-
async listTargets(timeout) {
|
|
276
|
+
async listTargets(timeout, options) {
|
|
226
277
|
const mode = this.getMode();
|
|
227
278
|
if (mode === 'none') {
|
|
228
279
|
return [];
|
|
229
280
|
}
|
|
230
281
|
const deadline = timeout === undefined ? undefined : Date.now() + timeout;
|
|
231
282
|
const driver = await this.getDriver(timeout);
|
|
232
|
-
const targets = await driver.listTargets(deadline === undefined ? undefined : remainingNavigationTimeout(deadline));
|
|
283
|
+
const targets = await driver.listTargets(deadline === undefined ? undefined : remainingNavigationTimeout(deadline), options);
|
|
233
284
|
const liveKeys = new Set(targets.map((t) => `${mode}:${t.targetId ?? String(t.id)}`));
|
|
234
285
|
for (const key of this.pageMetadata.keys()) {
|
|
235
286
|
if (key.startsWith(`${mode}:`) && !liveKeys.has(key)) {
|
|
236
287
|
this.pageMetadata.delete(key);
|
|
237
288
|
}
|
|
238
289
|
}
|
|
290
|
+
for (const target of targets) {
|
|
291
|
+
if (target.metadata) {
|
|
292
|
+
this.rememberPageMetadata(target.targetId ?? String(target.id), target.metadata);
|
|
293
|
+
}
|
|
294
|
+
}
|
|
239
295
|
return targets.map(({ id, ...target }) => ({
|
|
240
296
|
...target,
|
|
241
297
|
targetId: target.targetId ?? String(id),
|
|
@@ -245,7 +301,7 @@ class UnifiedSessionManager {
|
|
|
245
301
|
metadata: this.getPageMetadata(target.targetId ?? String(id)),
|
|
246
302
|
}));
|
|
247
303
|
}
|
|
248
|
-
async listTopology(timeout) {
|
|
304
|
+
async listTopology(timeout, options) {
|
|
249
305
|
const mode = this.getMode();
|
|
250
306
|
if (mode === 'none') {
|
|
251
307
|
return null;
|
|
@@ -255,7 +311,13 @@ class UnifiedSessionManager {
|
|
|
255
311
|
if (!driver.listWindows) {
|
|
256
312
|
return null;
|
|
257
313
|
}
|
|
258
|
-
|
|
314
|
+
const topology = await driver.listWindows(deadline === undefined ? undefined : remainingNavigationTimeout(deadline), options);
|
|
315
|
+
for (const tab of topology.windows.flatMap((window) => window.tabs)) {
|
|
316
|
+
if (tab.metadata) {
|
|
317
|
+
this.rememberPageMetadata(tab.targetId ?? String(tab.id), tab.metadata);
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
return topology;
|
|
259
321
|
}
|
|
260
322
|
async setReadAccess(targetId, allow) {
|
|
261
323
|
const driver = await this.getDriver();
|
|
@@ -311,7 +373,7 @@ class UnifiedSessionManager {
|
|
|
311
373
|
if (!driver.updateGroup) {
|
|
312
374
|
throw new DriverCapabilityError('updateGroup 仅支持 Extension 模式');
|
|
313
375
|
}
|
|
314
|
-
return
|
|
376
|
+
return driver.updateGroup(groupId, options);
|
|
315
377
|
}
|
|
316
378
|
async moveGroup(groupId, options) {
|
|
317
379
|
const driver = await this.getDriver();
|
|
@@ -440,7 +502,7 @@ class UnifiedSessionManager {
|
|
|
440
502
|
*/
|
|
441
503
|
async click(refId) {
|
|
442
504
|
const driver = await this.getDriver();
|
|
443
|
-
await driver.click(refId);
|
|
505
|
+
await driver.click(refId, undefined, this.modifiers);
|
|
444
506
|
}
|
|
445
507
|
/**
|
|
446
508
|
* 带 actionability 检查的点击(Extension 模式)
|
|
@@ -451,21 +513,43 @@ class UnifiedSessionManager {
|
|
|
451
513
|
const driver = await this.getDriver();
|
|
452
514
|
if (point) {
|
|
453
515
|
this.currentMousePosition = { x: point.x, y: point.y };
|
|
516
|
+
this.rememberMouseFrame();
|
|
454
517
|
}
|
|
455
|
-
if (
|
|
456
|
-
|
|
518
|
+
if (this.inputMode === 'stealth' || force) {
|
|
519
|
+
if (button === 'left' && clickCount === 1) {
|
|
520
|
+
return driver.actionableClick(refId, force, undefined, this.modifiers);
|
|
521
|
+
}
|
|
457
522
|
}
|
|
458
523
|
if (!force) {
|
|
459
524
|
const result = await driver.checkActionability(refId);
|
|
460
|
-
if (!result.success)
|
|
525
|
+
if (!result.success) {
|
|
461
526
|
return result;
|
|
527
|
+
}
|
|
528
|
+
if (result.clickPoint) {
|
|
529
|
+
this.currentMousePosition = { ...result.clickPoint };
|
|
530
|
+
this.rememberMouseFrame();
|
|
531
|
+
}
|
|
462
532
|
}
|
|
463
533
|
if (force) {
|
|
464
534
|
const { x, y } = this.currentMousePosition;
|
|
465
|
-
await driver.stealthClick(x, y, button, clickCount, refId);
|
|
535
|
+
await driver.stealthClick(x, y, button, clickCount, refId, undefined, this.modifiers);
|
|
466
536
|
}
|
|
467
537
|
else {
|
|
468
|
-
|
|
538
|
+
try {
|
|
539
|
+
await this.mouseClick(button, clickCount, refId);
|
|
540
|
+
}
|
|
541
|
+
catch (error) {
|
|
542
|
+
const code = error?.code;
|
|
543
|
+
if (code !== 'NATIVE_INPUT_UNAVAILABLE' ||
|
|
544
|
+
!this.isExtensionConnected() ||
|
|
545
|
+
button !== 'left' ||
|
|
546
|
+
clickCount !== 1 ||
|
|
547
|
+
this.modifiers !== 0) {
|
|
548
|
+
throw error;
|
|
549
|
+
}
|
|
550
|
+
const result = await driver.actionableClick(refId, false, undefined, this.modifiers);
|
|
551
|
+
return { ...result, warning: '后台元素单击使用合成事件,isTrusted=false,未激活标签页' };
|
|
552
|
+
}
|
|
469
553
|
}
|
|
470
554
|
return { success: true };
|
|
471
555
|
}
|
|
@@ -524,7 +608,7 @@ class UnifiedSessionManager {
|
|
|
524
608
|
* @param _retried 内部重试标记,外部不应传入
|
|
525
609
|
*/
|
|
526
610
|
async evaluate(code, mode, timeout, args, options = {}, _retried = false) {
|
|
527
|
-
|
|
611
|
+
this.assertRequestActive();
|
|
528
612
|
this.lastEvaluationMetadata = undefined;
|
|
529
613
|
const effectiveMode = mode ?? this.inputMode;
|
|
530
614
|
const hasArgs = args !== undefined;
|
|
@@ -535,53 +619,34 @@ class UnifiedSessionManager {
|
|
|
535
619
|
expression = `(${code})(${argsStr})`;
|
|
536
620
|
}
|
|
537
621
|
const cdpScript = hasArgs ? code : expression;
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
if (!this.isExtensionConnected()) {
|
|
542
|
-
this.assertCdpFallbackAllowed();
|
|
543
|
-
return this.evaluateViaCdp(cdpScript, args, timeout);
|
|
544
|
-
}
|
|
622
|
+
if (!_retried && !hasArgs && /\breturn\b/.test(code)) {
|
|
623
|
+
try {
|
|
624
|
+
new Script(code);
|
|
545
625
|
}
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
return this.evaluateViaCdp(cdpScript, args, timeout);
|
|
626
|
+
catch (error) {
|
|
627
|
+
if (error instanceof SyntaxError && /Illegal return statement/.test(error.message)) {
|
|
628
|
+
return this.evaluate(`(() => { ${code} })()`, mode, timeout, undefined, options, true);
|
|
550
629
|
}
|
|
551
630
|
}
|
|
552
|
-
// Extension 路径
|
|
553
|
-
if (effectiveMode === 'precise') {
|
|
554
|
-
return this.evaluateViaExtensionPrecise(code, expression, args, timeout, options);
|
|
555
|
-
}
|
|
556
|
-
return this.evaluateViaExtensionStealth(expression, timeout);
|
|
557
631
|
}
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
if (!_retried && (isExtensionDisconnected(err) || msg.includes('not connected'))) {
|
|
564
|
-
if (remainingTimeout !== undefined && remainingTimeout <= 0) {
|
|
565
|
-
throw err;
|
|
566
|
-
}
|
|
567
|
-
await new Promise((r) => setTimeout(r, 2000));
|
|
568
|
-
if (this.isExtensionConnected()) {
|
|
569
|
-
return this.evaluate(code, mode, remainingTimeout, args, options, true);
|
|
570
|
-
}
|
|
571
|
-
// Extension 重连失败,尝试 CDP fallback
|
|
572
|
-
if (!this.requireExtension) {
|
|
573
|
-
return this.evaluateViaCdp(cdpScript, args, remainingTimeout);
|
|
574
|
-
}
|
|
632
|
+
if (timeout !== undefined) {
|
|
633
|
+
// 轮询上下文:快速失败,端到端预算受控
|
|
634
|
+
if (!this.isExtensionConnected()) {
|
|
635
|
+
this.assertCdpFallbackAllowed();
|
|
636
|
+
return await this.evaluateViaCdp(cdpScript, args, timeout, options);
|
|
575
637
|
}
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
}
|
|
638
|
+
}
|
|
639
|
+
else {
|
|
640
|
+
// 非轮询上下文:允许等待重连;连接失败时回退 CDP
|
|
641
|
+
if (!(await this.ensureExtensionConnected())) {
|
|
642
|
+
return await this.evaluateViaCdp(cdpScript, args, timeout, options);
|
|
582
643
|
}
|
|
583
|
-
throw err;
|
|
584
644
|
}
|
|
645
|
+
// Extension 路径
|
|
646
|
+
if (effectiveMode === 'precise') {
|
|
647
|
+
return await this.evaluateViaExtensionPrecise(code, expression, args, timeout, options);
|
|
648
|
+
}
|
|
649
|
+
return await this.evaluateViaExtensionStealth(expression, timeout, options.userAction);
|
|
585
650
|
}
|
|
586
651
|
/**
|
|
587
652
|
* 获取页面文本
|
|
@@ -604,8 +669,8 @@ class UnifiedSessionManager {
|
|
|
604
669
|
async getComputedStyleByRef(refId, prop) {
|
|
605
670
|
return this.extensionReadBridge().getComputedStyle(refId, prop);
|
|
606
671
|
}
|
|
607
|
-
async findAtPoint(x, y, matchIndex, timeout) {
|
|
608
|
-
return this.extensionReadBridge().find(undefined, undefined, undefined, timeout, { x, y }, matchIndex);
|
|
672
|
+
async findAtPoint(x, y, matchIndex, timeout, coordinateSpace) {
|
|
673
|
+
return this.extensionReadBridge().find(undefined, undefined, undefined, timeout, { x, y }, matchIndex, coordinateSpace);
|
|
609
674
|
}
|
|
610
675
|
async getHtmlWithImagesByRef(refId) {
|
|
611
676
|
return this.extensionReadBridge().getHtmlWithImages(undefined, true, refId);
|
|
@@ -677,22 +742,28 @@ class UnifiedSessionManager {
|
|
|
677
742
|
* @param text 文本内容
|
|
678
743
|
* @param xpath XPath 表达式
|
|
679
744
|
* @param timeout 端到端预算(毫秒),包含连接等待和请求超时,传给 bridge.find → sendCommand
|
|
745
|
+
* @param semanticOrMatchIndex 语义名称条件,或兼容旧调用的匹配序号
|
|
746
|
+
* @param matchIndex 匹配序号,从 0 开始,指定时只返回该位置
|
|
747
|
+
* @param coordinateSpace frame 仅查询当前文档内的元素和矩形,viewport 保留既有根视口转换
|
|
680
748
|
*/
|
|
681
|
-
async find(selector, text, xpath, timeout, matchIndex) {
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
749
|
+
async find(selector, text, xpath, timeout, semanticOrMatchIndex, matchIndex, coordinateSpace) {
|
|
750
|
+
const semantic = typeof semanticOrMatchIndex === 'object' ? semanticOrMatchIndex : undefined;
|
|
751
|
+
matchIndex = typeof semanticOrMatchIndex === 'number' ? semanticOrMatchIndex : matchIndex;
|
|
752
|
+
const coordinates = coordinateSpace === undefined ? [] : [coordinateSpace];
|
|
753
|
+
this.assertRequestActive();
|
|
685
754
|
if (timeout !== undefined) {
|
|
686
755
|
// 轮询上下文:快速失败,端到端预算受控
|
|
687
756
|
if (this.isExtensionConnected()) {
|
|
688
|
-
return this.extensionBridge.find(selector, text, xpath, timeout);
|
|
757
|
+
return this.extensionBridge.find(selector, text, xpath, timeout, semantic, matchIndex, ...coordinates);
|
|
689
758
|
}
|
|
690
759
|
this.assertCdpFallbackAllowed();
|
|
691
|
-
return getCdpSession().find(selector, text, xpath, timeout);
|
|
760
|
+
return getCdpSession().find(selector, text, xpath, timeout, semantic, matchIndex);
|
|
692
761
|
}
|
|
693
762
|
// 非轮询上下文:允许等待重连,断连时 fallback CDP
|
|
694
763
|
const driver = await this.getDriver();
|
|
695
|
-
return this.withExtensionRetry(() => driver
|
|
764
|
+
return this.withExtensionRetry(() => driver === this.extensionBridge
|
|
765
|
+
? this.extensionBridge.find(selector, text, xpath, undefined, semantic, matchIndex, ...coordinates)
|
|
766
|
+
: getCdpSession().find(selector, text, xpath, undefined, semantic, matchIndex), () => getCdpSession().find(selector, text, xpath, undefined, semantic, matchIndex));
|
|
696
767
|
}
|
|
697
768
|
/**
|
|
698
769
|
* 获取元素属性
|
|
@@ -718,8 +789,8 @@ class UnifiedSessionManager {
|
|
|
718
789
|
const driver = await this.getDriver();
|
|
719
790
|
await driver.setCookie(params);
|
|
720
791
|
}
|
|
721
|
-
async deleteCookie(url, name) {
|
|
722
|
-
return (await this.getDriver()).deleteCookie(url, name);
|
|
792
|
+
async deleteCookie(url, name, scope) {
|
|
793
|
+
return (await this.getDriver()).deleteCookie(url, name, scope);
|
|
723
794
|
}
|
|
724
795
|
async clearCookies(filter) {
|
|
725
796
|
if (!filter || (!filter.url && !filter.domain)) {
|
|
@@ -736,6 +807,12 @@ class UnifiedSessionManager {
|
|
|
736
807
|
const op = async () => {
|
|
737
808
|
const result = await driver.newPage(url, timeout, options);
|
|
738
809
|
this.recordPageUse(result.targetId, true);
|
|
810
|
+
if (result.metadata) {
|
|
811
|
+
this.rememberPageMetadata(result.targetId, result.metadata);
|
|
812
|
+
}
|
|
813
|
+
else if (!isExt && (options?.taskHint !== undefined || options?.note !== undefined)) {
|
|
814
|
+
await this.setPageMetadata(result.targetId, options);
|
|
815
|
+
}
|
|
739
816
|
return {
|
|
740
817
|
...result,
|
|
741
818
|
targetId: result.targetId,
|
|
@@ -802,8 +879,272 @@ class UnifiedSessionManager {
|
|
|
802
879
|
*
|
|
803
880
|
* 同一请求内的嵌套调用可重入,恢复状态只保存在当前请求的 AsyncLocalStorage 中
|
|
804
881
|
*/
|
|
805
|
-
|
|
882
|
+
assertRequestActive(scope = this.tabLockContext.getStore()) {
|
|
883
|
+
if (scope?.budgetError) {
|
|
884
|
+
throw scope.budgetError;
|
|
885
|
+
}
|
|
886
|
+
scope?.signal?.throwIfAborted();
|
|
887
|
+
if (scope?.deadline !== undefined && Date.now() >= scope.deadline) {
|
|
888
|
+
throw setErrorSource(new StructuredToolError('TIMEOUT', 'Request timeout before dispatch', '请检查页面状态后再提交操作', {
|
|
889
|
+
actionExecuted: false,
|
|
890
|
+
actionStatus: 'not_started',
|
|
891
|
+
retryable: false,
|
|
892
|
+
}), 'budget');
|
|
893
|
+
}
|
|
894
|
+
}
|
|
895
|
+
async withUserAction(kind, fn, restoreTargetAfterPreparationFailure = false) {
|
|
896
|
+
return this.withTabLock(async () => {
|
|
897
|
+
const scope = this.tabLockContext.getStore();
|
|
898
|
+
const previous = scope.userAction;
|
|
899
|
+
const action = {
|
|
900
|
+
kind,
|
|
901
|
+
dispatched: false,
|
|
902
|
+
acknowledged: false,
|
|
903
|
+
phase: 'preparation',
|
|
904
|
+
};
|
|
905
|
+
scope.userAction = action;
|
|
906
|
+
try {
|
|
907
|
+
return await fn();
|
|
908
|
+
}
|
|
909
|
+
catch (error) {
|
|
910
|
+
if (restoreTargetAfterPreparationFailure && !action.dispatched && action.phase === 'preparation') {
|
|
911
|
+
scope.restoreTargetAfterPreparationFailure = true;
|
|
912
|
+
}
|
|
913
|
+
throw this.annotateUserActionError(error);
|
|
914
|
+
}
|
|
915
|
+
finally {
|
|
916
|
+
if (previous?.kind === kind && action.dispatched) {
|
|
917
|
+
previous.dispatched = true;
|
|
918
|
+
}
|
|
919
|
+
if (previous?.kind === kind && action.acknowledged) {
|
|
920
|
+
previous.acknowledged = true;
|
|
921
|
+
}
|
|
922
|
+
scope.userAction = previous;
|
|
923
|
+
}
|
|
924
|
+
});
|
|
925
|
+
}
|
|
926
|
+
acknowledgeInputAction() {
|
|
927
|
+
const action = this.tabLockContext.getStore()?.userAction;
|
|
928
|
+
if (action?.kind === 'input') {
|
|
929
|
+
action.acknowledged = true;
|
|
930
|
+
}
|
|
931
|
+
}
|
|
932
|
+
annotateUserActionError(error) {
|
|
933
|
+
const scope = this.tabLockContext.getStore();
|
|
934
|
+
const source = getErrorSource(error);
|
|
935
|
+
if (scope && error instanceof Error && source === 'budget') {
|
|
936
|
+
scope.budgetError ??= error;
|
|
937
|
+
}
|
|
938
|
+
const action = scope?.userAction;
|
|
939
|
+
const uncertain = source !== 'page' && isNativeExecutionResultUnknown(error);
|
|
940
|
+
const outputFailure = source !== 'page' &&
|
|
941
|
+
error instanceof StructuredToolError &&
|
|
942
|
+
error.code === 'NON_SERIALIZABLE_EVALUATE_RESULT' &&
|
|
943
|
+
getErrorPageContext(error)?.contextPhase === 'preparation';
|
|
944
|
+
const preparing = action &&
|
|
945
|
+
!action.dispatched &&
|
|
946
|
+
(uncertain ||
|
|
947
|
+
source === 'budget' ||
|
|
948
|
+
source === 'cancelled' ||
|
|
949
|
+
outputFailure ||
|
|
950
|
+
getErrorPageContext(error)?.diagnosticsPreparation === true ||
|
|
951
|
+
getErrorPageContext(error)?.keyboardFocusPreparation === true ||
|
|
952
|
+
getErrorPageContext(error)?.frameResolutionPreparation === true ||
|
|
953
|
+
action.phase === 'precondition' ||
|
|
954
|
+
error instanceof PagePreconditionError);
|
|
955
|
+
return setErrorPageContext(error, {
|
|
956
|
+
inputActionAcknowledged: action?.acknowledged === true,
|
|
957
|
+
contextPhase: preparing
|
|
958
|
+
? error instanceof PagePreconditionError
|
|
959
|
+
? 'precondition'
|
|
960
|
+
: action.phase
|
|
961
|
+
: 'action',
|
|
962
|
+
});
|
|
963
|
+
}
|
|
964
|
+
async prepareKeyboardFocus(timeout) {
|
|
965
|
+
if (this.getMode() !== 'extension' || this.inputMode !== 'precise') {
|
|
966
|
+
return undefined;
|
|
967
|
+
}
|
|
968
|
+
const scope = this.tabLockContext.getStore();
|
|
969
|
+
if (scope.keyboardFocus) {
|
|
970
|
+
return undefined;
|
|
971
|
+
}
|
|
972
|
+
const bridge = this.extensionBridge;
|
|
973
|
+
const targetId = bridge.getCurrentTargetId();
|
|
974
|
+
if (!targetId) {
|
|
975
|
+
throw new TargetNotFoundError('没有当前原生按键目标');
|
|
976
|
+
}
|
|
977
|
+
const focus = { id: randomUUID(), targetId };
|
|
978
|
+
scope.keyboardFocus = focus;
|
|
979
|
+
try {
|
|
980
|
+
this.assertRequestActive();
|
|
981
|
+
const result = await bridge.acquireKeyboardFocus(focus.id, timeout);
|
|
982
|
+
if (result.changed) {
|
|
983
|
+
focus.warning = '原生按键期间文档临时报告 visible;操作结束后恢复原设置,实际标签页与窗口焦点保持';
|
|
984
|
+
}
|
|
985
|
+
this.assertRequestActive();
|
|
986
|
+
return focus.warning;
|
|
987
|
+
}
|
|
988
|
+
catch (error) {
|
|
989
|
+
throw setErrorPageContext(error, { keyboardFocusPreparation: true });
|
|
990
|
+
}
|
|
991
|
+
}
|
|
992
|
+
async withInputCleanup(fn) {
|
|
993
|
+
const scope = this.tabLockContext.getStore();
|
|
994
|
+
const keysBefore = new Set(this.pressedKeys);
|
|
995
|
+
const driver = this.getMode() === 'extension' ? this.extensionBridge : getCdpSession();
|
|
996
|
+
const targetId = driver?.getCurrentTargetId();
|
|
997
|
+
const frameId = driver?.getCurrentFrameId?.();
|
|
998
|
+
const documentId = this.extensionBridge === driver ? this.extensionBridge?.getCurrentFrameDocumentId?.() : undefined;
|
|
999
|
+
const keys = new Set();
|
|
1000
|
+
scope.inputKeys = keys;
|
|
1001
|
+
const cleanup = async (releaseUnknown = false) => {
|
|
1002
|
+
if (!releaseUnknown &&
|
|
1003
|
+
!scope.signal?.aborted &&
|
|
1004
|
+
!scope.budgetError &&
|
|
1005
|
+
(scope.deadline === undefined || Date.now() < scope.deadline)) {
|
|
1006
|
+
return;
|
|
1007
|
+
}
|
|
1008
|
+
scope.inputCleanup = undefined;
|
|
1009
|
+
if (![...keys].some((key) => !keysBefore.has(key))) {
|
|
1010
|
+
return;
|
|
1011
|
+
}
|
|
1012
|
+
const cleanupDeadline = Date.now() + 1000;
|
|
1013
|
+
try {
|
|
1014
|
+
await this.tabLockContext.run({
|
|
1015
|
+
...scope,
|
|
1016
|
+
signal: undefined,
|
|
1017
|
+
deadline: cleanupDeadline,
|
|
1018
|
+
inputCleanup: undefined,
|
|
1019
|
+
userAction: undefined,
|
|
1020
|
+
budgetError: undefined,
|
|
1021
|
+
pendingPageCondition: null,
|
|
1022
|
+
}, async () => {
|
|
1023
|
+
const bridge = driver === this.extensionBridge ? this.extensionBridge : null;
|
|
1024
|
+
const previousTarget = driver?.getCurrentTargetId();
|
|
1025
|
+
const previousFrame = driver?.getCurrentFrameId?.();
|
|
1026
|
+
const previousDocument = bridge?.getCurrentFrameDocumentId?.();
|
|
1027
|
+
const inputContext = !bridge && targetId && (targetId !== previousTarget || frameId !== previousFrame)
|
|
1028
|
+
? { targetId, frameId }
|
|
1029
|
+
: undefined;
|
|
1030
|
+
try {
|
|
1031
|
+
if (bridge && targetId) {
|
|
1032
|
+
bridge.setCurrentTargetId(targetId);
|
|
1033
|
+
if (frameId !== undefined) {
|
|
1034
|
+
bridge.setCurrentFrameId(frameId, documentId);
|
|
1035
|
+
}
|
|
1036
|
+
}
|
|
1037
|
+
for (const key of keys) {
|
|
1038
|
+
if (keysBefore.has(key)) {
|
|
1039
|
+
continue;
|
|
1040
|
+
}
|
|
1041
|
+
try {
|
|
1042
|
+
await this.keyUp(key, remainingNavigationTimeout(cleanupDeadline), inputContext);
|
|
1043
|
+
}
|
|
1044
|
+
catch (error) {
|
|
1045
|
+
console.warn('[MCP] Cancelled input key release failed:', key, error);
|
|
1046
|
+
}
|
|
1047
|
+
}
|
|
1048
|
+
}
|
|
1049
|
+
finally {
|
|
1050
|
+
if (bridge && previousTarget !== undefined) {
|
|
1051
|
+
bridge.setCurrentTargetId(previousTarget);
|
|
1052
|
+
if (previousFrame !== undefined) {
|
|
1053
|
+
bridge.setCurrentFrameId(previousFrame, previousDocument);
|
|
1054
|
+
}
|
|
1055
|
+
}
|
|
1056
|
+
}
|
|
1057
|
+
});
|
|
1058
|
+
}
|
|
1059
|
+
catch (error) {
|
|
1060
|
+
console.warn('[MCP] Cancelled input scope cleanup failed:', error);
|
|
1061
|
+
}
|
|
1062
|
+
};
|
|
1063
|
+
scope.inputCleanup = cleanup;
|
|
1064
|
+
let failed = false;
|
|
1065
|
+
let primaryError;
|
|
1066
|
+
let cleanupError;
|
|
1067
|
+
let value;
|
|
1068
|
+
try {
|
|
1069
|
+
value = await fn();
|
|
1070
|
+
}
|
|
1071
|
+
catch (error) {
|
|
1072
|
+
failed = true;
|
|
1073
|
+
primaryError = error;
|
|
1074
|
+
if (scope.keyboardFocus?.warning) {
|
|
1075
|
+
setErrorPageContext(error, { keyboardFocusWarning: scope.keyboardFocus.warning });
|
|
1076
|
+
}
|
|
1077
|
+
throw error;
|
|
1078
|
+
}
|
|
1079
|
+
finally {
|
|
1080
|
+
await cleanup(failed && isNativeExecutionResultUnknown(primaryError));
|
|
1081
|
+
const focus = scope.keyboardFocus;
|
|
1082
|
+
if (focus && driver === this.extensionBridge) {
|
|
1083
|
+
const releaseKeys = failed ||
|
|
1084
|
+
Boolean(scope.signal?.aborted || scope.budgetError) ||
|
|
1085
|
+
(scope.deadline !== undefined && Date.now() >= scope.deadline);
|
|
1086
|
+
let result;
|
|
1087
|
+
try {
|
|
1088
|
+
result = await this.tabLockContext.run({
|
|
1089
|
+
...scope,
|
|
1090
|
+
signal: undefined,
|
|
1091
|
+
deadline: Date.now() + 4000,
|
|
1092
|
+
budgetError: undefined,
|
|
1093
|
+
userAction: undefined,
|
|
1094
|
+
pendingPageCondition: null,
|
|
1095
|
+
keyboardFocus: undefined,
|
|
1096
|
+
inputCleanup: undefined,
|
|
1097
|
+
}, () => this.extensionBridge.releaseKeyboardFocus(focus, releaseKeys));
|
|
1098
|
+
if (releaseKeys && result.restored) {
|
|
1099
|
+
for (const key of keys) {
|
|
1100
|
+
if (!keysBefore.has(key)) {
|
|
1101
|
+
this.pressedKeys.delete(key);
|
|
1102
|
+
this.modifiers &= ~(MODIFIER_KEYS[key] ?? 0);
|
|
1103
|
+
}
|
|
1104
|
+
}
|
|
1105
|
+
}
|
|
1106
|
+
}
|
|
1107
|
+
catch (error) {
|
|
1108
|
+
result = { restored: false, errors: [error instanceof Error ? error.message : String(error)] };
|
|
1109
|
+
}
|
|
1110
|
+
finally {
|
|
1111
|
+
scope.keyboardFocus = undefined;
|
|
1112
|
+
}
|
|
1113
|
+
if (!result.restored) {
|
|
1114
|
+
if (failed) {
|
|
1115
|
+
setErrorPageContext(primaryError, { keyboardFocusCleanup: result });
|
|
1116
|
+
}
|
|
1117
|
+
else {
|
|
1118
|
+
cleanupError = new StructuredToolError('KEYBOARD_FOCUS_CLEANUP_FAILED', '原生按键已执行,焦点状态恢复未完成', '请读取当前页面与 Extension 日志', {
|
|
1119
|
+
failureStage: 'cleanup',
|
|
1120
|
+
retryable: false,
|
|
1121
|
+
keyboardFocusCleanup: result,
|
|
1122
|
+
});
|
|
1123
|
+
}
|
|
1124
|
+
}
|
|
1125
|
+
}
|
|
1126
|
+
}
|
|
1127
|
+
if (cleanupError) {
|
|
1128
|
+
throw cleanupError;
|
|
1129
|
+
}
|
|
1130
|
+
return value;
|
|
1131
|
+
}
|
|
1132
|
+
async wait(ms) {
|
|
1133
|
+
this.assertRequestActive();
|
|
1134
|
+
await delay(ms, undefined, { signal: this.tabLockContext.getStore()?.signal });
|
|
1135
|
+
this.assertRequestActive();
|
|
1136
|
+
}
|
|
1137
|
+
async withMetadataContext(fn, deadline, signal) {
|
|
1138
|
+
const mode = this.getMode();
|
|
1139
|
+
return this.tabLockContext.run({ session: this, deadline, signal, mode: mode === 'none' ? undefined : mode }, async () => {
|
|
1140
|
+
this.assertRequestActive();
|
|
1141
|
+
return fn();
|
|
1142
|
+
});
|
|
1143
|
+
}
|
|
1144
|
+
async withTabLock(fn, deadline, signal) {
|
|
1145
|
+
signal?.throwIfAborted();
|
|
806
1146
|
if (this.tabLockContext.getStore()?.session === this) {
|
|
1147
|
+
this.assertRequestActive();
|
|
807
1148
|
if (deadline !== undefined) {
|
|
808
1149
|
remainingNavigationTimeout(deadline);
|
|
809
1150
|
}
|
|
@@ -816,9 +1157,28 @@ class UnifiedSessionManager {
|
|
|
816
1157
|
});
|
|
817
1158
|
let acquired = false;
|
|
818
1159
|
try {
|
|
819
|
-
await waitForNavigationQueue(previousLock, deadline);
|
|
1160
|
+
await waitForNavigationQueue(previousLock, deadline, signal);
|
|
820
1161
|
acquired = true;
|
|
821
|
-
|
|
1162
|
+
signal?.throwIfAborted();
|
|
1163
|
+
return await this.tabLockContext.run({ session: this, deadline, signal }, async () => {
|
|
1164
|
+
const scope = this.tabLockContext.getStore();
|
|
1165
|
+
try {
|
|
1166
|
+
return await fn();
|
|
1167
|
+
}
|
|
1168
|
+
finally {
|
|
1169
|
+
try {
|
|
1170
|
+
if (scope.signal?.aborted ||
|
|
1171
|
+
scope.budgetError ||
|
|
1172
|
+
(scope.deadline !== undefined && Date.now() >= scope.deadline)) {
|
|
1173
|
+
await scope.inputCleanup?.();
|
|
1174
|
+
}
|
|
1175
|
+
}
|
|
1176
|
+
finally {
|
|
1177
|
+
scope.inputKeys = undefined;
|
|
1178
|
+
scope.inputCleanup = undefined;
|
|
1179
|
+
}
|
|
1180
|
+
}
|
|
1181
|
+
});
|
|
822
1182
|
}
|
|
823
1183
|
finally {
|
|
824
1184
|
if (acquired) {
|
|
@@ -904,49 +1264,79 @@ class UnifiedSessionManager {
|
|
|
904
1264
|
temporaryTargetId: targetId,
|
|
905
1265
|
currentTargetId: null,
|
|
906
1266
|
};
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
1267
|
+
const independentCleanup = (scope.restoreTargetAfterPreparationFailure === true ||
|
|
1268
|
+
scope.restoreTargetAfterLookup === true) &&
|
|
1269
|
+
(scope.signal?.aborted === true ||
|
|
1270
|
+
scope.budgetError !== undefined ||
|
|
1271
|
+
(scope.deadline !== undefined && Date.now() >= scope.deadline));
|
|
1272
|
+
scope.restoreTargetAfterPreparationFailure = undefined;
|
|
1273
|
+
scope.restoreTargetAfterLookup = undefined;
|
|
1274
|
+
const cleanupDeadline = independentCleanup ? Date.now() + 1000 : deadline;
|
|
1275
|
+
const cleanupRemaining = () => cleanupDeadline === undefined ? undefined : remainingNavigationTimeout(cleanupDeadline);
|
|
1276
|
+
await this.tabLockContext.run(independentCleanup
|
|
1277
|
+
? {
|
|
1278
|
+
...scope,
|
|
1279
|
+
signal: undefined,
|
|
1280
|
+
deadline: cleanupDeadline,
|
|
1281
|
+
budgetError: undefined,
|
|
1282
|
+
userAction: undefined,
|
|
1283
|
+
pendingPageCondition: null,
|
|
1284
|
+
keyboardFocus: undefined,
|
|
1285
|
+
inputCleanup: undefined,
|
|
918
1286
|
}
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
restoration.status = 'failed';
|
|
922
|
-
restoration.error = error instanceof Error ? error.message : String(error);
|
|
923
|
-
}
|
|
924
|
-
if (clearTarget) {
|
|
1287
|
+
: scope, async () => {
|
|
1288
|
+
let clearTarget = true;
|
|
925
1289
|
try {
|
|
926
|
-
if (
|
|
927
|
-
driver.
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
1290
|
+
if (previousId) {
|
|
1291
|
+
const targets = await driver.listTargets(cleanupRemaining());
|
|
1292
|
+
if (targets.some((target) => (target.targetId ?? String(target.id)) === previousId)) {
|
|
1293
|
+
await driver.selectPage(previousId, cleanupRemaining());
|
|
1294
|
+
clearTarget = false;
|
|
1295
|
+
}
|
|
1296
|
+
else {
|
|
1297
|
+
restoration.status = 'previous_target_closed';
|
|
1298
|
+
}
|
|
931
1299
|
}
|
|
932
1300
|
}
|
|
933
1301
|
catch (error) {
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
1302
|
+
restoration.status = 'failed';
|
|
1303
|
+
restoration.error = error instanceof Error ? error.message : String(error);
|
|
1304
|
+
}
|
|
1305
|
+
if (clearTarget) {
|
|
1306
|
+
try {
|
|
1307
|
+
if (mode === 'extension') {
|
|
1308
|
+
driver.setCurrentTargetId(null);
|
|
1309
|
+
}
|
|
1310
|
+
else {
|
|
1311
|
+
await getCdpSession().deselectPage(cleanupDeadline === undefined
|
|
1312
|
+
? undefined
|
|
1313
|
+
: Math.max(0, cleanupDeadline - Date.now()));
|
|
1314
|
+
}
|
|
937
1315
|
}
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
restoration.error
|
|
1316
|
+
catch (error) {
|
|
1317
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
1318
|
+
if (restoration.error) {
|
|
1319
|
+
restoration.cleanupError = message;
|
|
1320
|
+
}
|
|
1321
|
+
else {
|
|
1322
|
+
restoration.status = 'failed';
|
|
1323
|
+
restoration.error = message;
|
|
1324
|
+
}
|
|
941
1325
|
}
|
|
942
1326
|
}
|
|
943
|
-
}
|
|
1327
|
+
});
|
|
944
1328
|
restoration.currentTargetId = driver.getCurrentTargetId();
|
|
945
|
-
|
|
1329
|
+
scope.targetRestoration = restoration;
|
|
946
1330
|
}
|
|
947
1331
|
}
|
|
948
1332
|
}, deadline);
|
|
949
1333
|
}
|
|
1334
|
+
preserveTargetAfterLookup() {
|
|
1335
|
+
const scope = this.tabLockContext.getStore();
|
|
1336
|
+
if (scope) {
|
|
1337
|
+
scope.restoreTargetAfterLookup = true;
|
|
1338
|
+
}
|
|
1339
|
+
}
|
|
950
1340
|
getTargetRestoration() {
|
|
951
1341
|
return this.tabLockContext.getStore()?.targetRestoration;
|
|
952
1342
|
}
|
|
@@ -963,57 +1353,81 @@ class UnifiedSessionManager {
|
|
|
963
1353
|
? await this.extensionBridge.getPageSnapshot(id, timeout)
|
|
964
1354
|
: await getCdpSession().getPageSnapshot(id, timeout);
|
|
965
1355
|
this.recordPageUse(id);
|
|
1356
|
+
const nativeMetadata = snapshot.metadata;
|
|
1357
|
+
if (nativeMetadata) {
|
|
1358
|
+
this.rememberPageMetadata(id, nativeMetadata);
|
|
1359
|
+
}
|
|
966
1360
|
return snapshot;
|
|
967
1361
|
}
|
|
968
1362
|
async withPageCondition(condition, fn, deadline) {
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
const mode = this.getMode();
|
|
974
|
-
const targetId = condition.targetId ?? this.getCurrentTargetId();
|
|
975
|
-
if (mode === 'none' || !targetId) {
|
|
976
|
-
throw new DriverCapabilityError('没有操作目标,请先选择页面');
|
|
977
|
-
}
|
|
978
|
-
const driver = mode === 'extension' ? this.extensionBridge : getCdpSession();
|
|
979
|
-
const previousRequireExtension = this.requireExtension;
|
|
980
|
-
const previousCdpSelection = this.cdpExplicitlySelected;
|
|
981
|
-
const previousMode = this.activeMode;
|
|
982
|
-
if (mode === 'extension') {
|
|
983
|
-
this.requireExtension = true;
|
|
984
|
-
}
|
|
985
|
-
else {
|
|
986
|
-
this.cdpExplicitlySelected = true;
|
|
1363
|
+
let contextPhase = 'action';
|
|
1364
|
+
try {
|
|
1365
|
+
if (condition.expectedUrl === undefined && condition.expectedRevision === undefined) {
|
|
1366
|
+
return await fn();
|
|
987
1367
|
}
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
1368
|
+
return await this.withTabLock(async () => {
|
|
1369
|
+
contextPhase = 'precondition';
|
|
1370
|
+
const userAction = this.tabLockContext.getStore()?.userAction;
|
|
1371
|
+
if (userAction) {
|
|
1372
|
+
userAction.phase = 'precondition';
|
|
992
1373
|
}
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
1374
|
+
const mode = this.getMode();
|
|
1375
|
+
const targetId = condition.targetId ?? this.getCurrentTargetId();
|
|
1376
|
+
if (mode === 'none' || !targetId) {
|
|
1377
|
+
throw new DriverCapabilityError('没有操作目标,请先选择页面');
|
|
1378
|
+
}
|
|
1379
|
+
const driver = mode === 'extension' ? this.extensionBridge : getCdpSession();
|
|
1380
|
+
const previousRequireExtension = this.requireExtension;
|
|
1381
|
+
const previousCdpSelection = this.cdpExplicitlySelected;
|
|
1382
|
+
const previousMode = this.activeMode;
|
|
1383
|
+
if (mode === 'extension') {
|
|
1384
|
+
this.requireExtension = true;
|
|
1385
|
+
}
|
|
1386
|
+
else {
|
|
1387
|
+
this.cdpExplicitlySelected = true;
|
|
1388
|
+
}
|
|
1389
|
+
try {
|
|
1390
|
+
const snapshot = await driver.getPageSnapshot(targetId, deadline === undefined ? undefined : remainingNavigationTimeout(deadline));
|
|
1391
|
+
if (snapshot.revision === null) {
|
|
1392
|
+
throw new StructuredToolError('DRIVER_CAPABILITY_ERROR', '当前 Extension 不支持原子页面条件,未执行操作', '更新 Extension 后再使用 expectedUrl 或 expectedRevision', {
|
|
1393
|
+
targetId,
|
|
1394
|
+
actionExecuted: false,
|
|
1395
|
+
actionStatus: 'not_started',
|
|
1396
|
+
failureStage: 'precondition',
|
|
1397
|
+
});
|
|
1398
|
+
}
|
|
1399
|
+
if ((condition.expectedUrl !== undefined && snapshot.url !== condition.expectedUrl) ||
|
|
1400
|
+
(condition.expectedRevision !== undefined && snapshot.revision !== condition.expectedRevision)) {
|
|
1401
|
+
throw new PagePreconditionError({
|
|
1402
|
+
targetId,
|
|
1403
|
+
expectedUrl: condition.expectedUrl ?? null,
|
|
1404
|
+
expectedRevision: condition.expectedRevision ?? null,
|
|
1405
|
+
actualUrl: snapshot.url,
|
|
1406
|
+
actualRevision: snapshot.revision,
|
|
1407
|
+
});
|
|
1408
|
+
}
|
|
1409
|
+
driver.setPageCondition({
|
|
996
1410
|
targetId,
|
|
997
|
-
expectedUrl: condition.expectedUrl
|
|
998
|
-
expectedRevision: condition.expectedRevision
|
|
999
|
-
actualUrl: snapshot.url,
|
|
1000
|
-
actualRevision: snapshot.revision,
|
|
1411
|
+
expectedUrl: condition.expectedUrl,
|
|
1412
|
+
expectedRevision: condition.expectedRevision,
|
|
1001
1413
|
});
|
|
1414
|
+
contextPhase = 'action';
|
|
1415
|
+
if (userAction) {
|
|
1416
|
+
userAction.phase = 'preparation';
|
|
1417
|
+
}
|
|
1418
|
+
return await fn();
|
|
1002
1419
|
}
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
}
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
this.activeMode = previousMode;
|
|
1015
|
-
}
|
|
1016
|
-
}, deadline);
|
|
1420
|
+
finally {
|
|
1421
|
+
driver.setPageCondition(null);
|
|
1422
|
+
this.requireExtension = previousRequireExtension;
|
|
1423
|
+
this.cdpExplicitlySelected = previousCdpSelection;
|
|
1424
|
+
this.activeMode = previousMode;
|
|
1425
|
+
}
|
|
1426
|
+
}, deadline);
|
|
1427
|
+
}
|
|
1428
|
+
catch (error) {
|
|
1429
|
+
throw setErrorPageContext(error, { contextPhase });
|
|
1430
|
+
}
|
|
1017
1431
|
}
|
|
1018
1432
|
/**
|
|
1019
1433
|
* 临时切换操作目标 iframe,执行完后恢复
|
|
@@ -1028,35 +1442,97 @@ class UnifiedSessionManager {
|
|
|
1028
1442
|
if (frame === undefined) {
|
|
1029
1443
|
return fn();
|
|
1030
1444
|
}
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
const
|
|
1035
|
-
|
|
1445
|
+
const scope = this.tabLockContext.getStore();
|
|
1446
|
+
deadline = deadline === undefined ? scope?.deadline : Math.min(deadline, scope?.deadline ?? deadline);
|
|
1447
|
+
const driver = await this.getDriver(deadline === undefined ? undefined : remainingNavigationTimeout(deadline));
|
|
1448
|
+
const { frameId, offset, frameContext } = await driver
|
|
1449
|
+
.resolveFrame(frame, deadline === undefined ? undefined : remainingNavigationTimeout(deadline))
|
|
1450
|
+
.catch((error) => {
|
|
1451
|
+
if (!scope?.userAction?.dispatched) {
|
|
1452
|
+
if (scope) {
|
|
1453
|
+
scope.restoreTargetAfterPreparationFailure = true;
|
|
1454
|
+
}
|
|
1455
|
+
throw setErrorPageContext(error, { contextPhase: 'preparation', frameResolutionPreparation: true });
|
|
1456
|
+
}
|
|
1457
|
+
throw error;
|
|
1458
|
+
});
|
|
1036
1459
|
const previousFrameId = driver.getCurrentFrameId();
|
|
1037
|
-
const
|
|
1038
|
-
const
|
|
1039
|
-
const
|
|
1040
|
-
|
|
1460
|
+
const extension = driver === this.extensionBridge ? this.extensionBridge : undefined;
|
|
1461
|
+
const previousDocument = extension?.getCurrentFrameDocumentId?.();
|
|
1462
|
+
const previousOffset = this.currentFrameOffset;
|
|
1463
|
+
if (extension) {
|
|
1464
|
+
extension.setCurrentFrameId(frameId, typeof frameContext?.documentId === 'string' ? frameContext.documentId : undefined);
|
|
1465
|
+
}
|
|
1466
|
+
else {
|
|
1467
|
+
driver.setCurrentFrameId(frameId);
|
|
1468
|
+
}
|
|
1041
1469
|
this.currentFrameOffset = offset;
|
|
1042
|
-
this.requireExtension = true;
|
|
1043
|
-
this.activeMode = 'extension';
|
|
1044
1470
|
try {
|
|
1045
1471
|
return await fn();
|
|
1046
1472
|
}
|
|
1047
1473
|
catch (error) {
|
|
1048
|
-
throw setErrorPageContext(error, {
|
|
1049
|
-
frameContext: { selection: frame, frameId, ...frameContext },
|
|
1050
|
-
});
|
|
1474
|
+
throw setErrorPageContext(error, { frameContext: { selection: frame, frameId, ...frameContext } });
|
|
1051
1475
|
}
|
|
1052
1476
|
finally {
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1477
|
+
this.currentFrameOffset = previousOffset;
|
|
1478
|
+
if (extension) {
|
|
1479
|
+
extension.setCurrentFrameId(previousFrameId, previousDocument);
|
|
1480
|
+
}
|
|
1481
|
+
else {
|
|
1482
|
+
driver.setCurrentFrameId(previousFrameId);
|
|
1483
|
+
}
|
|
1058
1484
|
}
|
|
1059
1485
|
}
|
|
1486
|
+
hasBrowserEventObservation(targetId) {
|
|
1487
|
+
return this.getMode() === 'cdp' || this.extensionBridge?.hasBrowserEventObservation(targetId) === true;
|
|
1488
|
+
}
|
|
1489
|
+
async browserEvents(action, command, targetId, timeout = DEFAULT_TIMEOUT, signal, deadline = Date.now() + timeout) {
|
|
1490
|
+
if (!targetId && !command.ref) {
|
|
1491
|
+
return this.withTabLock(() => {
|
|
1492
|
+
const id = this.getCurrentTargetId();
|
|
1493
|
+
if (!id) {
|
|
1494
|
+
throw new DriverCapabilityError('没有操作目标,请先选择页面');
|
|
1495
|
+
}
|
|
1496
|
+
return this.browserEvents(action, command, id, timeout, signal, deadline);
|
|
1497
|
+
}, deadline, signal);
|
|
1498
|
+
}
|
|
1499
|
+
const refTarget = command.ref ? eventRefTarget(command.ref) : undefined;
|
|
1500
|
+
const mode = refTarget?.backend ?? this.getMode();
|
|
1501
|
+
if (targetId && refTarget && targetId !== refTarget.targetId) {
|
|
1502
|
+
throw new DriverCapabilityError('Browser event ref target mismatch');
|
|
1503
|
+
}
|
|
1504
|
+
const driver = mode === 'extension' ? this.extensionBridge : mode === 'cdp' ? getCdpSession() : null;
|
|
1505
|
+
if (!driver) {
|
|
1506
|
+
throw new DriverCapabilityError('Browser event backend is not connected');
|
|
1507
|
+
}
|
|
1508
|
+
const id = refTarget?.targetId ?? targetId ?? driver.getCurrentTargetId() ?? undefined;
|
|
1509
|
+
const scope = this.tabLockContext.getStore();
|
|
1510
|
+
return this.tabLockContext.run({
|
|
1511
|
+
session: this,
|
|
1512
|
+
signal: signal ?? scope?.signal,
|
|
1513
|
+
deadline: Math.min(scope?.deadline ?? Infinity, deadline),
|
|
1514
|
+
}, () => driver.browserEvents(action, command, id, timeout));
|
|
1515
|
+
}
|
|
1516
|
+
async browserFetchCommand(method, params, targetId, signal) {
|
|
1517
|
+
const mode = this.getMode();
|
|
1518
|
+
const driver = mode === 'extension' ? this.extensionBridge : mode === 'cdp' ? getCdpSession() : null;
|
|
1519
|
+
if (!driver?.browserFetchCommand) {
|
|
1520
|
+
throw new DriverCapabilityError('Fetch control backend unavailable');
|
|
1521
|
+
}
|
|
1522
|
+
const scope = this.tabLockContext.getStore();
|
|
1523
|
+
return this.tabLockContext.run({
|
|
1524
|
+
...scope,
|
|
1525
|
+
session: this,
|
|
1526
|
+
signal: signal ?? scope?.signal,
|
|
1527
|
+
deadline: Math.min(scope?.deadline ?? Infinity, Date.now() + DEFAULT_TIMEOUT),
|
|
1528
|
+
}, () => driver.browserFetchCommand(method, params, targetId));
|
|
1529
|
+
}
|
|
1530
|
+
async suspendBrowserEventsForChallenge() {
|
|
1531
|
+
const driver = await this.getDriver();
|
|
1532
|
+
return driver.suspendBrowserEventsForChallenge
|
|
1533
|
+
? driver.suspendBrowserEventsForChallenge()
|
|
1534
|
+
: { suspended: false, reason: 'backend capability unavailable' };
|
|
1535
|
+
}
|
|
1060
1536
|
/**
|
|
1061
1537
|
* 获取当前状态
|
|
1062
1538
|
*/
|
|
@@ -1067,15 +1543,15 @@ class UnifiedSessionManager {
|
|
|
1067
1543
|
}
|
|
1068
1544
|
return getCdpSession().getState();
|
|
1069
1545
|
}
|
|
1070
|
-
async getLiveState() {
|
|
1546
|
+
async getLiveState(timeout) {
|
|
1071
1547
|
if (this.getMode() !== 'extension') {
|
|
1072
|
-
return getCdpSession().getLiveState();
|
|
1548
|
+
return getCdpSession().getLiveState(timeout);
|
|
1073
1549
|
}
|
|
1074
1550
|
const targetId = this.extensionBridge.getCurrentTargetId();
|
|
1075
1551
|
if (!targetId) {
|
|
1076
1552
|
return null;
|
|
1077
1553
|
}
|
|
1078
|
-
const targets = await this.extensionBridge.listTargets();
|
|
1554
|
+
const targets = await this.extensionBridge.listTargets(timeout);
|
|
1079
1555
|
const target = targets.find((item) => item.targetId === targetId);
|
|
1080
1556
|
if (!target) {
|
|
1081
1557
|
return null;
|
|
@@ -1109,6 +1585,7 @@ class UnifiedSessionManager {
|
|
|
1109
1585
|
throw new Error('commands 参数不支持 stealth 输入模式,请先调用 manage action=inputMode inputMode=precise 切换后重试');
|
|
1110
1586
|
}
|
|
1111
1587
|
// Puppeteer 风格:连续 keyDown 同 key → rawKeyDown + autoRepeat(长按重复)
|
|
1588
|
+
this.tabLockContext.getStore()?.inputKeys?.add(key);
|
|
1112
1589
|
const isRepeat = this.pressedKeys.has(key);
|
|
1113
1590
|
const nextModifiers = MODIFIER_KEYS[key] ? this.modifiers | MODIFIER_KEYS[key] : this.modifiers;
|
|
1114
1591
|
if (!isExt) {
|
|
@@ -1139,14 +1616,19 @@ class UnifiedSessionManager {
|
|
|
1139
1616
|
/**
|
|
1140
1617
|
* 释放键盘按键
|
|
1141
1618
|
*/
|
|
1142
|
-
async keyUp(key, timeout) {
|
|
1619
|
+
async keyUp(key, timeout, inputContext) {
|
|
1143
1620
|
const deadline = timeout === undefined ? undefined : Date.now() + timeout;
|
|
1144
1621
|
const driver = await this.getDriver(timeout);
|
|
1145
1622
|
const remaining = () => (deadline === undefined ? undefined : remainingNavigationTimeout(deadline));
|
|
1146
1623
|
const isExt = this.isExtensionConnected();
|
|
1147
1624
|
const nextModifiers = MODIFIER_KEYS[key] ? this.modifiers & ~MODIFIER_KEYS[key] : this.modifiers;
|
|
1148
1625
|
if (!isExt) {
|
|
1149
|
-
|
|
1626
|
+
if (inputContext) {
|
|
1627
|
+
await getCdpSession().keyUp(key, remaining(), { ...inputContext, modifiers: nextModifiers });
|
|
1628
|
+
}
|
|
1629
|
+
else {
|
|
1630
|
+
await getCdpSession().keyUp(key, remaining());
|
|
1631
|
+
}
|
|
1150
1632
|
}
|
|
1151
1633
|
else if (this.inputMode === 'stealth') {
|
|
1152
1634
|
await driver.stealthKey(key, 'up', this.getModifierNames(nextModifiers), remaining());
|
|
@@ -1162,6 +1644,7 @@ class UnifiedSessionManager {
|
|
|
1162
1644
|
}
|
|
1163
1645
|
this.modifiers = nextModifiers;
|
|
1164
1646
|
this.pressedKeys.delete(key);
|
|
1647
|
+
this.tabLockContext.getStore()?.inputKeys?.delete(key);
|
|
1165
1648
|
}
|
|
1166
1649
|
/**
|
|
1167
1650
|
* 输入文本
|
|
@@ -1188,50 +1671,59 @@ class UnifiedSessionManager {
|
|
|
1188
1671
|
if (remaining !== undefined && remaining <= 0) {
|
|
1189
1672
|
throw new TimeoutError('input timeout before mouse movement');
|
|
1190
1673
|
}
|
|
1191
|
-
this.currentMousePosition = { x, y }; // 更新位置
|
|
1192
1674
|
const isExt = this.isExtensionConnected();
|
|
1675
|
+
if (!isExt) {
|
|
1676
|
+
;
|
|
1677
|
+
({ x, y } = await getCdpSession().mapFramePoint({ x, y }, remaining));
|
|
1678
|
+
}
|
|
1193
1679
|
if (isExt && this.inputMode === 'stealth') {
|
|
1194
|
-
await driver.stealthMouse('mousemove', x, y, undefined, remaining);
|
|
1680
|
+
await driver.stealthMouse('mousemove', x, y, undefined, remaining, this.modifiers);
|
|
1195
1681
|
}
|
|
1196
1682
|
else {
|
|
1197
1683
|
await driver.inputMouse('mouseMoved', x, y, { modifiers: this.modifiers }, remaining);
|
|
1198
1684
|
}
|
|
1685
|
+
this.currentMousePosition = { x, y };
|
|
1686
|
+
this.rememberMouseFrame();
|
|
1199
1687
|
}
|
|
1200
1688
|
/**
|
|
1201
1689
|
* 鼠标按下
|
|
1202
1690
|
*/
|
|
1203
1691
|
async mouseDown(button = 'left', clickCount = 1) {
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
|
|
1692
|
+
return this.withMouseFrame(async () => {
|
|
1693
|
+
const { x, y } = this.currentMousePosition;
|
|
1694
|
+
const driver = await this.getDriver();
|
|
1695
|
+
const isExt = this.isExtensionConnected();
|
|
1696
|
+
if (isExt && this.inputMode === 'stealth') {
|
|
1697
|
+
await driver.stealthMouse('mousedown', x, y, button, undefined, this.modifiers);
|
|
1698
|
+
}
|
|
1699
|
+
else {
|
|
1700
|
+
await driver.inputMouse('mousePressed', x, y, {
|
|
1701
|
+
button,
|
|
1702
|
+
clickCount,
|
|
1703
|
+
modifiers: this.modifiers,
|
|
1704
|
+
});
|
|
1705
|
+
}
|
|
1706
|
+
});
|
|
1217
1707
|
}
|
|
1218
1708
|
/**
|
|
1219
1709
|
* 鼠标释放
|
|
1220
1710
|
*/
|
|
1221
1711
|
async mouseUp(button = 'left', clickCount = 1) {
|
|
1222
|
-
|
|
1223
|
-
|
|
1224
|
-
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
|
|
1712
|
+
return this.withMouseFrame(async () => {
|
|
1713
|
+
const { x, y } = this.currentMousePosition;
|
|
1714
|
+
const driver = await this.getDriver();
|
|
1715
|
+
const isExt = this.isExtensionConnected();
|
|
1716
|
+
if (isExt && this.inputMode === 'stealth') {
|
|
1717
|
+
await driver.stealthMouse('mouseup', x, y, button, undefined, this.modifiers);
|
|
1718
|
+
}
|
|
1719
|
+
else {
|
|
1720
|
+
await driver.inputMouse('mouseReleased', x, y, {
|
|
1721
|
+
button,
|
|
1722
|
+
clickCount,
|
|
1723
|
+
modifiers: this.modifiers,
|
|
1724
|
+
});
|
|
1725
|
+
}
|
|
1726
|
+
});
|
|
1235
1727
|
}
|
|
1236
1728
|
/**
|
|
1237
1729
|
* 鼠标点击(mousedown + mouseup + click 三合一)
|
|
@@ -1240,50 +1732,71 @@ class UnifiedSessionManager {
|
|
|
1240
1732
|
* precise / CDP 模式:mouseDown + mouseUp,浏览器自动合成原生 click 事件
|
|
1241
1733
|
*/
|
|
1242
1734
|
async mouseClick(button = 'left', clickCount = 1, refId) {
|
|
1243
|
-
|
|
1244
|
-
|
|
1245
|
-
|
|
1246
|
-
|
|
1247
|
-
|
|
1248
|
-
|
|
1249
|
-
|
|
1250
|
-
|
|
1251
|
-
|
|
1252
|
-
|
|
1253
|
-
|
|
1254
|
-
|
|
1255
|
-
|
|
1256
|
-
|
|
1735
|
+
return this.withMouseFrame(async () => {
|
|
1736
|
+
const driver = await this.getDriver();
|
|
1737
|
+
const isExt = this.isExtensionConnected();
|
|
1738
|
+
if (isExt && (this.inputMode === 'stealth' || button === 'right')) {
|
|
1739
|
+
const { x, y } = this.currentMousePosition;
|
|
1740
|
+
// stealth 通过单次脚本注入完成所有 mouse/click/dblclick/contextmenu 事件
|
|
1741
|
+
// refId 透传:嵌套 iframe overlay 场景下绕过 elementFromPoint 命中外层 IFRAME 的问题
|
|
1742
|
+
await driver.stealthClick(x, y, button, clickCount, refId, undefined, this.modifiers);
|
|
1743
|
+
return;
|
|
1744
|
+
}
|
|
1745
|
+
// CDP 每次 mousePressed/mouseReleased 递增 clickCount,让浏览器合成 dblclick/tripleclick
|
|
1746
|
+
for (let i = 1; i <= clickCount; i++) {
|
|
1747
|
+
let pressRejected = false;
|
|
1748
|
+
try {
|
|
1749
|
+
await this.mouseDown(button, i);
|
|
1750
|
+
}
|
|
1751
|
+
catch (error) {
|
|
1752
|
+
pressRejected = error instanceof StructuredToolError && error.context.actionExecuted === false;
|
|
1753
|
+
throw error;
|
|
1754
|
+
}
|
|
1755
|
+
finally {
|
|
1756
|
+
if (!pressRejected) {
|
|
1757
|
+
const { x, y } = this.currentMousePosition;
|
|
1758
|
+
await driver.inputMouse('mouseReleased', x, y, {
|
|
1759
|
+
button,
|
|
1760
|
+
clickCount: i,
|
|
1761
|
+
modifiers: this.modifiers,
|
|
1762
|
+
});
|
|
1763
|
+
}
|
|
1764
|
+
}
|
|
1765
|
+
}
|
|
1766
|
+
});
|
|
1257
1767
|
}
|
|
1258
1768
|
/**
|
|
1259
1769
|
* Turnstile 点击:同页控件走页面鼠标;Cloudflare iframe 只在已聚焦窗口的活动受控 tab 上使用系统鼠标
|
|
1260
1770
|
*/
|
|
1261
1771
|
// noinspection JSUnusedGlobalSymbols — resolveChallenge 通过 ChallengeHost 结构类型调用
|
|
1262
|
-
async clickTurnstile(snapshot, timeout) {
|
|
1263
|
-
const
|
|
1264
|
-
|
|
1772
|
+
async clickTurnstile(snapshot, timeout, signal, refreshSnapshot) {
|
|
1773
|
+
const scope = this.tabLockContext.getStore();
|
|
1774
|
+
signal ??= scope?.signal;
|
|
1775
|
+
signal?.throwIfAborted();
|
|
1776
|
+
const deadline = timeout === undefined ? scope?.deadline : Math.min(Date.now() + timeout, scope?.deadline ?? Infinity);
|
|
1777
|
+
const remaining = () => {
|
|
1778
|
+
signal?.throwIfAborted();
|
|
1779
|
+
return deadline === undefined ? undefined : remainingNavigationTimeout(deadline);
|
|
1780
|
+
};
|
|
1265
1781
|
if (snapshot.clickSource !== 'iframe') {
|
|
1266
1782
|
const point = snapshot.clickPoint;
|
|
1267
1783
|
if (!point) {
|
|
1268
1784
|
throw new OsMouseError('TURNSTILE_OS_CLICK_UNAVAILABLE', '页面未返回可点击的 Turnstile 坐标', '重新检查页面状态后再重试;交互式验证码无法自动完成');
|
|
1269
1785
|
}
|
|
1270
|
-
const driver = await withNavigationBudget(() => this.getDriver(remaining()), deadline);
|
|
1786
|
+
const driver = await withNavigationBudget(() => this.getDriver(remaining()), deadline, signal);
|
|
1271
1787
|
if (this.isExtensionConnected()) {
|
|
1272
|
-
await withNavigationBudget(() => driver.stealthClick(point.x, point.y, 'left', 1, undefined, remaining()), deadline);
|
|
1788
|
+
await withNavigationBudget(() => driver.stealthClick(point.x, point.y, 'left', 1, undefined, remaining()), deadline, signal);
|
|
1273
1789
|
return;
|
|
1274
1790
|
}
|
|
1275
|
-
const
|
|
1276
|
-
const clickPoint = frameOffset && this.inputMode !== 'stealth'
|
|
1277
|
-
? { x: point.x + frameOffset.x, y: point.y + frameOffset.y }
|
|
1278
|
-
: point;
|
|
1791
|
+
const clickPoint = await withNavigationBudget(() => getCdpSession().mapFramePoint(point, remaining()), deadline, signal);
|
|
1279
1792
|
this.currentMousePosition = clickPoint;
|
|
1280
|
-
await withNavigationBudget(() => driver.inputMouse('mouseMoved', clickPoint.x, clickPoint.y, { modifiers: this.modifiers }), deadline);
|
|
1793
|
+
await withNavigationBudget(() => driver.inputMouse('mouseMoved', clickPoint.x, clickPoint.y, { modifiers: this.modifiers }), deadline, signal);
|
|
1281
1794
|
await withNavigationBudget(() => getCdpSession().inputMouse('mousePressed', clickPoint.x, clickPoint.y, {
|
|
1282
1795
|
button: 'left',
|
|
1283
1796
|
clickCount: 1,
|
|
1284
1797
|
modifiers: this.modifiers,
|
|
1285
1798
|
releaseAfterPress: true,
|
|
1286
|
-
}, remaining()), deadline);
|
|
1799
|
+
}, remaining()), deadline, signal);
|
|
1287
1800
|
return;
|
|
1288
1801
|
}
|
|
1289
1802
|
if (this.getMode() !== 'extension' || !this.extensionBridge) {
|
|
@@ -1293,7 +1806,7 @@ class UnifiedSessionManager {
|
|
|
1293
1806
|
if (!targetId) {
|
|
1294
1807
|
throw new OsMouseError('TURNSTILE_OS_CLICK_UNAVAILABLE', '没有当前受控页面,无法使用系统鼠标点击 Turnstile', '先 browse attach 到受控测试页,把该页放到已经聚焦的窗口并保持为活动 tab 后重试');
|
|
1295
1808
|
}
|
|
1296
|
-
const topology = await this.listTopology(remaining());
|
|
1809
|
+
const topology = await withNavigationBudget(() => this.listTopology(remaining()), deadline, signal);
|
|
1297
1810
|
const windowInfo = topology?.windows.find((item) => item.tabs.some((tab) => String(tab.id) === targetId || tab.targetId === targetId));
|
|
1298
1811
|
const tab = windowInfo?.tabs.find((item) => String(item.id) === targetId || item.targetId === targetId);
|
|
1299
1812
|
if (!windowInfo || !tab) {
|
|
@@ -1309,29 +1822,107 @@ class UnifiedSessionManager {
|
|
|
1309
1822
|
if (tab.active !== true && (activeTabId === undefined || String(activeTabId) !== targetId)) {
|
|
1310
1823
|
throw new OsMouseError('TURNSTILE_OS_CLICK_UNAVAILABLE', '受控测试页不是当前窗口的活动 tab,拒绝系统鼠标点击', '先把受控测试页设为该窗口的活动 tab,且窗口已经聚焦后再重试');
|
|
1311
1824
|
}
|
|
1312
|
-
await withNavigationBudget(() => this.extensionBridge.debuggerDetach(undefined, remaining()), deadline);
|
|
1313
|
-
const
|
|
1314
|
-
|
|
1315
|
-
const
|
|
1316
|
-
|
|
1317
|
-
|
|
1318
|
-
|
|
1319
|
-
|
|
1320
|
-
|
|
1321
|
-
|
|
1322
|
-
|
|
1323
|
-
|
|
1324
|
-
|
|
1325
|
-
|
|
1326
|
-
|
|
1327
|
-
|
|
1328
|
-
|
|
1329
|
-
|
|
1330
|
-
|
|
1331
|
-
|
|
1332
|
-
|
|
1333
|
-
|
|
1334
|
-
|
|
1825
|
+
await withNavigationBudget(() => this.extensionBridge.debuggerDetach(undefined, remaining()), deadline, signal);
|
|
1826
|
+
const pointerId = process.platform === 'linux' ? randomUUID() : undefined;
|
|
1827
|
+
let pointerDocumentId;
|
|
1828
|
+
const resolvePoint = async (refresh) => {
|
|
1829
|
+
const current = refresh && refreshSnapshot
|
|
1830
|
+
? await withNavigationBudget(() => refreshSnapshot(remaining()), deadline, signal)
|
|
1831
|
+
: snapshot;
|
|
1832
|
+
if (!current.clickPoint ||
|
|
1833
|
+
current.clickSource !== 'iframe' ||
|
|
1834
|
+
current.clickTargetKind !== snapshot.clickTargetKind ||
|
|
1835
|
+
current.url !== snapshot.url) {
|
|
1836
|
+
throw new OsMouseError('TURNSTILE_OS_CLICK_UNAVAILABLE', '系统鼠标按下前,页面或验证控件已变化', '重新检查页面验证状态后再重试');
|
|
1837
|
+
}
|
|
1838
|
+
const viewport = await withNavigationBudget(() => this.readViewportMetrics(0), deadline, signal);
|
|
1839
|
+
const refreshedTopology = await withNavigationBudget(() => this.listTopology(remaining()), deadline, signal);
|
|
1840
|
+
const refreshedWindow = refreshedTopology?.windows.find((item) => item.tabs.some((candidate) => String(candidate.id) === targetId || candidate.targetId === targetId));
|
|
1841
|
+
const refreshedTab = refreshedWindow?.tabs.find((candidate) => String(candidate.id) === targetId || candidate.targetId === targetId);
|
|
1842
|
+
const refreshedActiveTabId = refreshedWindow?.activeTabId;
|
|
1843
|
+
if (!refreshedWindow?.focused ||
|
|
1844
|
+
refreshedTab?.managed !== true ||
|
|
1845
|
+
(refreshedTab.active !== true &&
|
|
1846
|
+
(refreshedActiveTabId === undefined || String(refreshedActiveTabId) !== targetId))) {
|
|
1847
|
+
throw new OsMouseError('TURNSTILE_OS_CLICK_UNAVAILABLE', '系统鼠标点击前,受控测试页的窗口或活动 tab 已变化', '重新聚焦受控测试页并保持为活动 tab 后重试');
|
|
1848
|
+
}
|
|
1849
|
+
const frameOffset = this.currentFrameOffset;
|
|
1850
|
+
const clickPoint = frameOffset
|
|
1851
|
+
? { x: current.clickPoint.x + frameOffset.x, y: current.clickPoint.y + frameOffset.y }
|
|
1852
|
+
: current.clickPoint;
|
|
1853
|
+
const estimate = await withNavigationBudget(() => resolveTurnstileOsClickPoint({ ...current, clickPoint, screenPoint: null, viewport }, { deadline, signal }), deadline, signal);
|
|
1854
|
+
return { ...estimate, clickPoint, viewport };
|
|
1855
|
+
};
|
|
1856
|
+
let clickError;
|
|
1857
|
+
try {
|
|
1858
|
+
if (pointerId) {
|
|
1859
|
+
const started = (await withNavigationBudget(() => this.extensionBridge.getViewportMetrics(0, { operation: 'start', id: pointerId }, remaining()), deadline, signal));
|
|
1860
|
+
pointerDocumentId = started.documentId;
|
|
1861
|
+
if (!pointerDocumentId) {
|
|
1862
|
+
// noinspection ExceptionCaughtLocallyJS -- release the owned pointer observer after failure
|
|
1863
|
+
throw new OsMouseError('TURNSTILE_OS_CLICK_UNAVAILABLE', '原生指针观察器未取得文档身份', '重新检查当前受控页面');
|
|
1864
|
+
}
|
|
1865
|
+
}
|
|
1866
|
+
const screen = await resolvePoint(false);
|
|
1867
|
+
// 请求取消后仍须等待必要的鼠标释放完成,再结束 tab 锁
|
|
1868
|
+
await osLeftClick(screen.x, screen.y, {
|
|
1869
|
+
deadline,
|
|
1870
|
+
signal,
|
|
1871
|
+
refreshPoint: async (probe) => {
|
|
1872
|
+
while (true) {
|
|
1873
|
+
let point = await resolvePoint(true);
|
|
1874
|
+
if (!pointerId || !probe) {
|
|
1875
|
+
return point;
|
|
1876
|
+
}
|
|
1877
|
+
const request = await probe();
|
|
1878
|
+
const origin = (await withNavigationBudget(() => this.extensionBridge.getViewportMetrics(0, { operation: 'read', id: pointerId, documentId: pointerDocumentId, ...request }, remaining()), deadline, signal));
|
|
1879
|
+
if (origin.stable === false && deadline !== undefined) {
|
|
1880
|
+
continue;
|
|
1881
|
+
}
|
|
1882
|
+
if (origin.stable === false ||
|
|
1883
|
+
typeof origin.x !== 'number' ||
|
|
1884
|
+
!Number.isFinite(origin.x) ||
|
|
1885
|
+
typeof origin.y !== 'number' ||
|
|
1886
|
+
!Number.isFinite(origin.y)) {
|
|
1887
|
+
throw new OsMouseError('TURNSTILE_OS_CLICK_UNAVAILABLE', '未取得可信原生指针原点', '重新检查当前受控页面');
|
|
1888
|
+
}
|
|
1889
|
+
point = await resolvePoint(true);
|
|
1890
|
+
return {
|
|
1891
|
+
x: Math.round(origin.x + point.clickPoint.x * point.viewport.devicePixelRatio),
|
|
1892
|
+
y: Math.round(origin.y + point.clickPoint.y * point.viewport.devicePixelRatio),
|
|
1893
|
+
};
|
|
1894
|
+
}
|
|
1895
|
+
},
|
|
1896
|
+
});
|
|
1897
|
+
}
|
|
1898
|
+
catch (error) {
|
|
1899
|
+
clickError = { error };
|
|
1900
|
+
}
|
|
1901
|
+
if (pointerId) {
|
|
1902
|
+
try {
|
|
1903
|
+
await this.tabLockContext.run({
|
|
1904
|
+
...scope,
|
|
1905
|
+
session: this,
|
|
1906
|
+
signal: undefined,
|
|
1907
|
+
deadline: Date.now() + 5000,
|
|
1908
|
+
budgetError: undefined,
|
|
1909
|
+
userAction: undefined,
|
|
1910
|
+
pendingPageCondition: null,
|
|
1911
|
+
keyboardFocus: undefined,
|
|
1912
|
+
inputCleanup: undefined,
|
|
1913
|
+
}, () => this.extensionBridge.getViewportMetrics(0, { operation: 'stop', id: pointerId }, 5000));
|
|
1914
|
+
}
|
|
1915
|
+
catch (error) {
|
|
1916
|
+
console.warn('[MCP] Native pointer observer cleanup failed:', error);
|
|
1917
|
+
if (clickError?.error instanceof Error) {
|
|
1918
|
+
Object.assign(clickError.error, { pointerCleanupError: error });
|
|
1919
|
+
}
|
|
1920
|
+
clickError ??= { error };
|
|
1921
|
+
}
|
|
1922
|
+
}
|
|
1923
|
+
if (clickError) {
|
|
1924
|
+
throw clickError.error;
|
|
1925
|
+
}
|
|
1335
1926
|
}
|
|
1336
1927
|
// noinspection JSUnusedGlobalSymbols — resolveChallenge 通过 ChallengeHost 结构类型调用
|
|
1337
1928
|
async inspectChallenge(options, timeout) {
|
|
@@ -1340,6 +1931,9 @@ class UnifiedSessionManager {
|
|
|
1340
1931
|
}
|
|
1341
1932
|
const deadline = timeout === undefined ? undefined : Date.now() + timeout;
|
|
1342
1933
|
const state = await withNavigationBudget(() => this.getLiveState(), deadline);
|
|
1934
|
+
if (/^about:blank(?:[?#]|$)/.test(state?.url ?? '')) {
|
|
1935
|
+
return this.evaluate(inspectChallengePage.toString(), 'precise', deadline === undefined ? undefined : remainingNavigationTimeout(deadline), [options]);
|
|
1936
|
+
}
|
|
1343
1937
|
const title = (state?.title ?? '').trim().toLowerCase().replace(/…/g, '...');
|
|
1344
1938
|
const challengeTitle = options.titleNeedles.some((needle) => title.includes(needle.toLowerCase()));
|
|
1345
1939
|
const detach = () => withNavigationBudget(() => this.extensionBridge.debuggerDetach(undefined, deadline === undefined ? undefined : remainingNavigationTimeout(deadline)), deadline);
|
|
@@ -1360,10 +1954,20 @@ class UnifiedSessionManager {
|
|
|
1360
1954
|
/**
|
|
1361
1955
|
* 鼠标滚轮
|
|
1362
1956
|
*/
|
|
1363
|
-
async mouseWheel(deltaX, deltaY) {
|
|
1364
|
-
|
|
1365
|
-
const
|
|
1366
|
-
|
|
1957
|
+
async mouseWheel(deltaX, deltaY, timeout, point) {
|
|
1958
|
+
this.assertRequestActive();
|
|
1959
|
+
const deadline = timeout === undefined ? undefined : Date.now() + timeout;
|
|
1960
|
+
const remaining = () => (deadline === undefined ? undefined : remainingNavigationTimeout(deadline));
|
|
1961
|
+
const driver = await this.getDriver(remaining());
|
|
1962
|
+
this.assertRequestActive();
|
|
1963
|
+
const { x, y } = point ?? this.currentMousePosition;
|
|
1964
|
+
const dispatch = () => driver.inputMouse('mouseWheel', x, y, { deltaX, deltaY, modifiers: this.modifiers }, remaining());
|
|
1965
|
+
if (point) {
|
|
1966
|
+
await dispatch();
|
|
1967
|
+
}
|
|
1968
|
+
else {
|
|
1969
|
+
await this.withMouseFrame(dispatch);
|
|
1970
|
+
}
|
|
1367
1971
|
}
|
|
1368
1972
|
/**
|
|
1369
1973
|
* 注入反检测脚本
|
|
@@ -1465,17 +2069,29 @@ class UnifiedSessionManager {
|
|
|
1465
2069
|
metadataKey(targetId) {
|
|
1466
2070
|
return `${this.getMode()}:${targetId}`;
|
|
1467
2071
|
}
|
|
2072
|
+
rememberPageMetadata(targetId, metadata) {
|
|
2073
|
+
const lastUsedAt = this.getPageMetadata(targetId).lastUsedAt;
|
|
2074
|
+
this.pageMetadata.set(this.metadataKey(targetId), {
|
|
2075
|
+
...metadata,
|
|
2076
|
+
lastUsedAt: lastUsedAt && lastUsedAt > (metadata.lastUsedAt ?? '') ? lastUsedAt : metadata.lastUsedAt,
|
|
2077
|
+
});
|
|
2078
|
+
}
|
|
1468
2079
|
recordPageUse(targetId, created = false) {
|
|
1469
2080
|
const metadata = this.getPageMetadata(targetId);
|
|
1470
2081
|
this.pageMetadata.set(this.metadataKey(targetId), {
|
|
1471
2082
|
...metadata,
|
|
2083
|
+
origin: created ? 'created' : metadata.origin,
|
|
1472
2084
|
createdBy: created ? 'mcp-chrome' : metadata.createdBy,
|
|
1473
2085
|
createdAt: created ? new Date().toISOString() : metadata.createdAt,
|
|
1474
2086
|
lastUsedAt: new Date().toISOString(),
|
|
1475
2087
|
});
|
|
1476
2088
|
}
|
|
1477
|
-
evaluateViaCdp(script, args, timeout) {
|
|
1478
|
-
|
|
2089
|
+
evaluateViaCdp(script, args, timeout, options) {
|
|
2090
|
+
const session = getCdpSession();
|
|
2091
|
+
if (session.getCurrentFrameId() !== 0 && options.staleContextRetry === 'readOnly') {
|
|
2092
|
+
return Promise.reject(new StructuredToolError('DRIVER_CAPABILITY_ERROR', 'CDP iframe 暂不支持 readOnly context 重试,脚本未执行', '使用 Extension precise 执行无副作用脚本,或显式使用 never 并独立核对结果', { actionExecuted: false, actionStatus: 'not_started', failureStage: 'context', retryable: false }));
|
|
2093
|
+
}
|
|
2094
|
+
return session.evaluate(script, args, timeout, options.userAction);
|
|
1479
2095
|
}
|
|
1480
2096
|
async evaluateViaExtensionPrecise(code, expression, args, timeout, options = {}) {
|
|
1481
2097
|
const hasArgs = args !== undefined;
|
|
@@ -1492,7 +2108,7 @@ class UnifiedSessionManager {
|
|
|
1492
2108
|
const argsStr = args.map((a) => JSON.stringify(a)).join(', ');
|
|
1493
2109
|
iframeExpression = `(${code})(${argsStr})`;
|
|
1494
2110
|
}
|
|
1495
|
-
const result = (await this.extensionBridge.evaluateInFrame(currentFrameId, iframeExpression, timeout, staleContextRetry));
|
|
2111
|
+
const result = (await this.extensionBridge.evaluateInFrame(currentFrameId, iframeExpression, timeout, staleContextRetry, options.userAction));
|
|
1496
2112
|
if (result.frameContext) {
|
|
1497
2113
|
this.lastEvaluationMetadata = {
|
|
1498
2114
|
retryAttempted: result.retryAttempted === true,
|
|
@@ -1511,14 +2127,14 @@ class UnifiedSessionManager {
|
|
|
1511
2127
|
if (timeout !== undefined) {
|
|
1512
2128
|
params.timeout = timeout;
|
|
1513
2129
|
}
|
|
1514
|
-
const result = (await this.extensionBridge.debuggerSend('Runtime.evaluate', params, undefined, timeout));
|
|
2130
|
+
const result = (await this.extensionBridge.debuggerSend('Runtime.evaluate', params, undefined, timeout, options.userAction));
|
|
1515
2131
|
if (result.exceptionDetails) {
|
|
1516
|
-
throw new Error(formatCdpException(result.exceptionDetails));
|
|
2132
|
+
throw setErrorSource(new Error(formatCdpException(result.exceptionDetails)), 'page');
|
|
1517
2133
|
}
|
|
1518
2134
|
return this.materializeCdpResult(result.result, timeout, options.fileOutput);
|
|
1519
2135
|
}
|
|
1520
|
-
async evaluateViaExtensionStealth(expression, timeout) {
|
|
1521
|
-
return (await this.extensionBridge.evaluate(expression, undefined, timeout));
|
|
2136
|
+
async evaluateViaExtensionStealth(expression, timeout, userAction) {
|
|
2137
|
+
return (await this.extensionBridge.evaluate(expression, undefined, timeout, userAction));
|
|
1522
2138
|
}
|
|
1523
2139
|
async readViewportMetrics(frameId) {
|
|
1524
2140
|
let raw;
|
|
@@ -1547,7 +2163,8 @@ class UnifiedSessionManager {
|
|
|
1547
2163
|
}
|
|
1548
2164
|
catch (err) {
|
|
1549
2165
|
const msg = err instanceof Error ? err.message : String(err);
|
|
1550
|
-
if (
|
|
2166
|
+
if (!isNativeExecutionResultUnknown(err) &&
|
|
2167
|
+
(isExtensionDisconnected(err) || msg.includes('not connected'))) {
|
|
1551
2168
|
// 等待 Extension 重连
|
|
1552
2169
|
await new Promise((r) => setTimeout(r, 2000));
|
|
1553
2170
|
if (this.isExtensionConnected()) {
|
|
@@ -1584,7 +2201,7 @@ class UnifiedSessionManager {
|
|
|
1584
2201
|
*/
|
|
1585
2202
|
checkCdpResult(result) {
|
|
1586
2203
|
if (result.exceptionDetails) {
|
|
1587
|
-
throw new Error(formatCdpException(result.exceptionDetails));
|
|
2204
|
+
throw setErrorSource(new Error(formatCdpException(result.exceptionDetails)), 'page');
|
|
1588
2205
|
}
|
|
1589
2206
|
return extractCdpValue(result.result);
|
|
1590
2207
|
}
|
|
@@ -1592,18 +2209,24 @@ class UnifiedSessionManager {
|
|
|
1592
2209
|
if (!result?.objectId) {
|
|
1593
2210
|
return extractCdpValue(result);
|
|
1594
2211
|
}
|
|
2212
|
+
const state = {
|
|
2213
|
+
seen: new Set(),
|
|
2214
|
+
objectIds: new Set([result.objectId]),
|
|
2215
|
+
nodes: 0,
|
|
2216
|
+
chars: 0,
|
|
2217
|
+
maxNodes: fileOutput ? 100_000 : UnifiedSessionManager.CDP_MATERIALIZE_MAX_NODES,
|
|
2218
|
+
maxDepth: fileOutput ? 64 : UnifiedSessionManager.CDP_MATERIALIZE_MAX_DEPTH,
|
|
2219
|
+
maxChars: fileOutput
|
|
2220
|
+
? UnifiedSessionManager.CDP_MATERIALIZE_MAX_FILE_CHARS
|
|
2221
|
+
: UnifiedSessionManager.CDP_MATERIALIZE_MAX_CHARS,
|
|
2222
|
+
};
|
|
1595
2223
|
try {
|
|
1596
|
-
return await this.materializeRemoteObject(result, timeout,
|
|
1597
|
-
seen: new Set(),
|
|
1598
|
-
nodes: 0,
|
|
1599
|
-
chars: 0,
|
|
1600
|
-
maxChars: fileOutput
|
|
1601
|
-
? UnifiedSessionManager.CDP_MATERIALIZE_MAX_FILE_CHARS
|
|
1602
|
-
: UnifiedSessionManager.CDP_MATERIALIZE_MAX_CHARS,
|
|
1603
|
-
}, 0);
|
|
2224
|
+
return await this.materializeRemoteObject(result, timeout, state, 0);
|
|
1604
2225
|
}
|
|
1605
2226
|
finally {
|
|
1606
|
-
|
|
2227
|
+
for (const objectId of state.objectIds) {
|
|
2228
|
+
this.extensionBridge.debuggerSend('Runtime.releaseObject', { objectId }).catch(() => { });
|
|
2229
|
+
}
|
|
1607
2230
|
}
|
|
1608
2231
|
}
|
|
1609
2232
|
async materializeRemoteObject(result, timeout, state, depth) {
|
|
@@ -1613,6 +2236,18 @@ class UnifiedSessionManager {
|
|
|
1613
2236
|
if (isKnownNonSerializableRemoteObject(result) || state.seen.has(result.objectId)) {
|
|
1614
2237
|
throw new NonSerializableEvaluateResultError(result);
|
|
1615
2238
|
}
|
|
2239
|
+
if (state.seen.size > 0) {
|
|
2240
|
+
// 同一对象可有多个 CDP 句柄,循环检查按当前祖先对象的实际身份比较
|
|
2241
|
+
const identity = (await this.extensionBridge.debuggerSend('Runtime.callFunctionOn', {
|
|
2242
|
+
objectId: result.objectId,
|
|
2243
|
+
functionDeclaration: 'function () { for (let index = 0; index < arguments.length; ++index) { if (arguments[index] === this) { return true; } } return false; }',
|
|
2244
|
+
arguments: [...state.seen].map((objectId) => ({ objectId })),
|
|
2245
|
+
returnByValue: true,
|
|
2246
|
+
}, undefined, timeout));
|
|
2247
|
+
if (this.checkCdpResult(identity)) {
|
|
2248
|
+
throw new NonSerializableEvaluateResultError(result);
|
|
2249
|
+
}
|
|
2250
|
+
}
|
|
1616
2251
|
this.trackMaterializedNode(result, state, depth);
|
|
1617
2252
|
state.seen.add(result.objectId);
|
|
1618
2253
|
try {
|
|
@@ -1622,24 +2257,51 @@ class UnifiedSessionManager {
|
|
|
1622
2257
|
accessorPropertiesOnly: false,
|
|
1623
2258
|
generatePreview: false,
|
|
1624
2259
|
}, undefined, timeout));
|
|
2260
|
+
for (const descriptors of [
|
|
2261
|
+
properties.result,
|
|
2262
|
+
properties.internalProperties,
|
|
2263
|
+
properties.privateProperties,
|
|
2264
|
+
]) {
|
|
2265
|
+
for (const descriptor of descriptors ?? []) {
|
|
2266
|
+
for (const value of [descriptor.value, descriptor.get, descriptor.set, descriptor.symbol]) {
|
|
2267
|
+
if (value?.objectId) {
|
|
2268
|
+
state.objectIds.add(value.objectId);
|
|
2269
|
+
}
|
|
2270
|
+
}
|
|
2271
|
+
}
|
|
2272
|
+
}
|
|
1625
2273
|
if (properties.exceptionDetails) {
|
|
1626
|
-
throw new Error(formatCdpException(properties.exceptionDetails));
|
|
2274
|
+
throw setErrorSource(new Error(formatCdpException(properties.exceptionDetails)), 'page');
|
|
1627
2275
|
}
|
|
1628
2276
|
const descriptors = properties.result ?? [];
|
|
1629
2277
|
state.nodes += descriptors.length;
|
|
1630
2278
|
this.assertMaterializeBudget(state, depth, result);
|
|
1631
2279
|
if (result.subtype === 'array') {
|
|
1632
|
-
const
|
|
2280
|
+
const length = descriptors.find((descriptor) => descriptor.name === 'length')?.value?.value;
|
|
2281
|
+
if (typeof length !== 'number' ||
|
|
2282
|
+
!Number.isInteger(length) ||
|
|
2283
|
+
length < 0 ||
|
|
2284
|
+
length > (state.maxNodes ?? UnifiedSessionManager.CDP_MATERIALIZE_MAX_NODES)) {
|
|
2285
|
+
this.throwMaterializeLimit(state, depth, result);
|
|
2286
|
+
}
|
|
2287
|
+
const populated = descriptors.filter((descriptor) => /^\d+$/.test(descriptor.name) &&
|
|
2288
|
+
Number(descriptor.name) < length &&
|
|
2289
|
+
descriptor.value).length;
|
|
2290
|
+
const holes = length - populated;
|
|
2291
|
+
state.nodes += holes;
|
|
2292
|
+
state.chars += holes * 5;
|
|
2293
|
+
this.assertMaterializeBudget(state, depth, result);
|
|
2294
|
+
const array = new Array(length);
|
|
1633
2295
|
for (const descriptor of descriptors) {
|
|
1634
2296
|
if (!descriptor.value || !/^\d+$/.test(descriptor.name)) {
|
|
1635
2297
|
continue;
|
|
1636
2298
|
}
|
|
1637
2299
|
const index = Number(descriptor.name);
|
|
1638
|
-
if (index >= UnifiedSessionManager.CDP_MATERIALIZE_MAX_NODES) {
|
|
2300
|
+
if (index >= (state.maxNodes ?? UnifiedSessionManager.CDP_MATERIALIZE_MAX_NODES)) {
|
|
1639
2301
|
this.throwMaterializeLimit(state, depth, result);
|
|
1640
2302
|
}
|
|
1641
2303
|
this.trackMaterializedPropertyName(descriptor.name, state, depth, result);
|
|
1642
|
-
array[index] = await this.
|
|
2304
|
+
array[index] = await this.materializeRemoteObject(descriptor.value, timeout, state, depth + 1);
|
|
1643
2305
|
}
|
|
1644
2306
|
return array;
|
|
1645
2307
|
}
|
|
@@ -1649,7 +2311,12 @@ class UnifiedSessionManager {
|
|
|
1649
2311
|
continue;
|
|
1650
2312
|
}
|
|
1651
2313
|
this.trackMaterializedPropertyName(descriptor.name, state, depth, result);
|
|
1652
|
-
object
|
|
2314
|
+
Object.defineProperty(object, descriptor.name, {
|
|
2315
|
+
value: await this.materializeRemoteObject(descriptor.value, timeout, state, depth + 1),
|
|
2316
|
+
enumerable: true,
|
|
2317
|
+
configurable: true,
|
|
2318
|
+
writable: true,
|
|
2319
|
+
});
|
|
1653
2320
|
}
|
|
1654
2321
|
return object;
|
|
1655
2322
|
}
|
|
@@ -1657,17 +2324,6 @@ class UnifiedSessionManager {
|
|
|
1657
2324
|
state.seen.delete(result.objectId);
|
|
1658
2325
|
}
|
|
1659
2326
|
}
|
|
1660
|
-
async materializePropertyValue(value, timeout, state, depth) {
|
|
1661
|
-
if (!value.objectId) {
|
|
1662
|
-
return this.trackMaterializedValue(extractCdpValue(value), state, depth);
|
|
1663
|
-
}
|
|
1664
|
-
try {
|
|
1665
|
-
return await this.materializeRemoteObject(value, timeout, state, depth);
|
|
1666
|
-
}
|
|
1667
|
-
finally {
|
|
1668
|
-
this.extensionBridge.debuggerSend('Runtime.releaseObject', { objectId: value.objectId }).catch(() => { });
|
|
1669
|
-
}
|
|
1670
|
-
}
|
|
1671
2327
|
trackMaterializedNode(result, state, depth) {
|
|
1672
2328
|
state.nodes += 1;
|
|
1673
2329
|
state.chars += result.description?.length ?? 0;
|
|
@@ -1695,16 +2351,16 @@ class UnifiedSessionManager {
|
|
|
1695
2351
|
}
|
|
1696
2352
|
}
|
|
1697
2353
|
assertMaterializeBudget(state, depth, result) {
|
|
1698
|
-
if (depth > UnifiedSessionManager.CDP_MATERIALIZE_MAX_DEPTH ||
|
|
1699
|
-
state.nodes > UnifiedSessionManager.CDP_MATERIALIZE_MAX_NODES ||
|
|
2354
|
+
if (depth > (state.maxDepth ?? UnifiedSessionManager.CDP_MATERIALIZE_MAX_DEPTH) ||
|
|
2355
|
+
state.nodes > (state.maxNodes ?? UnifiedSessionManager.CDP_MATERIALIZE_MAX_NODES) ||
|
|
1700
2356
|
state.chars > state.maxChars) {
|
|
1701
2357
|
this.throwMaterializeLimit(state, depth, result);
|
|
1702
2358
|
}
|
|
1703
2359
|
}
|
|
1704
2360
|
throwMaterializeLimit(state, depth, result) {
|
|
1705
|
-
const exceeded = depth > UnifiedSessionManager.CDP_MATERIALIZE_MAX_DEPTH
|
|
2361
|
+
const exceeded = depth > (state.maxDepth ?? UnifiedSessionManager.CDP_MATERIALIZE_MAX_DEPTH)
|
|
1706
2362
|
? 'depth'
|
|
1707
|
-
: state.nodes > UnifiedSessionManager.CDP_MATERIALIZE_MAX_NODES
|
|
2363
|
+
: state.nodes > (state.maxNodes ?? UnifiedSessionManager.CDP_MATERIALIZE_MAX_NODES)
|
|
1708
2364
|
? 'nodes'
|
|
1709
2365
|
: 'chars';
|
|
1710
2366
|
throw new EvaluateResultTooLargeError({
|
|
@@ -1712,8 +2368,8 @@ class UnifiedSessionManager {
|
|
|
1712
2368
|
depth,
|
|
1713
2369
|
nodes: state.nodes,
|
|
1714
2370
|
chars: state.chars,
|
|
1715
|
-
maxDepth: UnifiedSessionManager.CDP_MATERIALIZE_MAX_DEPTH,
|
|
1716
|
-
maxNodes: UnifiedSessionManager.CDP_MATERIALIZE_MAX_NODES,
|
|
2371
|
+
maxDepth: state.maxDepth ?? UnifiedSessionManager.CDP_MATERIALIZE_MAX_DEPTH,
|
|
2372
|
+
maxNodes: state.maxNodes ?? UnifiedSessionManager.CDP_MATERIALIZE_MAX_NODES,
|
|
1717
2373
|
maxChars: state.maxChars,
|
|
1718
2374
|
type: result?.type,
|
|
1719
2375
|
subtype: result?.subtype,
|
|
@@ -1748,6 +2404,7 @@ class UnifiedSessionManager {
|
|
|
1748
2404
|
* 避免工具 timeout 被连接等待吞掉,不传则使用默认 30s
|
|
1749
2405
|
*/
|
|
1750
2406
|
async getDriver(timeout) {
|
|
2407
|
+
this.assertRequestActive();
|
|
1751
2408
|
const scopedMode = this.tabLockContext.getStore()?.mode;
|
|
1752
2409
|
if (scopedMode === 'extension') {
|
|
1753
2410
|
if (!this.extensionBridge?.isConnected()) {
|
|
@@ -1775,6 +2432,7 @@ class UnifiedSessionManager {
|
|
|
1775
2432
|
return getCdpSession();
|
|
1776
2433
|
}
|
|
1777
2434
|
async ensureExtensionConnected(maxWait) {
|
|
2435
|
+
this.assertRequestActive();
|
|
1778
2436
|
if (!this.extensionBridge) {
|
|
1779
2437
|
return this.assertCdpFallbackAllowed();
|
|
1780
2438
|
}
|
|
@@ -1803,7 +2461,9 @@ class UnifiedSessionManager {
|
|
|
1803
2461
|
}
|
|
1804
2462
|
console.error(`[MCP] Waiting for Chrome Extension connection (${waitTimeout}ms timeout)...`);
|
|
1805
2463
|
console.error('[MCP] Please ensure Chrome is running with MCP Chrome extension installed.');
|
|
1806
|
-
const
|
|
2464
|
+
const scope = this.tabLockContext.getStore();
|
|
2465
|
+
const connected = await withNavigationBudget(() => this.extensionBridge.waitForConnection(waitTimeout), scope?.deadline, scope?.signal);
|
|
2466
|
+
this.assertRequestActive();
|
|
1807
2467
|
if (connected) {
|
|
1808
2468
|
console.error('[MCP] Chrome Extension connected successfully');
|
|
1809
2469
|
this.lastConnectionFailure = 0;
|
|
@@ -1843,9 +2503,9 @@ class UnifiedSessionManager {
|
|
|
1843
2503
|
if (timeout !== undefined) {
|
|
1844
2504
|
params.timeout = timeout;
|
|
1845
2505
|
}
|
|
1846
|
-
const result = (await this.extensionBridge.debuggerSend('Runtime.callFunctionOn', params, undefined, timeout));
|
|
2506
|
+
const result = (await this.extensionBridge.debuggerSend('Runtime.callFunctionOn', params, undefined, timeout, options.userAction));
|
|
1847
2507
|
if (result.exceptionDetails) {
|
|
1848
|
-
throw new Error(formatCdpException(result.exceptionDetails));
|
|
2508
|
+
throw setErrorSource(new Error(formatCdpException(result.exceptionDetails)), 'page');
|
|
1849
2509
|
}
|
|
1850
2510
|
return await this.materializeCdpResult(result.result, timeout, options.fileOutput);
|
|
1851
2511
|
}
|
|
@@ -1855,6 +2515,34 @@ class UnifiedSessionManager {
|
|
|
1855
2515
|
}).catch(() => { });
|
|
1856
2516
|
}
|
|
1857
2517
|
}
|
|
2518
|
+
async withMouseFrame(fn) {
|
|
2519
|
+
const bridge = this.isExtensionConnected() ? this.extensionBridge : null;
|
|
2520
|
+
const original = this.mouseFrameContext?.targetId === this.getCurrentTargetId() ? this.mouseFrameContext : undefined;
|
|
2521
|
+
if (!bridge || !original) {
|
|
2522
|
+
return fn();
|
|
2523
|
+
}
|
|
2524
|
+
const frameId = bridge.getCurrentFrameId();
|
|
2525
|
+
const documentId = bridge.getCurrentFrameDocumentId();
|
|
2526
|
+
bridge.setCurrentFrameId(original.frameId, original.documentId);
|
|
2527
|
+
try {
|
|
2528
|
+
return await fn();
|
|
2529
|
+
}
|
|
2530
|
+
finally {
|
|
2531
|
+
bridge.setCurrentFrameId(frameId, documentId);
|
|
2532
|
+
}
|
|
2533
|
+
}
|
|
2534
|
+
rememberMouseFrame() {
|
|
2535
|
+
const bridge = this.isExtensionConnected() ? this.extensionBridge : null;
|
|
2536
|
+
const targetId = bridge?.getCurrentTargetId();
|
|
2537
|
+
this.mouseFrameContext =
|
|
2538
|
+
bridge && targetId
|
|
2539
|
+
? {
|
|
2540
|
+
targetId,
|
|
2541
|
+
frameId: bridge.getCurrentFrameId(),
|
|
2542
|
+
documentId: bridge.getCurrentFrameDocumentId(),
|
|
2543
|
+
}
|
|
2544
|
+
: undefined;
|
|
2545
|
+
}
|
|
1858
2546
|
}
|
|
1859
2547
|
/**
|
|
1860
2548
|
* 获取统一会话管理器实例
|