@pyrokine/mcp-chrome 2.4.4 → 3.0.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 +495 -115
- package/README_zh.md +423 -128
- package/dist/anti-detection/injection.js +3 -3
- package/dist/cdp/client.d.ts +1 -1
- package/dist/cdp/client.d.ts.map +1 -1
- package/dist/cdp/client.js +18 -7
- package/dist/cdp/client.js.map +1 -1
- package/dist/cdp/launcher.d.ts +2 -3
- package/dist/cdp/launcher.d.ts.map +1 -1
- package/dist/cdp/launcher.js +84 -24
- package/dist/cdp/launcher.js.map +1 -1
- package/dist/core/auto-wait.d.ts +3 -3
- package/dist/core/auto-wait.d.ts.map +1 -1
- package/dist/core/auto-wait.js +45 -20
- package/dist/core/auto-wait.js.map +1 -1
- package/dist/core/browser-driver.d.ts +105 -15
- package/dist/core/browser-driver.d.ts.map +1 -1
- package/dist/core/browser-driver.js.map +1 -1
- package/dist/core/errors.d.ts +22 -4
- package/dist/core/errors.d.ts.map +1 -1
- package/dist/core/errors.js +90 -7
- package/dist/core/errors.js.map +1 -1
- package/dist/core/locator.d.ts +7 -0
- package/dist/core/locator.d.ts.map +1 -1
- package/dist/core/locator.js +54 -29
- package/dist/core/locator.js.map +1 -1
- package/dist/core/os-mouse.d.ts +15 -2
- package/dist/core/os-mouse.d.ts.map +1 -1
- package/dist/core/os-mouse.js +52 -8
- package/dist/core/os-mouse.js.map +1 -1
- package/dist/core/session.d.ts +39 -22
- package/dist/core/session.d.ts.map +1 -1
- package/dist/core/session.js +632 -153
- package/dist/core/session.js.map +1 -1
- package/dist/core/types.d.ts +2 -0
- package/dist/core/types.d.ts.map +1 -1
- package/dist/core/types.js.map +1 -1
- package/dist/core/unified-session.d.ts +105 -27
- package/dist/core/unified-session.d.ts.map +1 -1
- package/dist/core/unified-session.js +554 -138
- package/dist/core/unified-session.js.map +1 -1
- package/dist/core/utils.d.ts +5 -0
- package/dist/core/utils.d.ts.map +1 -1
- package/dist/core/utils.js +475 -54
- package/dist/core/utils.js.map +1 -1
- package/dist/extension/bridge.d.ts +49 -39
- package/dist/extension/bridge.d.ts.map +1 -1
- package/dist/extension/bridge.js +318 -131
- package/dist/extension/bridge.js.map +1 -1
- package/dist/extension/http-server.d.ts +3 -0
- package/dist/extension/http-server.d.ts.map +1 -1
- package/dist/extension/http-server.js +28 -2
- package/dist/extension/http-server.js.map +1 -1
- package/dist/index.js +7 -0
- package/dist/index.js.map +1 -1
- package/dist/tools/browse.d.ts.map +1 -1
- package/dist/tools/browse.js +248 -172
- package/dist/tools/browse.js.map +1 -1
- package/dist/tools/challenge.d.ts +1 -1
- package/dist/tools/challenge.d.ts.map +1 -1
- package/dist/tools/challenge.js +86 -51
- package/dist/tools/challenge.js.map +1 -1
- package/dist/tools/cookies.d.ts.map +1 -1
- package/dist/tools/cookies.js +5 -3
- package/dist/tools/cookies.js.map +1 -1
- package/dist/tools/diagnostics.d.ts +3 -3
- package/dist/tools/diagnostics.d.ts.map +1 -1
- package/dist/tools/diagnostics.js +13 -12
- package/dist/tools/diagnostics.js.map +1 -1
- package/dist/tools/evaluate.d.ts.map +1 -1
- package/dist/tools/evaluate.js +67 -26
- package/dist/tools/evaluate.js.map +1 -1
- package/dist/tools/extract.d.ts.map +1 -1
- package/dist/tools/extract.js +137 -197
- package/dist/tools/extract.js.map +1 -1
- package/dist/tools/input.d.ts +1 -1
- package/dist/tools/input.d.ts.map +1 -1
- package/dist/tools/input.js +445 -197
- package/dist/tools/input.js.map +1 -1
- package/dist/tools/logs.d.ts.map +1 -1
- package/dist/tools/logs.js +5 -5
- package/dist/tools/logs.js.map +1 -1
- package/dist/tools/manage.d.ts.map +1 -1
- package/dist/tools/manage.js +395 -86
- package/dist/tools/manage.js.map +1 -1
- package/dist/tools/page-context.d.ts +17 -0
- package/dist/tools/page-context.d.ts.map +1 -0
- package/dist/tools/page-context.js +103 -0
- package/dist/tools/page-context.js.map +1 -0
- package/dist/tools/png.d.ts +5 -0
- package/dist/tools/png.d.ts.map +1 -1
- package/dist/tools/png.js +84 -19
- package/dist/tools/png.js.map +1 -1
- package/dist/tools/post-condition.d.ts.map +1 -1
- package/dist/tools/post-condition.js +24 -7
- package/dist/tools/post-condition.js.map +1 -1
- package/dist/tools/schema.d.ts +9 -0
- package/dist/tools/schema.d.ts.map +1 -1
- package/dist/tools/schema.js +15 -0
- package/dist/tools/schema.js.map +1 -1
- package/dist/tools/target-diagnostics.d.ts +9 -1
- package/dist/tools/target-diagnostics.d.ts.map +1 -1
- package/dist/tools/target-diagnostics.js +89 -33
- package/dist/tools/target-diagnostics.js.map +1 -1
- package/dist/tools/wait.d.ts.map +1 -1
- package/dist/tools/wait.js +38 -32
- package/dist/tools/wait.js.map +1 -1
- package/extension/dist/assets/content.ts-loader-DUWbDHRS.js +13 -0
- package/extension/dist/assets/content.ts-qe6IB5DK.js +13 -0
- package/extension/dist/assets/{index.ts-Bqcu2qiN.js → index.ts-D7_LCUoV.js} +2477 -2201
- package/extension/dist/assets/page-scripts-D508WLkW.js +1378 -0
- package/extension/dist/assets/{popup.html-B81xccfq.js → popup.html-B-eVq48O.js} +54 -5
- package/extension/dist/manifest.json +4 -3
- package/extension/dist/service-worker-loader.js +1 -1
- package/extension/dist/src/popup/popup.html +11 -1
- package/package.json +16 -8
- package/scripts/start-chrome.ps1 +58 -0
- package/extension/dist/assets/content.ts-Cj_iiU2s.js +0 -25
package/dist/tools/input.js
CHANGED
|
@@ -11,16 +11,26 @@
|
|
|
11
11
|
import { z } from 'zod';
|
|
12
12
|
import { generateBezierPath, getMouseMoveDelay, getTypingDelay, randomDelay } from '../anti-detection/index.js';
|
|
13
13
|
import { formatErrorResponse, formatResponse, getSession, getUnifiedSession } from '../core/index.js';
|
|
14
|
+
import { ElementNotFoundError, StructuredToolError, TimeoutError } from '../core/errors.js';
|
|
15
|
+
import { DEFAULT_TIMEOUT } from '../core/types.js';
|
|
14
16
|
import { ChallengeDeniedError, ChallengeTimeoutError, resolveChallenge } from './challenge.js';
|
|
15
17
|
import { appendDiagnostics, finishDiagnostics, startDiagnostics } from './diagnostics.js';
|
|
18
|
+
import { withPageContext } from './page-context.js';
|
|
16
19
|
import { postConditionSchema, waitForPostCondition } from './post-condition.js';
|
|
17
|
-
import { targetToFindParams, targetZodSchema } from './schema.js';
|
|
18
|
-
import { buildTargetDiagnostics } from './target-diagnostics.js';
|
|
20
|
+
import { pageConditionSchema, pageTargetIdSchema, resolvePageTargetId, targetToFindParams, targetZodSchema, } from './schema.js';
|
|
21
|
+
import { buildTargetDiagnostics, enrichTargetError, targetLookupTimeout } from './target-diagnostics.js';
|
|
19
22
|
const INPUT_WAIT_MAX_MS = 60_000;
|
|
20
23
|
const TEXT_SELECTION_INPUT_TYPES = new Set(['text', 'search', 'tel', 'url', 'password']);
|
|
21
24
|
function throwInputTimeoutError(timeout) {
|
|
22
25
|
throw new Error(`input timeout before event dispatch (${timeout}ms)`);
|
|
23
26
|
}
|
|
27
|
+
function remainingInputTimeout(deadline) {
|
|
28
|
+
const remaining = deadline - Date.now();
|
|
29
|
+
if (remaining <= 0) {
|
|
30
|
+
throw new TimeoutError('input timeout during event execution');
|
|
31
|
+
}
|
|
32
|
+
return remaining;
|
|
33
|
+
}
|
|
24
34
|
export function supportsTextSelection(tag, inputType) {
|
|
25
35
|
return (tag.toLowerCase() === 'textarea' ||
|
|
26
36
|
(tag.toLowerCase() === 'input' && TEXT_SELECTION_INPUT_TYPES.has((inputType ?? 'text').toLowerCase())));
|
|
@@ -126,6 +136,7 @@ const inputEventSchema = z.object({
|
|
|
126
136
|
* input 参数 schema
|
|
127
137
|
*/
|
|
128
138
|
const inputSchema = z.object({
|
|
139
|
+
...pageConditionSchema,
|
|
129
140
|
events: z.array(inputEventSchema).describe('事件序列'),
|
|
130
141
|
humanize: z.boolean().optional().describe('启用人类行为模拟(贝塞尔曲线移动、随机延迟)'),
|
|
131
142
|
diagnostics: z
|
|
@@ -135,49 +146,71 @@ const inputSchema = z.object({
|
|
|
135
146
|
postCondition: postConditionSchema
|
|
136
147
|
.optional()
|
|
137
148
|
.describe('动作执行后要验证的页面状态;不传时 success 只表示事件已发出,不表示业务结果已达成'),
|
|
138
|
-
|
|
139
|
-
|
|
149
|
+
targetId: pageTargetIdSchema.describe('目标页面 ID(Extension 或 CDP),优先于默认操作目标'),
|
|
150
|
+
tabId: pageTargetIdSchema.describe('旧版目标 Tab ID,与 targetId 相同语义,保留兼容'),
|
|
151
|
+
timeout: z
|
|
152
|
+
.number()
|
|
140
153
|
.optional()
|
|
141
|
-
.describe('
|
|
142
|
-
timeout: z.number().optional().describe('超时毫秒,含 Cloudflare Challenge 等待和事件派发'),
|
|
154
|
+
.describe('超时毫秒,含 Cloudflare Challenge 等待、目标准备、鼠标移动、字符输入和延迟;到期后停止后续动作,不自动重放'),
|
|
143
155
|
frame: z
|
|
144
156
|
.union([z.string(), z.number()])
|
|
145
157
|
.optional()
|
|
146
158
|
.describe('iframe 定位(可选,仅 Extension 模式),CSS 选择器(如 "iframe#main")或索引(如 0),不指定则在主框架操作'),
|
|
147
159
|
});
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
context;
|
|
152
|
-
constructor(code, message, suggestion, context) {
|
|
153
|
-
super(message);
|
|
154
|
-
this.code = code;
|
|
155
|
-
this.suggestion = suggestion;
|
|
156
|
-
this.context = context;
|
|
157
|
-
this.name = 'StructuredToolError';
|
|
158
|
-
}
|
|
159
|
-
// noinspection JSUnusedGlobalSymbols — formatErrorResponse 通过结构化 toJSON 调用
|
|
160
|
-
toJSON() {
|
|
161
|
-
return {
|
|
162
|
-
error: {
|
|
163
|
-
code: this.code,
|
|
164
|
-
message: this.message,
|
|
165
|
-
suggestion: this.suggestion,
|
|
166
|
-
context: this.context,
|
|
167
|
-
},
|
|
168
|
-
};
|
|
160
|
+
function sanitizeInputResponse(response, events, sensitiveInput) {
|
|
161
|
+
if (!response.isError && !sensitiveInput) {
|
|
162
|
+
return response;
|
|
169
163
|
}
|
|
170
|
-
}
|
|
171
|
-
function sanitizeInputFailureResponse(response, events) {
|
|
172
164
|
const sensitiveValues = events
|
|
173
165
|
.flatMap((event) => [event.find, event.text])
|
|
174
166
|
.filter((value) => !!value);
|
|
175
|
-
if (sensitiveValues.length === 0) {
|
|
176
|
-
return response;
|
|
177
|
-
}
|
|
178
167
|
for (const item of response.content) {
|
|
179
168
|
try {
|
|
180
|
-
|
|
169
|
+
const payload = JSON.parse(item.text);
|
|
170
|
+
for (const key of ['warnings', 'eventResults', 'diagnostics', 'diagnosticsError']) {
|
|
171
|
+
if (key in payload) {
|
|
172
|
+
payload[key] = redactSensitiveInputData(payload[key], sensitiveValues);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
const pageContext = payload.pageContext;
|
|
176
|
+
const postCondition = payload.postCondition;
|
|
177
|
+
const details = [
|
|
178
|
+
payload.error,
|
|
179
|
+
pageContext,
|
|
180
|
+
pageContext?.before,
|
|
181
|
+
pageContext?.after,
|
|
182
|
+
postCondition,
|
|
183
|
+
...(Array.isArray(postCondition?.checks) ? postCondition.checks : []),
|
|
184
|
+
];
|
|
185
|
+
for (const detail of details) {
|
|
186
|
+
if (!detail || typeof detail !== 'object') {
|
|
187
|
+
continue;
|
|
188
|
+
}
|
|
189
|
+
const record = detail;
|
|
190
|
+
for (const key of [
|
|
191
|
+
'message',
|
|
192
|
+
'suggestion',
|
|
193
|
+
'context',
|
|
194
|
+
'contextError',
|
|
195
|
+
'url',
|
|
196
|
+
'title',
|
|
197
|
+
'actual',
|
|
198
|
+
'error',
|
|
199
|
+
]) {
|
|
200
|
+
if (key in record) {
|
|
201
|
+
record[key] = redactSensitiveInputData(record[key], sensitiveValues);
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
if (sensitiveInput) {
|
|
206
|
+
if (payload.diagnostics) {
|
|
207
|
+
payload.diagnostics = { redacted: true, reason: 'sensitive_input_or_unresolved_target' };
|
|
208
|
+
}
|
|
209
|
+
if (payload.diagnosticsError) {
|
|
210
|
+
payload.diagnosticsError = '敏感输入的诊断详情已隐藏';
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
item.text = JSON.stringify(payload, null, 2);
|
|
181
214
|
}
|
|
182
215
|
catch {
|
|
183
216
|
item.text = redactSensitiveInputData(item.text, sensitiveValues);
|
|
@@ -189,6 +222,15 @@ function sanitizeInputFailureResponse(response, events) {
|
|
|
189
222
|
* input 工具处理器
|
|
190
223
|
*/
|
|
191
224
|
async function handleInput(args) {
|
|
225
|
+
const inputStartedAt = Date.now();
|
|
226
|
+
const deadline = args.timeout === undefined ? undefined : inputStartedAt + args.timeout;
|
|
227
|
+
let eventsExecuted = 0;
|
|
228
|
+
let actionMayHaveExecuted = false;
|
|
229
|
+
const privacy = { sensitive: false };
|
|
230
|
+
const progress = {
|
|
231
|
+
actionStatus: 'not_started',
|
|
232
|
+
};
|
|
233
|
+
let failureStage = 'action';
|
|
192
234
|
try {
|
|
193
235
|
if (args.events.length === 0) {
|
|
194
236
|
return {
|
|
@@ -210,16 +252,16 @@ async function handleInput(args) {
|
|
|
210
252
|
}
|
|
211
253
|
const unifiedSession = getUnifiedSession();
|
|
212
254
|
const humanize = args.humanize ?? false;
|
|
213
|
-
|
|
214
|
-
return
|
|
255
|
+
const response = await withPageContext(unifiedSession, resolvePageTargetId(args), async () => {
|
|
256
|
+
return unifiedSession.withPageCondition(args, () => unifiedSession.withFrame(args.frame, async () => {
|
|
215
257
|
const mode = unifiedSession.getMode();
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
258
|
+
if (args.expectedUrl === undefined && args.expectedRevision === undefined) {
|
|
259
|
+
await resolveChallenge(unifiedSession, 'input', 'not_started', deadline === undefined ? undefined : remainingInputTimeout(deadline));
|
|
260
|
+
}
|
|
261
|
+
const diagnostics = await startDiagnostics(unifiedSession, args.diagnostics, deadline);
|
|
219
262
|
const warnings = [];
|
|
220
263
|
const eventResults = [];
|
|
221
264
|
const session = mode === 'cdp' ? getSession() : undefined;
|
|
222
|
-
let eventsExecuted = 0;
|
|
223
265
|
try {
|
|
224
266
|
for (const event of args.events) {
|
|
225
267
|
const eventTimeout = args.timeout === undefined
|
|
@@ -228,7 +270,16 @@ async function handleInput(args) {
|
|
|
228
270
|
if (eventTimeout !== undefined && eventTimeout <= 0) {
|
|
229
271
|
throwInputTimeoutError(args.timeout);
|
|
230
272
|
}
|
|
231
|
-
|
|
273
|
+
progress.actionStatus = 'failed';
|
|
274
|
+
const result = await executeInputEvent({
|
|
275
|
+
unifiedSession,
|
|
276
|
+
session,
|
|
277
|
+
mode,
|
|
278
|
+
humanize,
|
|
279
|
+
timeout: eventTimeout,
|
|
280
|
+
frame: args.frame,
|
|
281
|
+
privacy,
|
|
282
|
+
}, event);
|
|
232
283
|
++eventsExecuted;
|
|
233
284
|
if (typeof result === 'string') {
|
|
234
285
|
warnings.push(result);
|
|
@@ -237,11 +288,16 @@ async function handleInput(args) {
|
|
|
237
288
|
eventResults.push(result);
|
|
238
289
|
}
|
|
239
290
|
}
|
|
291
|
+
if (args.timeout !== undefined && Date.now() - inputStartedAt >= args.timeout) {
|
|
292
|
+
throw new TimeoutError('input timeout after event dispatch');
|
|
293
|
+
}
|
|
240
294
|
}
|
|
241
295
|
catch (error) {
|
|
242
296
|
const response = formatErrorResponse(error);
|
|
243
297
|
const text = response.content[0]?.text;
|
|
244
|
-
if ((error instanceof ChallengeTimeoutError ||
|
|
298
|
+
if ((error instanceof ChallengeTimeoutError ||
|
|
299
|
+
error instanceof ChallengeDeniedError) &&
|
|
300
|
+
text) {
|
|
245
301
|
try {
|
|
246
302
|
const payload = JSON.parse(text);
|
|
247
303
|
Object.assign(payload, {
|
|
@@ -249,13 +305,13 @@ async function handleInput(args) {
|
|
|
249
305
|
remainingEventsNotExecuted: args.events.length - eventsExecuted,
|
|
250
306
|
verificationRequested: Boolean(args.postCondition),
|
|
251
307
|
});
|
|
252
|
-
appendDiagnostics(payload, await finishDiagnostics(unifiedSession, diagnostics));
|
|
308
|
+
appendDiagnostics(payload, await finishDiagnostics(unifiedSession, diagnostics, deadline));
|
|
253
309
|
response.content[0].text = JSON.stringify(payload, null, 2);
|
|
254
310
|
}
|
|
255
311
|
catch {
|
|
256
312
|
// 保留原始 Challenge 错误响应
|
|
257
313
|
}
|
|
258
|
-
return
|
|
314
|
+
return response;
|
|
259
315
|
}
|
|
260
316
|
if (text) {
|
|
261
317
|
try {
|
|
@@ -263,23 +319,39 @@ async function handleInput(args) {
|
|
|
263
319
|
const message = error instanceof Error ? error.message : String(error);
|
|
264
320
|
const timedOut = /timeout|超时/i.test(message);
|
|
265
321
|
const preActionTimeout = /timeout before event dispatch|超过 input 剩余 timeout/i.test(message);
|
|
266
|
-
|
|
322
|
+
actionMayHaveExecuted = timedOut && !preActionTimeout;
|
|
323
|
+
const preconditionFailed = error !== null &&
|
|
324
|
+
typeof error === 'object' &&
|
|
325
|
+
'code' in error &&
|
|
326
|
+
error.code === 'ACTION_PRECONDITION_FAILED';
|
|
327
|
+
actionMayHaveExecuted = actionMayHaveExecuted && !preconditionFailed;
|
|
328
|
+
progress.actionStatus =
|
|
329
|
+
preconditionFailed && eventsExecuted === 0
|
|
330
|
+
? 'not_started'
|
|
331
|
+
: actionMayHaveExecuted
|
|
332
|
+
? 'unknown'
|
|
333
|
+
: 'failed';
|
|
267
334
|
payload.eventsExecuted = eventsExecuted;
|
|
268
335
|
payload.actionExecuted = eventsExecuted > 0 || actionMayHaveExecuted;
|
|
269
|
-
payload.actionStatus =
|
|
336
|
+
payload.actionStatus = progress.actionStatus;
|
|
270
337
|
payload.verificationRequested = Boolean(args.postCondition);
|
|
271
338
|
payload.verificationStatus = 'unavailable';
|
|
272
339
|
payload.failureStage = 'action';
|
|
273
|
-
payload.retryable =
|
|
274
|
-
|
|
340
|
+
payload.retryable =
|
|
341
|
+
eventsExecuted === 0 &&
|
|
342
|
+
!actionMayHaveExecuted &&
|
|
343
|
+
/timeout|超时|disconnect|未连接|context|debugger/i.test(message);
|
|
344
|
+
appendDiagnostics(payload, await finishDiagnostics(unifiedSession, diagnostics, deadline));
|
|
275
345
|
response.content[0].text = JSON.stringify(payload, null, 2);
|
|
276
346
|
}
|
|
277
347
|
catch {
|
|
278
348
|
// 保留原始错误响应
|
|
279
349
|
}
|
|
280
350
|
}
|
|
281
|
-
return
|
|
351
|
+
return response;
|
|
282
352
|
}
|
|
353
|
+
progress.actionStatus = 'completed';
|
|
354
|
+
failureStage = 'context';
|
|
283
355
|
const result = {
|
|
284
356
|
success: true,
|
|
285
357
|
eventsExecuted,
|
|
@@ -297,7 +369,9 @@ async function handleInput(args) {
|
|
|
297
369
|
result.eventResults = eventResults;
|
|
298
370
|
}
|
|
299
371
|
if (args.postCondition) {
|
|
372
|
+
failureStage = 'verification';
|
|
300
373
|
const postCondition = await waitForPostCondition(unifiedSession, args.postCondition, 'input');
|
|
374
|
+
failureStage = 'context';
|
|
301
375
|
result.postCondition = postCondition;
|
|
302
376
|
result.verificationStatus = postCondition.verificationStatus;
|
|
303
377
|
result.retryable = postCondition.retryable;
|
|
@@ -306,10 +380,11 @@ async function handleInput(args) {
|
|
|
306
380
|
result.failureStage = 'verification';
|
|
307
381
|
}
|
|
308
382
|
}
|
|
309
|
-
appendDiagnostics(result, await finishDiagnostics(unifiedSession, diagnostics));
|
|
383
|
+
appendDiagnostics(result, await finishDiagnostics(unifiedSession, diagnostics, deadline));
|
|
310
384
|
return formatResponse(result);
|
|
311
|
-
}); // withFrame
|
|
312
|
-
}); // withTabId
|
|
385
|
+
}, deadline), deadline); // withPageCondition, withFrame
|
|
386
|
+
}, deadline); // withTabId
|
|
387
|
+
return sanitizeInputResponse(response, args.events, privacy.sensitive);
|
|
313
388
|
}
|
|
314
389
|
catch (error) {
|
|
315
390
|
const response = formatErrorResponse(error);
|
|
@@ -318,17 +393,42 @@ async function handleInput(args) {
|
|
|
318
393
|
try {
|
|
319
394
|
const payload = JSON.parse(text);
|
|
320
395
|
const message = error instanceof Error ? error.message : String(error);
|
|
396
|
+
const unavailableCondition = eventsExecuted === 0 &&
|
|
397
|
+
!actionMayHaveExecuted &&
|
|
398
|
+
progress.actionStatus === 'not_started' &&
|
|
399
|
+
error instanceof StructuredToolError &&
|
|
400
|
+
error.code === 'DRIVER_CAPABILITY_ERROR' &&
|
|
401
|
+
error.context.actionExecuted === false &&
|
|
402
|
+
error.context.actionStatus === 'not_started' &&
|
|
403
|
+
error.context.failureStage === 'precondition';
|
|
404
|
+
const preconditionFailed = unavailableCondition ||
|
|
405
|
+
(error !== null &&
|
|
406
|
+
typeof error === 'object' &&
|
|
407
|
+
'code' in error &&
|
|
408
|
+
error.code === 'ACTION_PRECONDITION_FAILED');
|
|
321
409
|
Object.assign(payload, {
|
|
322
|
-
|
|
323
|
-
|
|
410
|
+
eventsExecuted,
|
|
411
|
+
actionExecuted: eventsExecuted > 0 || actionMayHaveExecuted,
|
|
412
|
+
actionStatus: preconditionFailed && eventsExecuted === 0
|
|
413
|
+
? 'not_started'
|
|
414
|
+
: progress.actionStatus === 'not_started'
|
|
415
|
+
? 'failed'
|
|
416
|
+
: progress.actionStatus,
|
|
324
417
|
verificationRequested: Boolean(args.postCondition),
|
|
325
418
|
verificationStatus: 'unavailable',
|
|
326
|
-
failureStage: '
|
|
327
|
-
retryable:
|
|
419
|
+
failureStage: unavailableCondition ? 'precondition' : failureStage,
|
|
420
|
+
retryable: progress.actionStatus !== 'completed' &&
|
|
421
|
+
eventsExecuted === 0 &&
|
|
422
|
+
!actionMayHaveExecuted &&
|
|
423
|
+
!preconditionFailed &&
|
|
424
|
+
/timeout|超时|disconnect|未连接|context|debugger|attach/i.test(message),
|
|
328
425
|
diagnosticsStatus: args.diagnostics ? 'unavailable' : 'disabled',
|
|
329
426
|
});
|
|
330
427
|
if (args.diagnostics) {
|
|
331
|
-
payload.diagnosticsError =
|
|
428
|
+
payload.diagnosticsError =
|
|
429
|
+
eventsExecuted > 0 || actionMayHaveExecuted
|
|
430
|
+
? 'input 执行后未能取得 diagnostics'
|
|
431
|
+
: 'diagnostics 未能在 tab/frame 初始化失败前启动';
|
|
332
432
|
}
|
|
333
433
|
response.content[0].text = JSON.stringify(payload, null, 2);
|
|
334
434
|
}
|
|
@@ -336,10 +436,12 @@ async function handleInput(args) {
|
|
|
336
436
|
// 保留原始错误响应
|
|
337
437
|
}
|
|
338
438
|
}
|
|
339
|
-
return
|
|
439
|
+
return sanitizeInputResponse(response, args.events, privacy.sensitive);
|
|
340
440
|
}
|
|
341
441
|
}
|
|
342
442
|
const unifiedOnlyEventTypes = new Set([
|
|
443
|
+
'keydown',
|
|
444
|
+
'keyup',
|
|
343
445
|
'select',
|
|
344
446
|
'replace',
|
|
345
447
|
'editorContext',
|
|
@@ -347,21 +449,33 @@ const unifiedOnlyEventTypes = new Set([
|
|
|
347
449
|
'editorCommand',
|
|
348
450
|
]);
|
|
349
451
|
async function executeInputEvent(context, event) {
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
452
|
+
const timeout = context.timeout ?? DEFAULT_TIMEOUT;
|
|
453
|
+
const deadline = Date.now() + timeout;
|
|
454
|
+
try {
|
|
455
|
+
if (context.mode === 'extension' || unifiedOnlyEventTypes.has(event.type)) {
|
|
456
|
+
return await executeUnifiedEvent({
|
|
457
|
+
unifiedSession: context.unifiedSession,
|
|
458
|
+
session: context.session,
|
|
459
|
+
mode: context.mode,
|
|
460
|
+
humanize: context.humanize,
|
|
461
|
+
timeout: context.timeout,
|
|
462
|
+
frame: context.frame,
|
|
463
|
+
privacy: context.privacy,
|
|
464
|
+
}, event);
|
|
465
|
+
}
|
|
466
|
+
if (!context.session) {
|
|
467
|
+
throw new Error('CDP 输入事件缺少 session');
|
|
468
|
+
}
|
|
469
|
+
await executeCdpEvent(context.session, event, context.humanize, context.timeout);
|
|
470
|
+
return undefined;
|
|
471
|
+
}
|
|
472
|
+
catch (error) {
|
|
473
|
+
if (context.mode === 'cdp' && error instanceof ElementNotFoundError) {
|
|
474
|
+
const target = error.context.target;
|
|
475
|
+
await enrichTargetError(error, context.unifiedSession, target, { timeout, deadline, frame: context.frame });
|
|
476
|
+
}
|
|
477
|
+
throw error;
|
|
478
|
+
}
|
|
365
479
|
}
|
|
366
480
|
/**
|
|
367
481
|
* 通过真实鼠标事件选中页面文本
|
|
@@ -373,73 +487,94 @@ async function executeInputEvent(context, event) {
|
|
|
373
487
|
* @returns 格式化标签名(如 "code"),若被替换文本在格式化节点内
|
|
374
488
|
*/
|
|
375
489
|
/**
|
|
376
|
-
*
|
|
377
|
-
* select/replace 事件用,保证选区建立前 activeElement 就是目标
|
|
490
|
+
* 命令与 editor 目标只定位并聚焦,不派发鼠标事件
|
|
378
491
|
*/
|
|
379
492
|
async function focusTargetForCommands(unifiedSession, target, timeout, frame) {
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
await
|
|
383
|
-
await unifiedSession.mouseClick('left');
|
|
493
|
+
const deadline = Date.now() + (timeout ?? DEFAULT_TIMEOUT);
|
|
494
|
+
if (unifiedSession.getMode() === 'cdp') {
|
|
495
|
+
await focusCdpTarget(getSession(), target, remainingInputTimeout(deadline));
|
|
384
496
|
return;
|
|
385
497
|
}
|
|
386
498
|
const params = targetToFindParams(target);
|
|
387
|
-
const els =
|
|
499
|
+
const els = 'x' in target && 'y' in target
|
|
500
|
+
? await unifiedSession.findAtPoint(target.x, target.y, undefined, remainingInputTimeout(deadline))
|
|
501
|
+
: await unifiedSession.find(params.selector, params.text, params.xpath, remainingInputTimeout(deadline));
|
|
388
502
|
const nth0 = params.nth ?? 0;
|
|
389
503
|
if (els.length <= nth0) {
|
|
390
504
|
throw new StructuredToolError(els.length === 0 ? 'TARGET_NOT_FOUND' : 'TARGET_INDEX_OUT_OF_RANGE', els.length === 0
|
|
391
505
|
? `未找到命令目标: ${JSON.stringify(target)}`
|
|
392
|
-
: `第 ${nth0} 个命令目标不存在(共 ${els.length} 个)`, '请检查 target、frame 和 target.nth,或先用 extract type=state 查看当前候选元素', await buildTargetDiagnosticContext(unifiedSession, target, els.length, nth0,
|
|
506
|
+
: `第 ${nth0} 个命令目标不存在(共 ${els.length} 个)`, '请检查 target、frame 和 target.nth,或先用 extract type=state 查看当前候选元素', await buildTargetDiagnosticContext(unifiedSession, target, els.length, nth0, remainingInputTimeout(deadline), '命令目标未找到', frame));
|
|
393
507
|
}
|
|
394
508
|
const focused = await unifiedSession.evaluate(`(() => {
|
|
509
|
+
if (Date.now() >= ${deadline}) throw new Error('input timeout before command focus')
|
|
395
510
|
const ref = window.__mcpElementMap?.[${JSON.stringify(els[nth0].refId)}]
|
|
396
511
|
const el = ref?.deref()
|
|
397
512
|
if (!(el instanceof HTMLElement)) {
|
|
398
|
-
|
|
513
|
+
return false
|
|
399
514
|
}
|
|
400
|
-
el.focus()
|
|
515
|
+
el.focus({preventScroll: true})
|
|
401
516
|
return document.activeElement === el
|
|
402
|
-
})()`, undefined,
|
|
517
|
+
})()`, undefined, remainingInputTimeout(deadline));
|
|
403
518
|
if (focused !== true) {
|
|
404
|
-
throw new StructuredToolError('TARGET_FOCUS_FAILED', '命令目标未成功聚焦', '请检查目标是否可编辑、未 disabled 并处于正确 frame', await buildTargetDiagnosticContext(unifiedSession, target, els.length, nth0,
|
|
519
|
+
throw new StructuredToolError('TARGET_FOCUS_FAILED', '命令目标未成功聚焦', '请检查目标是否可编辑、未 disabled 并处于正确 frame', await buildTargetDiagnosticContext(unifiedSession, target, els.length, nth0, remainingInputTimeout(deadline), '命令目标聚焦失败', frame));
|
|
405
520
|
}
|
|
406
521
|
}
|
|
407
522
|
export async function focusCdpTarget(session, target, timeout) {
|
|
408
|
-
const
|
|
409
|
-
const
|
|
523
|
+
const deadline = Date.now() + (timeout ?? DEFAULT_TIMEOUT);
|
|
524
|
+
const focus = `function() {
|
|
525
|
+
if (Date.now() >= ${deadline}) throw new Error('input timeout before command focus');
|
|
410
526
|
if (!(this instanceof HTMLElement)) return false;
|
|
411
527
|
this.focus({preventScroll: true});
|
|
412
528
|
return document.activeElement === this;
|
|
413
|
-
}
|
|
529
|
+
}`;
|
|
530
|
+
const focused = 'x' in target && 'y' in target
|
|
531
|
+
? await session.evaluate(`(() => {
|
|
532
|
+
const el = document.elementFromPoint(${target.x}, ${target.y});
|
|
533
|
+
return el ? (${focus}).call(el) : null;
|
|
534
|
+
})()`, undefined, remainingInputTimeout(deadline))
|
|
535
|
+
: await session
|
|
536
|
+
.createLocator(target, { timeout: targetLookupTimeout(remainingInputTimeout(deadline)) })
|
|
537
|
+
.evaluateOn(focus);
|
|
538
|
+
if (focused === null) {
|
|
539
|
+
throw new StructuredToolError('TARGET_NOT_FOUND', '目标坐标未命中元素', '请检查目标坐标是否在当前页面视口内', {
|
|
540
|
+
target,
|
|
541
|
+
});
|
|
542
|
+
}
|
|
414
543
|
if (!focused) {
|
|
415
|
-
throw new StructuredToolError('TARGET_FOCUS_FAILED', '目标元素未成功聚焦', '
|
|
544
|
+
throw new StructuredToolError('TARGET_FOCUS_FAILED', '目标元素未成功聚焦', '请检查目标是否可聚焦、未 disabled,并确认 target 指向当前页面元素', { target });
|
|
416
545
|
}
|
|
417
546
|
}
|
|
418
547
|
async function focusTargetIfNeeded(unifiedSession, session, mode, target, timeout, frame) {
|
|
419
548
|
if (!target) {
|
|
420
549
|
return;
|
|
421
550
|
}
|
|
551
|
+
const deadline = Date.now() + (timeout ?? DEFAULT_TIMEOUT);
|
|
422
552
|
if ('x' in target || 'y' in target) {
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
553
|
+
await handleUnifiedClick({
|
|
554
|
+
unifiedSession,
|
|
555
|
+
mode: unifiedSession.getMode(),
|
|
556
|
+
humanize: false,
|
|
557
|
+
timeout: remainingInputTimeout(deadline),
|
|
558
|
+
frame,
|
|
559
|
+
}, { type: 'click', target });
|
|
426
560
|
return;
|
|
427
561
|
}
|
|
428
562
|
if (mode === 'cdp') {
|
|
429
563
|
if (!session) {
|
|
430
564
|
throw new Error('CDP locator 聚焦缺少 session');
|
|
431
565
|
}
|
|
432
|
-
await focusCdpTarget(session, target,
|
|
566
|
+
await focusCdpTarget(session, target, remainingInputTimeout(deadline));
|
|
433
567
|
return;
|
|
434
568
|
}
|
|
435
569
|
const params = targetToFindParams(target);
|
|
436
|
-
const els = await unifiedSession.find(params.selector, params.text, params.xpath,
|
|
570
|
+
const els = await unifiedSession.find(params.selector, params.text, params.xpath, remainingInputTimeout(deadline));
|
|
437
571
|
const nth0 = params.nth ?? 0;
|
|
438
572
|
if (els.length <= nth0) {
|
|
439
573
|
throw new StructuredToolError(els.length === 0 ? 'TARGET_NOT_FOUND' : 'TARGET_INDEX_OUT_OF_RANGE', els.length === 0
|
|
440
574
|
? `未找到目标元素: ${JSON.stringify(target)}`
|
|
441
575
|
: `第 ${nth0} 个匹配元素不存在(共 ${els.length} 个)`, '请检查 target、frame 和 target.nth,或先用 extract type=state 查看当前候选元素', await buildTargetDiagnosticContext(unifiedSession, target, els.length, nth0, timeout, '聚焦目标未找到', frame));
|
|
442
576
|
}
|
|
577
|
+
remainingInputTimeout(deadline);
|
|
443
578
|
const result = await unifiedSession.actionableClick(els[nth0].refId);
|
|
444
579
|
if (!result.success) {
|
|
445
580
|
throw new StructuredToolError('ACTIONABILITY_FAILED', result.error || '目标元素无法聚焦', result.suggestions?.[0] ?? '请检查元素是否可见、未被遮挡并处于正确 frame', {
|
|
@@ -565,7 +700,12 @@ async function collectTextSelectionDiagnostics(unifiedSession, findText, scopeTa
|
|
|
565
700
|
};
|
|
566
701
|
}`, undefined, timeout, [findText, occurrenceNth, scopeNth, scopeTarget ?? null, scopeSelector, scopeText, scopeXpath]);
|
|
567
702
|
}
|
|
568
|
-
async function selectText(unifiedSession, findText, scopeTarget, nth = 0, timeout) {
|
|
703
|
+
async function selectText(unifiedSession, findText, scopeTarget, nth = 0, timeout, privacy) {
|
|
704
|
+
const deadline = Date.now() + (timeout ?? DEFAULT_TIMEOUT);
|
|
705
|
+
const sensitiveBefore = privacy?.sensitive ?? false;
|
|
706
|
+
if (privacy) {
|
|
707
|
+
privacy.sensitive = true;
|
|
708
|
+
}
|
|
569
709
|
// 将 target 转为查询参数,传入注入脚本进行 DOM 查询
|
|
570
710
|
let scopeSelector = null;
|
|
571
711
|
let scopeText = null;
|
|
@@ -621,12 +761,12 @@ async function selectText(unifiedSession, findText, scopeTarget, nth = 0, timeou
|
|
|
621
761
|
var pos = -1;
|
|
622
762
|
for (var n = 0; n <= nth; n++) {
|
|
623
763
|
pos = val.indexOf(findText, pos + (n > 0 ? 1 : 0));
|
|
624
|
-
if (pos === -1) return {type: 'notfound'};
|
|
764
|
+
if (pos === -1) return {type: 'notfound', inputType: inputType};
|
|
625
765
|
}
|
|
626
766
|
// 原子化:定位到 input 同时完成 focus + setSelectionRange,避免外层 mouseClick 聚焦不可靠
|
|
627
767
|
root.focus();
|
|
628
768
|
root.setSelectionRange(pos, pos + findText.length);
|
|
629
|
-
return {type: 'input', selectionStart: pos, selectionEnd: pos + findText.length};
|
|
769
|
+
return {type: 'input', inputType: inputType, selectionStart: pos, selectionEnd: pos + findText.length};
|
|
630
770
|
}
|
|
631
771
|
|
|
632
772
|
// 在子树中查找 input/textarea
|
|
@@ -654,7 +794,7 @@ async function selectText(unifiedSession, findText, scopeTarget, nth = 0, timeou
|
|
|
654
794
|
if (ip !== -1) {
|
|
655
795
|
inp.focus();
|
|
656
796
|
inp.setSelectionRange(ip, ip + findText.length);
|
|
657
|
-
return {type: 'input', selectionStart: ip, selectionEnd: ip + findText.length};
|
|
797
|
+
return {type: 'input', inputType: childType, selectionStart: ip, selectionEnd: ip + findText.length};
|
|
658
798
|
}
|
|
659
799
|
}
|
|
660
800
|
|
|
@@ -731,7 +871,13 @@ async function selectText(unifiedSession, findText, scopeTarget, nth = 0, timeou
|
|
|
731
871
|
endY: er.y + er.height / 2,
|
|
732
872
|
formatted: formatted || undefined
|
|
733
873
|
};
|
|
734
|
-
}`, undefined,
|
|
874
|
+
}`, undefined, remainingInputTimeout(deadline), [findText, nth, scopeNth, scopeSelector, scopeText, scopeXpath]);
|
|
875
|
+
if (privacy &&
|
|
876
|
+
result &&
|
|
877
|
+
result.type !== 'noscope' &&
|
|
878
|
+
(result.type !== 'notfound' || result.inputType !== undefined)) {
|
|
879
|
+
privacy.sensitive = sensitiveBefore || ('inputType' in result && result.inputType === 'password');
|
|
880
|
+
}
|
|
735
881
|
if (!result || result.type === 'noscope') {
|
|
736
882
|
const diagnostics = await collectTextSelectionDiagnostics(unifiedSession, findText, scopeTarget, nth, scopeNth, scopeSelector, scopeText, scopeXpath, timeout);
|
|
737
883
|
throw new StructuredToolError('TEXT_SCOPE_NOT_FOUND', `未找到目标元素: ${JSON.stringify(scopeTarget)}`, '请检查 target 是否能定位到包含目标文本的元素,或先用 extract(state) 查看可交互元素', diagnostics);
|
|
@@ -769,11 +915,25 @@ async function selectText(unifiedSession, findText, scopeTarget, nth = 0, timeou
|
|
|
769
915
|
coords.endY += frameOffset.y;
|
|
770
916
|
}
|
|
771
917
|
// Step 2: 模拟鼠标选择
|
|
772
|
-
await unifiedSession.mouseMove(coords.startX, coords.startY);
|
|
773
|
-
|
|
774
|
-
await unifiedSession.keyDown('Shift');
|
|
775
|
-
await unifiedSession.mouseMove(coords.endX, coords.endY);
|
|
918
|
+
await unifiedSession.mouseMove(coords.startX, coords.startY, remainingInputTimeout(deadline));
|
|
919
|
+
remainingInputTimeout(deadline);
|
|
776
920
|
await unifiedSession.mouseClick('left');
|
|
921
|
+
remainingInputTimeout(deadline);
|
|
922
|
+
try {
|
|
923
|
+
await unifiedSession.keyDown('Shift');
|
|
924
|
+
await unifiedSession.mouseMove(coords.endX, coords.endY, remainingInputTimeout(deadline));
|
|
925
|
+
remainingInputTimeout(deadline);
|
|
926
|
+
await unifiedSession.mouseClick('left');
|
|
927
|
+
}
|
|
928
|
+
catch (error) {
|
|
929
|
+
try {
|
|
930
|
+
await unifiedSession.keyUp('Shift');
|
|
931
|
+
}
|
|
932
|
+
catch (releaseError) {
|
|
933
|
+
console.error('[MCP] Shift release failed after selection error:', releaseError);
|
|
934
|
+
}
|
|
935
|
+
throw error;
|
|
936
|
+
}
|
|
777
937
|
await unifiedSession.keyUp('Shift');
|
|
778
938
|
return coords.formatted;
|
|
779
939
|
}
|
|
@@ -784,6 +944,9 @@ function requiredEventParam(event, name) {
|
|
|
784
944
|
throw new Error(`events[].${String(name)} 是 ${event.type} 事件的必填参数`);
|
|
785
945
|
}
|
|
786
946
|
function validateEvent(event) {
|
|
947
|
+
if (event.force && (event.type !== 'click' || !event.target || 'x' in event.target)) {
|
|
948
|
+
throw new StructuredToolError('INVALID_ARGUMENT', 'force 仅支持带元素 target 的 click 事件,不能使用坐标或省略 target', '请指定 CSS、role、文本或其他元素定位方式', { actionExecuted: false });
|
|
949
|
+
}
|
|
787
950
|
if (event.commands && event.commands.length > 0 && event.type !== 'keydown') {
|
|
788
951
|
throw new Error(`events[].commands 只能用于 keydown 事件,当前事件类型为 ${event.type},如需触发编辑命令,请把 commands 放在 keydown 事件上`);
|
|
789
952
|
}
|
|
@@ -876,55 +1039,63 @@ const unifiedInputHandlers = {
|
|
|
876
1039
|
drag: handleUnifiedDrag,
|
|
877
1040
|
};
|
|
878
1041
|
async function handleUnifiedKeyDown({ unifiedSession, timeout, frame }, event) {
|
|
879
|
-
|
|
1042
|
+
const deadline = Date.now() + (timeout ?? DEFAULT_TIMEOUT);
|
|
1043
|
+
if (unifiedSession.isExtensionConnected() &&
|
|
1044
|
+
unifiedSession.getInputMode() === 'stealth' &&
|
|
1045
|
+
event.commands &&
|
|
1046
|
+
event.commands.length > 0) {
|
|
880
1047
|
throw new Error('commands 参数不支持 stealth 输入模式,请先调用 manage action=inputMode inputMode=precise 切换后重试');
|
|
881
1048
|
}
|
|
882
1049
|
if (event.commands && event.commands.length > 0 && event.target) {
|
|
883
|
-
await focusTargetForCommands(unifiedSession, event.target,
|
|
1050
|
+
await focusTargetForCommands(unifiedSession, event.target, remainingInputTimeout(deadline), frame);
|
|
884
1051
|
}
|
|
885
|
-
|
|
1052
|
+
remainingInputTimeout(deadline);
|
|
1053
|
+
await unifiedSession.keyDown(event.key, event.commands, remainingInputTimeout(deadline));
|
|
886
1054
|
return undefined;
|
|
887
1055
|
}
|
|
888
|
-
async function handleUnifiedKeyUp({ unifiedSession }, event) {
|
|
889
|
-
await unifiedSession.keyUp(event.key);
|
|
1056
|
+
async function handleUnifiedKeyUp({ unifiedSession, timeout }, event) {
|
|
1057
|
+
await unifiedSession.keyUp(event.key, timeout);
|
|
890
1058
|
return undefined;
|
|
891
1059
|
}
|
|
892
|
-
async function handleUnifiedClick({ unifiedSession, timeout, frame }, event) {
|
|
1060
|
+
async function handleUnifiedClick({ unifiedSession, humanize, timeout, frame }, event) {
|
|
1061
|
+
const deadline = Date.now() + (timeout ?? DEFAULT_TIMEOUT);
|
|
893
1062
|
const button = event.button ?? 'left';
|
|
894
1063
|
const clickCount = event.clickCount ?? 1;
|
|
895
1064
|
if (!event.target) {
|
|
896
1065
|
await unifiedSession.mouseClick(button, clickCount);
|
|
897
1066
|
return undefined;
|
|
898
1067
|
}
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
1068
|
+
const point = await getTargetPointExtension(unifiedSession, event.target, remainingInputTimeout(deadline), frame, !event.force);
|
|
1069
|
+
if (humanize) {
|
|
1070
|
+
for (const p of generateBezierPath(unifiedSession.getMousePosition(), point, event.steps)) {
|
|
1071
|
+
await unifiedSession.mouseMove(p.x, p.y, remainingInputTimeout(deadline));
|
|
1072
|
+
const remaining = remainingInputTimeout(deadline);
|
|
1073
|
+
await randomDelay(Math.min(getMouseMoveDelay(), remaining), Math.min(getMouseMoveDelay() * 2, remaining));
|
|
1074
|
+
}
|
|
904
1075
|
}
|
|
905
|
-
if (button
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
}
|
|
915
|
-
return undefined;
|
|
1076
|
+
else if (!point.refId || button !== 'left' || clickCount !== 1) {
|
|
1077
|
+
await unifiedSession.mouseMove(point.x, point.y, remainingInputTimeout(deadline));
|
|
1078
|
+
}
|
|
1079
|
+
remainingInputTimeout(deadline);
|
|
1080
|
+
if (point.refId) {
|
|
1081
|
+
const result = await unifiedSession.actionableClick(point.refId, event.force === true, button, clickCount, point);
|
|
1082
|
+
if (!result.success) {
|
|
1083
|
+
throw new StructuredToolError('ACTIONABILITY_FAILED', result.error || 'Click failed', result.suggestions?.[0] ??
|
|
1084
|
+
'请根据 context.rect、context.clickPoint 和 context.coveringElement 调整 target', result);
|
|
916
1085
|
}
|
|
917
1086
|
}
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
1087
|
+
else {
|
|
1088
|
+
await unifiedSession.mouseClick(button, clickCount);
|
|
1089
|
+
}
|
|
921
1090
|
return undefined;
|
|
922
1091
|
}
|
|
923
1092
|
async function handleUnifiedMouseDown({ unifiedSession, timeout, frame }, event) {
|
|
1093
|
+
const deadline = Date.now() + (timeout ?? DEFAULT_TIMEOUT);
|
|
924
1094
|
if (event.target) {
|
|
925
|
-
const point = await getTargetPointExtension(unifiedSession, event.target,
|
|
926
|
-
await unifiedSession.mouseMove(point.x, point.y);
|
|
1095
|
+
const point = await getTargetPointExtension(unifiedSession, event.target, remainingInputTimeout(deadline), frame);
|
|
1096
|
+
await unifiedSession.mouseMove(point.x, point.y, remainingInputTimeout(deadline));
|
|
927
1097
|
}
|
|
1098
|
+
remainingInputTimeout(deadline);
|
|
928
1099
|
await unifiedSession.mouseDown(event.button ?? 'left');
|
|
929
1100
|
return undefined;
|
|
930
1101
|
}
|
|
@@ -933,31 +1104,37 @@ async function handleUnifiedMouseUp({ unifiedSession }, event) {
|
|
|
933
1104
|
return undefined;
|
|
934
1105
|
}
|
|
935
1106
|
async function handleUnifiedMouseMove({ unifiedSession, humanize, timeout, frame }, event) {
|
|
936
|
-
const
|
|
1107
|
+
const deadline = Date.now() + (timeout ?? DEFAULT_TIMEOUT);
|
|
1108
|
+
const point = await getTargetPointExtension(unifiedSession, event.target, remainingInputTimeout(deadline), frame);
|
|
937
1109
|
if (humanize && event.steps && event.steps > 1) {
|
|
938
1110
|
const path = generateBezierPath(unifiedSession.getMousePosition(), point, event.steps);
|
|
939
1111
|
for (const p of path) {
|
|
940
|
-
await unifiedSession.mouseMove(p.x, p.y);
|
|
941
|
-
|
|
1112
|
+
await unifiedSession.mouseMove(p.x, p.y, remainingInputTimeout(deadline));
|
|
1113
|
+
const remaining = remainingInputTimeout(deadline);
|
|
1114
|
+
await randomDelay(Math.min(getMouseMoveDelay(), remaining), Math.min(getMouseMoveDelay() * 2, remaining));
|
|
942
1115
|
}
|
|
943
1116
|
}
|
|
944
1117
|
else {
|
|
945
|
-
await unifiedSession.mouseMove(point.x, point.y);
|
|
1118
|
+
await unifiedSession.mouseMove(point.x, point.y, remainingInputTimeout(deadline));
|
|
946
1119
|
}
|
|
1120
|
+
remainingInputTimeout(deadline);
|
|
947
1121
|
return undefined;
|
|
948
1122
|
}
|
|
949
1123
|
async function handleUnifiedWheel({ unifiedSession, timeout, frame }, event) {
|
|
1124
|
+
const deadline = Date.now() + (timeout ?? DEFAULT_TIMEOUT);
|
|
950
1125
|
if (event.target) {
|
|
951
1126
|
const { selector, text, xpath, nth: nthParam } = targetToFindParams(event.target);
|
|
952
|
-
const elements = await unifiedSession.find(selector, text, xpath,
|
|
1127
|
+
const elements = await unifiedSession.find(selector, text, xpath, remainingInputTimeout(deadline));
|
|
953
1128
|
const nth = nthParam ?? 0;
|
|
954
1129
|
if (elements.length > nth) {
|
|
1130
|
+
remainingInputTimeout(deadline);
|
|
955
1131
|
await unifiedSession.scroll(event.deltaX ?? 0, event.deltaY ?? 0, elements[nth].refId);
|
|
956
1132
|
return undefined;
|
|
957
1133
|
}
|
|
958
|
-
const point = await getTargetPointExtension(unifiedSession, event.target,
|
|
959
|
-
await unifiedSession.mouseMove(point.x, point.y);
|
|
1134
|
+
const point = await getTargetPointExtension(unifiedSession, event.target, remainingInputTimeout(deadline), frame);
|
|
1135
|
+
await unifiedSession.mouseMove(point.x, point.y, remainingInputTimeout(deadline));
|
|
960
1136
|
}
|
|
1137
|
+
remainingInputTimeout(deadline);
|
|
961
1138
|
await unifiedSession.mouseWheel(event.deltaX ?? 0, event.deltaY ?? 0);
|
|
962
1139
|
return undefined;
|
|
963
1140
|
}
|
|
@@ -975,33 +1152,38 @@ async function handleUnifiedTouchEnd({ unifiedSession }) {
|
|
|
975
1152
|
await unifiedSession.touchEnd();
|
|
976
1153
|
return undefined;
|
|
977
1154
|
}
|
|
978
|
-
async function handleUnifiedType(
|
|
1155
|
+
async function handleUnifiedType(context, event) {
|
|
1156
|
+
const { unifiedSession, humanize, timeout, frame } = context;
|
|
979
1157
|
if (event.mode === 'controlled' || event.dispatch) {
|
|
980
1158
|
await inputControlled(unifiedSession, event, timeout, frame);
|
|
981
1159
|
return undefined;
|
|
982
1160
|
}
|
|
1161
|
+
const deadline = Date.now() + (timeout ?? DEFAULT_TIMEOUT);
|
|
983
1162
|
if (event.target) {
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
1163
|
+
await handleUnifiedClick({ ...context, timeout: remainingInputTimeout(deadline) }, { type: 'click', target: event.target });
|
|
1164
|
+
if (!('x' in event.target)) {
|
|
1165
|
+
await focusTargetForCommands(unifiedSession, event.target, remainingInputTimeout(deadline), frame);
|
|
1166
|
+
}
|
|
987
1167
|
}
|
|
988
1168
|
else {
|
|
989
1169
|
const hasActiveFocus = await unifiedSession.evaluate('!!document.activeElement && ' +
|
|
990
1170
|
'document.activeElement !== document.body && ' +
|
|
991
|
-
'document.activeElement !== document.documentElement');
|
|
1171
|
+
'document.activeElement !== document.documentElement', undefined, remainingInputTimeout(deadline));
|
|
992
1172
|
if (!hasActiveFocus) {
|
|
993
1173
|
throw new Error('type 事件在无 target 时需要页面已有焦点元素,请提供 target 或先 click 目标元素');
|
|
994
1174
|
}
|
|
995
1175
|
}
|
|
996
1176
|
const delay = event.delay ?? 0;
|
|
997
1177
|
if (humanize) {
|
|
998
|
-
for (const char of event.text) {
|
|
999
|
-
await unifiedSession.typeText(char);
|
|
1000
|
-
|
|
1178
|
+
for (const char of event.text.replace(/\r\n?/g, '\n')) {
|
|
1179
|
+
await unifiedSession.typeText(char, 0, remainingInputTimeout(deadline));
|
|
1180
|
+
const remaining = remainingInputTimeout(deadline);
|
|
1181
|
+
await randomDelay(Math.min(getTypingDelay(delay), remaining), Math.min(getTypingDelay(delay) * 1.5, remaining));
|
|
1001
1182
|
}
|
|
1183
|
+
remainingInputTimeout(deadline);
|
|
1002
1184
|
}
|
|
1003
1185
|
else {
|
|
1004
|
-
await unifiedSession.typeText(event.text, delay);
|
|
1186
|
+
await unifiedSession.typeText(event.text, delay, remainingInputTimeout(deadline));
|
|
1005
1187
|
}
|
|
1006
1188
|
return undefined;
|
|
1007
1189
|
}
|
|
@@ -1015,17 +1197,25 @@ async function handleUnifiedWait({ timeout }, event) {
|
|
|
1015
1197
|
await new Promise((resolve) => setTimeout(resolve, event.ms));
|
|
1016
1198
|
return undefined;
|
|
1017
1199
|
}
|
|
1018
|
-
async function handleUnifiedSelect({ unifiedSession, session, mode, timeout, frame }, event) {
|
|
1019
|
-
|
|
1200
|
+
async function handleUnifiedSelect({ unifiedSession, session, mode, timeout, frame, privacy }, event) {
|
|
1201
|
+
const deadline = Date.now() + (timeout ?? DEFAULT_TIMEOUT);
|
|
1202
|
+
const sensitiveBefore = privacy?.sensitive ?? false;
|
|
1203
|
+
if (privacy) {
|
|
1204
|
+
privacy.sensitive = true;
|
|
1205
|
+
}
|
|
1206
|
+
await focusTargetIfNeeded(unifiedSession, session, mode, event.target, remainingInputTimeout(deadline), frame);
|
|
1020
1207
|
const selectionTarget = await unifiedSession.evaluate(`(() => {
|
|
1021
1208
|
const el = document.activeElement;
|
|
1022
1209
|
if (!(el instanceof HTMLInputElement)) return null;
|
|
1023
1210
|
return {
|
|
1024
1211
|
tag: 'input',
|
|
1025
1212
|
inputType: (el.type || 'text').toLowerCase(),
|
|
1026
|
-
value: String(el.value || '').slice(0, 160)
|
|
1213
|
+
value: el.type === 'password' ? undefined : String(el.value || '').slice(0, 160)
|
|
1027
1214
|
};
|
|
1028
|
-
})()`, undefined,
|
|
1215
|
+
})()`, undefined, remainingInputTimeout(deadline));
|
|
1216
|
+
if (privacy) {
|
|
1217
|
+
privacy.sensitive = sensitiveBefore || selectionTarget?.inputType === 'password';
|
|
1218
|
+
}
|
|
1029
1219
|
if (selectionTarget && !supportsTextSelection(selectionTarget.tag, selectionTarget.inputType)) {
|
|
1030
1220
|
throw new StructuredToolError('UNSUPPORTED_SELECTION', `input[type=${selectionTarget.inputType}] 不支持文本 selection`, '请改用 type mode="controlled" 设置完整 value,replace 事件会对这类 input 使用完整 value 更新路径', {
|
|
1031
1221
|
elementTag: selectionTarget.tag,
|
|
@@ -1035,16 +1225,21 @@ async function handleUnifiedSelect({ unifiedSession, session, mode, timeout, fra
|
|
|
1035
1225
|
recommendedMode: 'controlled',
|
|
1036
1226
|
});
|
|
1037
1227
|
}
|
|
1038
|
-
await selectText(unifiedSession, event.find, event.target, event.nth,
|
|
1228
|
+
await selectText(unifiedSession, event.find, event.target, event.nth, remainingInputTimeout(deadline), privacy);
|
|
1039
1229
|
return undefined;
|
|
1040
1230
|
}
|
|
1041
|
-
async function handleUnifiedReplace({ unifiedSession, session, mode, timeout, frame }, event) {
|
|
1042
|
-
|
|
1231
|
+
async function handleUnifiedReplace({ unifiedSession, session, mode, timeout, frame, privacy }, event) {
|
|
1232
|
+
const deadline = Date.now() + (timeout ?? DEFAULT_TIMEOUT);
|
|
1233
|
+
const sensitiveBefore = privacy?.sensitive ?? false;
|
|
1234
|
+
if (privacy) {
|
|
1235
|
+
privacy.sensitive = true;
|
|
1236
|
+
}
|
|
1237
|
+
await focusTargetIfNeeded(unifiedSession, session, mode, event.target, remainingInputTimeout(deadline), frame);
|
|
1043
1238
|
const directResult = await unifiedSession.evaluate(`function(findText, replacementText, nth) {
|
|
1044
1239
|
var el = document.activeElement;
|
|
1045
1240
|
if (!(el instanceof HTMLInputElement)) return {handled: false};
|
|
1046
1241
|
var type = (el.type || 'text').toLowerCase();
|
|
1047
|
-
if (['text', 'search', 'tel', 'url', 'password'].indexOf(type) !== -1) return {handled: false};
|
|
1242
|
+
if (['text', 'search', 'tel', 'url', 'password'].indexOf(type) !== -1) return {handled: false, inputType: type};
|
|
1048
1243
|
var current = String(el.value || '');
|
|
1049
1244
|
var pos = -1;
|
|
1050
1245
|
for (var i = 0; i <= nth; i++) {
|
|
@@ -1097,7 +1292,10 @@ async function handleUnifiedReplace({ unifiedSession, session, mode, timeout, fr
|
|
|
1097
1292
|
actualValue: actual.slice(0, 160),
|
|
1098
1293
|
reason: actual === requested ? undefined : 'value_not_applied'
|
|
1099
1294
|
};
|
|
1100
|
-
}`, undefined,
|
|
1295
|
+
}`, undefined, remainingInputTimeout(deadline), [event.find, event.text, event.nth ?? 0]);
|
|
1296
|
+
if (privacy) {
|
|
1297
|
+
privacy.sensitive = sensitiveBefore || directResult.inputType === 'password';
|
|
1298
|
+
}
|
|
1101
1299
|
if (directResult.handled) {
|
|
1102
1300
|
if (!directResult.success) {
|
|
1103
1301
|
const valueNotApplied = directResult.reason === 'value_not_applied';
|
|
@@ -1130,7 +1328,7 @@ async function handleUnifiedReplace({ unifiedSession, session, mode, timeout, fr
|
|
|
1130
1328
|
normalized: directResult.actualValue !== directResult.requestedValue,
|
|
1131
1329
|
};
|
|
1132
1330
|
}
|
|
1133
|
-
const formatted = await selectText(unifiedSession, event.find, event.target, event.nth,
|
|
1331
|
+
const formatted = await selectText(unifiedSession, event.find, event.target, event.nth, remainingInputTimeout(deadline), privacy);
|
|
1134
1332
|
let selectionConfirmed = false;
|
|
1135
1333
|
for (let i = 0; i < 25; i++) {
|
|
1136
1334
|
const hasSelection = await unifiedSession.evaluate(`(function() {
|
|
@@ -1140,12 +1338,12 @@ async function handleUnifiedReplace({ unifiedSession, session, mode, timeout, fr
|
|
|
1140
1338
|
}
|
|
1141
1339
|
var sel = window.getSelection();
|
|
1142
1340
|
return sel && !sel.isCollapsed;
|
|
1143
|
-
})()
|
|
1341
|
+
})()`, undefined, remainingInputTimeout(deadline));
|
|
1144
1342
|
if (hasSelection) {
|
|
1145
1343
|
selectionConfirmed = true;
|
|
1146
1344
|
break;
|
|
1147
1345
|
}
|
|
1148
|
-
await new Promise((resolve) => setTimeout(resolve, 20));
|
|
1346
|
+
await new Promise((resolve) => setTimeout(resolve, Math.min(20, remainingInputTimeout(deadline))));
|
|
1149
1347
|
}
|
|
1150
1348
|
if (!selectionConfirmed) {
|
|
1151
1349
|
throw new Error(`选区同步失败:文本 "${event.find}" 已定位但未能建立选区,无法执行替换`);
|
|
@@ -1168,12 +1366,12 @@ async function handleUnifiedReplace({ unifiedSession, session, mode, timeout, fr
|
|
|
1168
1366
|
if (!editable || !editable.isContentEditable) return 'readonly';
|
|
1169
1367
|
if (document.execCommand('insertText', false, replacementText)) return 'ok';
|
|
1170
1368
|
return 'fallback';
|
|
1171
|
-
}`, undefined,
|
|
1369
|
+
}`, undefined, remainingInputTimeout(deadline), [event.text]);
|
|
1172
1370
|
if (replaceResult === 'readonly') {
|
|
1173
1371
|
throw new Error(`目标元素不可编辑,已选中文本 "${event.find}" 但无法替换`);
|
|
1174
1372
|
}
|
|
1175
1373
|
if (replaceResult === 'fallback') {
|
|
1176
|
-
await unifiedSession.typeText(event.text);
|
|
1374
|
+
await unifiedSession.typeText(event.text, 0, remainingInputTimeout(deadline));
|
|
1177
1375
|
}
|
|
1178
1376
|
if (formatted) {
|
|
1179
1377
|
return `替换的文本原在 <${formatted}> 标签内,替换后格式可能丢失`;
|
|
@@ -1270,8 +1468,9 @@ async function buildTargetDiagnosticContext(unifiedSession, target, matchCount,
|
|
|
1270
1468
|
});
|
|
1271
1469
|
}
|
|
1272
1470
|
async function editorAction(unifiedSession, action, event, timeout, frame) {
|
|
1471
|
+
const deadline = Date.now() + (timeout ?? DEFAULT_TIMEOUT);
|
|
1273
1472
|
if (event.target) {
|
|
1274
|
-
await focusTargetForCommands(unifiedSession, event.target,
|
|
1473
|
+
await focusTargetForCommands(unifiedSession, event.target, remainingInputTimeout(deadline), frame);
|
|
1275
1474
|
}
|
|
1276
1475
|
return unifiedSession.evaluate(`function(action, text, command) {
|
|
1277
1476
|
var active = document.activeElement;
|
|
@@ -1334,9 +1533,18 @@ async function editorAction(unifiedSession, action, event, timeout, frame) {
|
|
|
1334
1533
|
throw new Error('editorCommand 执行失败: ' + command);
|
|
1335
1534
|
}
|
|
1336
1535
|
return {success: true, command: command};
|
|
1337
|
-
}`, undefined,
|
|
1536
|
+
}`, undefined, remainingInputTimeout(deadline), [action, event.text ?? '', event.command ?? '']);
|
|
1338
1537
|
}
|
|
1339
|
-
async function getTargetPointExtension(unifiedSession, target, timeout, frame) {
|
|
1538
|
+
async function getTargetPointExtension(unifiedSession, target, timeout, frame, scroll = true) {
|
|
1539
|
+
const budget = timeout ?? DEFAULT_TIMEOUT;
|
|
1540
|
+
const deadline = Date.now() + budget;
|
|
1541
|
+
const remaining = () => {
|
|
1542
|
+
const value = deadline - Date.now();
|
|
1543
|
+
if (value <= 0) {
|
|
1544
|
+
throwInputTimeoutError(budget);
|
|
1545
|
+
}
|
|
1546
|
+
return value;
|
|
1547
|
+
};
|
|
1340
1548
|
const frameOffset = unifiedSession.getFrameOffset();
|
|
1341
1549
|
const isStealth = unifiedSession.getInputMode() === 'stealth';
|
|
1342
1550
|
// 原始坐标:用户意图为 iframe 相对坐标
|
|
@@ -1348,7 +1556,7 @@ async function getTargetPointExtension(unifiedSession, target, timeout, frame) {
|
|
|
1348
1556
|
return { x: target.x, y: target.y };
|
|
1349
1557
|
}
|
|
1350
1558
|
const { selector, text, xpath, nth: nthParam } = targetToFindParams(target);
|
|
1351
|
-
const elements = await unifiedSession.find(selector, text, xpath,
|
|
1559
|
+
const elements = await unifiedSession.find(selector, text, xpath, remaining());
|
|
1352
1560
|
const nth = nthParam ?? 0;
|
|
1353
1561
|
if (elements.length === 0) {
|
|
1354
1562
|
throw new StructuredToolError('TARGET_NOT_FOUND', `未找到目标元素: ${JSON.stringify(target)}`, '请检查 target 是否正确,或先用 extract type=state 查看 interactiveElements 候选', await buildTargetDiagnosticContext(unifiedSession, target, elements.length, nth, timeout, '目标元素未找到', frame));
|
|
@@ -1364,20 +1572,15 @@ async function getTargetPointExtension(unifiedSession, target, timeout, frame) {
|
|
|
1364
1572
|
const el = ref?.deref();
|
|
1365
1573
|
if (!el) return null;
|
|
1366
1574
|
const r = el.getBoundingClientRect();
|
|
1367
|
-
if (r.top < 0 || r.bottom > window.innerHeight || r.left < 0 || r.right > window.innerWidth) {
|
|
1575
|
+
if (${scroll} && (r.top < 0 || r.bottom > window.innerHeight || r.left < 0 || r.right > window.innerWidth)) {
|
|
1368
1576
|
el.scrollIntoView({ block: 'center', inline: 'center', behavior: 'instant' });
|
|
1369
1577
|
}
|
|
1370
1578
|
const r2 = el.getBoundingClientRect();
|
|
1371
1579
|
return { x: r2.x, y: r2.y, width: r2.width, height: r2.height };
|
|
1372
|
-
})()
|
|
1373
|
-
|
|
1580
|
+
})()`, undefined, remaining());
|
|
1581
|
+
remaining();
|
|
1374
1582
|
if (!iframeRelRect) {
|
|
1375
|
-
|
|
1376
|
-
const point = { x: rect.x + rect.width / 2, y: rect.y + rect.height / 2 };
|
|
1377
|
-
if (frameOffset && isStealth) {
|
|
1378
|
-
return { x: point.x - frameOffset.x, y: point.y - frameOffset.y, refId };
|
|
1379
|
-
}
|
|
1380
|
-
return { ...point, refId };
|
|
1583
|
+
throw new StructuredToolError('TARGET_NOT_FOUND', '目标元素引用已失效,未使用旧坐标派发事件', '请重新读取页面并确认 target', { target, refId, actionExecuted: false });
|
|
1381
1584
|
}
|
|
1382
1585
|
// 主 frame:iframeRelRect 就是父视口绝对
|
|
1383
1586
|
if (!frameOffset) {
|
|
@@ -1398,7 +1601,8 @@ async function getTargetPointExtension(unifiedSession, target, timeout, frame) {
|
|
|
1398
1601
|
// iframe + precise:消费者(chrome.debugger)需要父视口绝对,
|
|
1399
1602
|
// scrollIntoView({block:'center'}) 会 cascade 到父框架,导致 frameOffset 与父绝对 rect 都过期,
|
|
1400
1603
|
// refetch find() 让 content-handler 重新计算 frameOffset 并返回最新父绝对 rect
|
|
1401
|
-
const refreshed = await unifiedSession.find(selector, text, xpath,
|
|
1604
|
+
const refreshed = await unifiedSession.find(selector, text, xpath, remaining());
|
|
1605
|
+
remaining();
|
|
1402
1606
|
const rect = refreshed[nth]?.rect ?? elements[nth].rect;
|
|
1403
1607
|
return { x: rect.x + rect.width / 2, y: rect.y + rect.height / 2, refId };
|
|
1404
1608
|
}
|
|
@@ -1411,8 +1615,6 @@ async function executeCdpEvent(session, event, humanize, timeout) {
|
|
|
1411
1615
|
await handler({ session, humanize, timeout }, event);
|
|
1412
1616
|
}
|
|
1413
1617
|
const cdpInputHandlers = {
|
|
1414
|
-
keydown: handleCdpKeyDown,
|
|
1415
|
-
keyup: handleCdpKeyUp,
|
|
1416
1618
|
click: handleCdpClick,
|
|
1417
1619
|
mousedown: handleCdpMouseDown,
|
|
1418
1620
|
mouseup: handleCdpMouseUp,
|
|
@@ -1425,13 +1627,33 @@ const cdpInputHandlers = {
|
|
|
1425
1627
|
wait: handleCdpWait,
|
|
1426
1628
|
drag: handleCdpDrag,
|
|
1427
1629
|
};
|
|
1428
|
-
async function handleCdpKeyDown({ session }, event) {
|
|
1429
|
-
await session.keyDown(event.key, event.commands);
|
|
1430
|
-
}
|
|
1431
|
-
async function handleCdpKeyUp({ session }, event) {
|
|
1432
|
-
await session.keyUp(event.key);
|
|
1433
|
-
}
|
|
1434
1630
|
async function handleCdpClick({ session, humanize, timeout }, event) {
|
|
1631
|
+
if (event.force) {
|
|
1632
|
+
const locator = session.createLocator(event.target, {
|
|
1633
|
+
timeout: targetLookupTimeout(timeout ?? DEFAULT_TIMEOUT),
|
|
1634
|
+
});
|
|
1635
|
+
await locator.evaluateOn(`function() {
|
|
1636
|
+
if (!this.isConnected) throw new Error('目标元素已移除,未执行点击');
|
|
1637
|
+
const button = ${JSON.stringify(event.button ?? 'left')};
|
|
1638
|
+
const count = ${event.clickCount ?? 1};
|
|
1639
|
+
const codes = {left: 0, middle: 1, right: 2, back: 3, forward: 4};
|
|
1640
|
+
const masks = {left: 1, middle: 4, right: 2, back: 8, forward: 16};
|
|
1641
|
+
const rect = this.getBoundingClientRect();
|
|
1642
|
+
const base = {bubbles: true, cancelable: true, composed: true, view: window,
|
|
1643
|
+
button: codes[button], clientX: rect.x + rect.width / 2, clientY: rect.y + rect.height / 2};
|
|
1644
|
+
for (let detail = 1; detail <= count; ++detail) {
|
|
1645
|
+
const down = this.dispatchEvent(new MouseEvent('mousedown', {...base, detail, buttons: masks[button]}));
|
|
1646
|
+
if (down && button === 'left' && typeof this.focus === 'function') this.focus();
|
|
1647
|
+
this.dispatchEvent(new MouseEvent('mouseup', {...base, detail, buttons: 0}));
|
|
1648
|
+
const type = button === 'left' ? 'click' : button === 'right' ? 'contextmenu' : 'auxclick';
|
|
1649
|
+
this.dispatchEvent(new MouseEvent(type, {...base, detail, buttons: 0}));
|
|
1650
|
+
if (button === 'left' && detail === 2) {
|
|
1651
|
+
this.dispatchEvent(new MouseEvent('dblclick', {...base, detail, buttons: 0}));
|
|
1652
|
+
}
|
|
1653
|
+
}
|
|
1654
|
+
}`);
|
|
1655
|
+
return;
|
|
1656
|
+
}
|
|
1435
1657
|
if (event.target) {
|
|
1436
1658
|
await moveToTarget(session, event.target, humanize, timeout);
|
|
1437
1659
|
}
|
|
@@ -1465,18 +1687,23 @@ async function handleCdpTouchStart({ session, timeout }, event) {
|
|
|
1465
1687
|
await session.touchStart(point.x, point.y);
|
|
1466
1688
|
}
|
|
1467
1689
|
async function handleCdpTouchMove({ session, humanize, timeout }, event) {
|
|
1468
|
-
const
|
|
1690
|
+
const deadline = Date.now() + (timeout ?? DEFAULT_TIMEOUT);
|
|
1691
|
+
const point = await getTargetPoint(session, event.target, remainingInputTimeout(deadline));
|
|
1469
1692
|
if (humanize && event.steps && event.steps > 1) {
|
|
1470
1693
|
const current = session.getBehaviorSimulator().getCurrentPosition();
|
|
1471
1694
|
const path = generateBezierPath(current, point, event.steps);
|
|
1472
1695
|
for (const p of path) {
|
|
1696
|
+
remainingInputTimeout(deadline);
|
|
1473
1697
|
await session.touchMove(p.x, p.y);
|
|
1474
|
-
|
|
1698
|
+
const remaining = remainingInputTimeout(deadline);
|
|
1699
|
+
await randomDelay(Math.min(5, remaining), Math.min(15, remaining));
|
|
1475
1700
|
}
|
|
1476
1701
|
}
|
|
1477
1702
|
else {
|
|
1703
|
+
remainingInputTimeout(deadline);
|
|
1478
1704
|
await session.touchMove(point.x, point.y);
|
|
1479
1705
|
}
|
|
1706
|
+
remainingInputTimeout(deadline);
|
|
1480
1707
|
}
|
|
1481
1708
|
async function handleCdpTouchEnd({ session }) {
|
|
1482
1709
|
await session.touchEnd();
|
|
@@ -1485,28 +1712,34 @@ async function handleCdpType({ session, humanize, timeout }, event) {
|
|
|
1485
1712
|
if (event.dispatch || event.mode === 'controlled') {
|
|
1486
1713
|
throw new Error('controlled/dispatch 输入需要 Extension 连接,当前为 CDP 模式');
|
|
1487
1714
|
}
|
|
1715
|
+
const deadline = Date.now() + (timeout ?? DEFAULT_TIMEOUT);
|
|
1488
1716
|
if (event.target) {
|
|
1489
|
-
await moveToTarget(session, event.target, humanize,
|
|
1717
|
+
await moveToTarget(session, event.target, humanize, remainingInputTimeout(deadline), undefined, 'input');
|
|
1490
1718
|
await session.mouseDown('left');
|
|
1491
1719
|
await session.mouseUp('left');
|
|
1720
|
+
if (!('x' in event.target)) {
|
|
1721
|
+
await focusCdpTarget(session, event.target, remainingInputTimeout(deadline));
|
|
1722
|
+
}
|
|
1492
1723
|
}
|
|
1493
1724
|
else {
|
|
1494
1725
|
const hasActiveFocus = await session.evaluate('!!document.activeElement && ' +
|
|
1495
1726
|
'document.activeElement !== document.body && ' +
|
|
1496
|
-
'document.activeElement !== document.documentElement');
|
|
1727
|
+
'document.activeElement !== document.documentElement', undefined, remainingInputTimeout(deadline));
|
|
1497
1728
|
if (!hasActiveFocus) {
|
|
1498
1729
|
throw new Error('type 事件在无 target 时需要页面已有焦点元素,请提供 target 或先 click 目标元素');
|
|
1499
1730
|
}
|
|
1500
1731
|
}
|
|
1501
1732
|
const delay = event.delay ?? 0;
|
|
1502
1733
|
if (humanize) {
|
|
1503
|
-
for (const char of event.text) {
|
|
1504
|
-
await session.type(char);
|
|
1505
|
-
|
|
1734
|
+
for (const char of event.text.replace(/\r\n?/g, '\n')) {
|
|
1735
|
+
await session.type(char, 0, remainingInputTimeout(deadline));
|
|
1736
|
+
const remaining = remainingInputTimeout(deadline);
|
|
1737
|
+
await randomDelay(Math.min(getTypingDelay(delay), remaining), Math.min(getTypingDelay(delay) * 1.5, remaining));
|
|
1506
1738
|
}
|
|
1739
|
+
remainingInputTimeout(deadline);
|
|
1507
1740
|
}
|
|
1508
1741
|
else {
|
|
1509
|
-
await session.type(event.text, delay);
|
|
1742
|
+
await session.type(event.text, delay, remainingInputTimeout(deadline));
|
|
1510
1743
|
}
|
|
1511
1744
|
}
|
|
1512
1745
|
async function handleCdpWait({ timeout }, event) {
|
|
@@ -1520,14 +1753,16 @@ async function handleCdpDrag() {
|
|
|
1520
1753
|
* 移动到目标位置
|
|
1521
1754
|
*/
|
|
1522
1755
|
async function moveToTarget(session, target, humanize, timeout, steps, waitType = 'click') {
|
|
1523
|
-
const
|
|
1756
|
+
const deadline = Date.now() + (timeout ?? DEFAULT_TIMEOUT);
|
|
1757
|
+
const point = await getTargetPoint(session, target, remainingInputTimeout(deadline), waitType);
|
|
1524
1758
|
const simulator = session.getBehaviorSimulator();
|
|
1525
1759
|
if (humanize) {
|
|
1526
1760
|
// 人类化鼠标移动(贝塞尔曲线)
|
|
1527
1761
|
const path = generateBezierPath(simulator.getCurrentPosition(), point, steps);
|
|
1528
1762
|
for (const p of path) {
|
|
1529
|
-
await session.mouseMove(p.x, p.y);
|
|
1530
|
-
|
|
1763
|
+
await session.mouseMove(p.x, p.y, remainingInputTimeout(deadline));
|
|
1764
|
+
const remaining = remainingInputTimeout(deadline);
|
|
1765
|
+
await randomDelay(Math.min(getMouseMoveDelay(), remaining), Math.min(getMouseMoveDelay() * 2, remaining));
|
|
1531
1766
|
}
|
|
1532
1767
|
}
|
|
1533
1768
|
else if (steps && steps > 1) {
|
|
@@ -1537,13 +1772,14 @@ async function moveToTarget(session, target, humanize, timeout, steps, waitType
|
|
|
1537
1772
|
const t = i / steps;
|
|
1538
1773
|
const x = current.x + (point.x - current.x) * t;
|
|
1539
1774
|
const y = current.y + (point.y - current.y) * t;
|
|
1540
|
-
await session.mouseMove(x, y);
|
|
1775
|
+
await session.mouseMove(x, y, remainingInputTimeout(deadline));
|
|
1541
1776
|
}
|
|
1542
1777
|
}
|
|
1543
1778
|
else {
|
|
1544
1779
|
// 直接移动
|
|
1545
|
-
await session.mouseMove(point.x, point.y);
|
|
1780
|
+
await session.mouseMove(point.x, point.y, remainingInputTimeout(deadline));
|
|
1546
1781
|
}
|
|
1782
|
+
remainingInputTimeout(deadline);
|
|
1547
1783
|
}
|
|
1548
1784
|
/**
|
|
1549
1785
|
* 获取目标点坐标(带自动等待)
|
|
@@ -1553,12 +1789,21 @@ async function getTargetPoint(session, target, timeout, waitType = 'click') {
|
|
|
1553
1789
|
if ('x' in target && 'y' in target) {
|
|
1554
1790
|
return { x: target.x, y: target.y };
|
|
1555
1791
|
}
|
|
1792
|
+
const budget = timeout ?? DEFAULT_TIMEOUT;
|
|
1793
|
+
const deadline = Date.now() + budget;
|
|
1794
|
+
const remaining = () => {
|
|
1795
|
+
const value = deadline - Date.now();
|
|
1796
|
+
if (value <= 0) {
|
|
1797
|
+
throwInputTimeoutError(budget);
|
|
1798
|
+
}
|
|
1799
|
+
return value;
|
|
1800
|
+
};
|
|
1556
1801
|
// 使用 Locator 定位元素
|
|
1557
|
-
const locator = session.createLocator(target, { timeout });
|
|
1802
|
+
const locator = session.createLocator(target, { timeout: targetLookupTimeout(remaining()) });
|
|
1558
1803
|
const nodeId = await locator.find();
|
|
1559
1804
|
// 根据操作类型执行自动等待
|
|
1560
1805
|
if (waitType !== 'none') {
|
|
1561
|
-
const autoWait = session.createAutoWait({ timeout });
|
|
1806
|
+
const autoWait = session.createAutoWait({ timeout: remaining() });
|
|
1562
1807
|
if (waitType === 'click') {
|
|
1563
1808
|
await autoWait.waitForClickable(nodeId);
|
|
1564
1809
|
}
|
|
@@ -1566,7 +1811,10 @@ async function getTargetPoint(session, target, timeout, waitType = 'click') {
|
|
|
1566
1811
|
await autoWait.waitForInputReady(nodeId);
|
|
1567
1812
|
}
|
|
1568
1813
|
}
|
|
1569
|
-
|
|
1814
|
+
remaining();
|
|
1815
|
+
const point = await locator.getClickablePoint();
|
|
1816
|
+
remaining();
|
|
1817
|
+
return point;
|
|
1570
1818
|
}
|
|
1571
1819
|
/**
|
|
1572
1820
|
* 注册 input 工具
|