@pyrokine/mcp-chrome 2.4.0 → 2.4.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +220 -149
- package/README_zh.md +245 -179
- package/dist/anti-detection/behavior.js +1 -1
- package/dist/anti-detection/behavior.js.map +1 -1
- package/dist/anti-detection/injection.d.ts.map +1 -1
- package/dist/anti-detection/injection.js +2 -1
- package/dist/anti-detection/injection.js.map +1 -1
- package/dist/cdp/client.d.ts +2 -2
- package/dist/cdp/client.d.ts.map +1 -1
- package/dist/cdp/client.js +7 -4
- package/dist/cdp/client.js.map +1 -1
- package/dist/core/browser-driver.d.ts +9 -3
- package/dist/core/browser-driver.d.ts.map +1 -1
- package/dist/core/browser-driver.js +1 -0
- package/dist/core/browser-driver.js.map +1 -1
- package/dist/core/errors.d.ts +9 -0
- package/dist/core/errors.d.ts.map +1 -1
- package/dist/core/errors.js +16 -1
- package/dist/core/errors.js.map +1 -1
- package/dist/core/os-mouse.d.ts +65 -0
- package/dist/core/os-mouse.d.ts.map +1 -0
- package/dist/core/os-mouse.js +334 -0
- package/dist/core/os-mouse.js.map +1 -0
- package/dist/core/session.d.ts +16 -6
- package/dist/core/session.d.ts.map +1 -1
- package/dist/core/session.js +160 -55
- package/dist/core/session.js.map +1 -1
- package/dist/core/types.d.ts +10 -0
- package/dist/core/types.d.ts.map +1 -1
- package/dist/core/types.js +2 -0
- package/dist/core/types.js.map +1 -1
- package/dist/core/unified-session.d.ts +34 -5
- package/dist/core/unified-session.d.ts.map +1 -1
- package/dist/core/unified-session.js +172 -30
- package/dist/core/unified-session.js.map +1 -1
- package/dist/extension/bridge.d.ts +24 -2
- package/dist/extension/bridge.d.ts.map +1 -1
- package/dist/extension/bridge.js +32 -1
- package/dist/extension/bridge.js.map +1 -1
- package/dist/extension/http-server.d.ts +17 -0
- package/dist/extension/http-server.d.ts.map +1 -1
- package/dist/extension/http-server.js +69 -9
- package/dist/extension/http-server.js.map +1 -1
- package/dist/extension/index.d.ts +1 -0
- package/dist/extension/index.d.ts.map +1 -1
- package/dist/extension/index.js.map +1 -1
- package/dist/tools/browse.d.ts +1 -1
- package/dist/tools/browse.d.ts.map +1 -1
- package/dist/tools/browse.js +73 -26
- package/dist/tools/browse.js.map +1 -1
- package/dist/tools/challenge.d.ts +153 -0
- package/dist/tools/challenge.d.ts.map +1 -0
- package/dist/tools/challenge.js +573 -0
- package/dist/tools/challenge.js.map +1 -0
- package/dist/tools/cookies.d.ts.map +1 -1
- package/dist/tools/cookies.js +8 -4
- package/dist/tools/cookies.js.map +1 -1
- package/dist/tools/diagnostics.d.ts +2 -2
- package/dist/tools/diagnostics.d.ts.map +1 -1
- package/dist/tools/diagnostics.js +28 -9
- package/dist/tools/diagnostics.js.map +1 -1
- package/dist/tools/evaluate.d.ts +8 -0
- package/dist/tools/evaluate.d.ts.map +1 -1
- package/dist/tools/evaluate.js +92 -14
- package/dist/tools/evaluate.js.map +1 -1
- package/dist/tools/extract.d.ts.map +1 -1
- package/dist/tools/extract.js +119 -59
- package/dist/tools/extract.js.map +1 -1
- package/dist/tools/input.d.ts +4 -0
- package/dist/tools/input.d.ts.map +1 -1
- package/dist/tools/input.js +248 -94
- package/dist/tools/input.js.map +1 -1
- package/dist/tools/logs.d.ts +7 -4
- package/dist/tools/logs.d.ts.map +1 -1
- package/dist/tools/logs.js +11 -8
- package/dist/tools/logs.js.map +1 -1
- package/dist/tools/manage.d.ts.map +1 -1
- package/dist/tools/manage.js +20 -7
- package/dist/tools/manage.js.map +1 -1
- package/dist/tools/network-sanitizer.d.ts +15 -0
- package/dist/tools/network-sanitizer.d.ts.map +1 -0
- package/dist/tools/network-sanitizer.js +62 -0
- package/dist/tools/network-sanitizer.js.map +1 -0
- package/dist/tools/png.js +5 -3
- package/dist/tools/png.js.map +1 -1
- package/dist/tools/post-condition.d.ts.map +1 -1
- package/dist/tools/post-condition.js +17 -4
- package/dist/tools/post-condition.js.map +1 -1
- package/dist/tools/target-diagnostics.d.ts +7 -0
- package/dist/tools/target-diagnostics.d.ts.map +1 -1
- package/dist/tools/target-diagnostics.js +25 -2
- package/dist/tools/target-diagnostics.js.map +1 -1
- package/dist/tools/wait.d.ts.map +1 -1
- package/dist/tools/wait.js +42 -40
- package/dist/tools/wait.js.map +1 -1
- package/extension/dist/assets/content.ts-Cj_iiU2s.js +25 -0
- package/extension/dist/assets/{index.ts-BMmrcSQ6.js → index.ts-Bqcu2qiN.js} +620 -290
- package/extension/dist/manifest.json +3 -3
- package/extension/dist/service-worker-loader.js +1 -1
- package/extension/dist/src/popup/popup.html +44 -44
- package/package.json +8 -7
- package/dist/extension/native-host-installer.d.ts +0 -21
- package/dist/extension/native-host-installer.d.ts.map +0 -1
- package/dist/extension/native-host-installer.js +0 -147
- package/dist/extension/native-host-installer.js.map +0 -1
- package/dist/extension/socket-server.d.ts +0 -32
- package/dist/extension/socket-server.d.ts.map +0 -1
- package/dist/extension/socket-server.js +0 -177
- package/dist/extension/socket-server.js.map +0 -1
- package/dist/extension/ws-server.d.ts +0 -40
- package/dist/extension/ws-server.d.ts.map +0 -1
- package/dist/extension/ws-server.js +0 -246
- package/dist/extension/ws-server.js.map +0 -1
- package/dist/native-host/index.js +0 -280
- package/dist/native-host/mcp-chrome-host +0 -2
- package/extension/dist/assets/content.ts-pEASbjEL.js +0 -6
package/dist/tools/input.js
CHANGED
|
@@ -11,25 +11,44 @@
|
|
|
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 { ChallengeDeniedError, ChallengeTimeoutError, resolveChallenge } from './challenge.js';
|
|
14
15
|
import { appendDiagnostics, finishDiagnostics, startDiagnostics } from './diagnostics.js';
|
|
15
16
|
import { postConditionSchema, waitForPostCondition } from './post-condition.js';
|
|
16
17
|
import { targetToFindParams, targetZodSchema } from './schema.js';
|
|
17
18
|
import { buildTargetDiagnostics } from './target-diagnostics.js';
|
|
18
19
|
const INPUT_WAIT_MAX_MS = 60_000;
|
|
19
20
|
const TEXT_SELECTION_INPUT_TYPES = new Set(['text', 'search', 'tel', 'url', 'password']);
|
|
21
|
+
function throwInputTimeoutError(timeout) {
|
|
22
|
+
throw new Error(`input timeout before event dispatch (${timeout}ms)`);
|
|
23
|
+
}
|
|
20
24
|
export function supportsTextSelection(tag, inputType) {
|
|
21
25
|
return (tag.toLowerCase() === 'textarea' ||
|
|
22
26
|
(tag.toLowerCase() === 'input' && TEXT_SELECTION_INPUT_TYPES.has((inputType ?? 'text').toLowerCase())));
|
|
23
27
|
}
|
|
28
|
+
// noinspection JSUnusedGlobalSymbols — 回归测试从构建产物导入该 helper
|
|
24
29
|
export function replaceNthOccurrence(value, find, replacement, nth = 0) {
|
|
25
30
|
let index = -1;
|
|
26
31
|
for (let occurrence = 0; occurrence <= nth; occurrence++) {
|
|
27
32
|
index = value.indexOf(find, index + (occurrence > 0 ? 1 : 0));
|
|
28
|
-
if (index === -1)
|
|
33
|
+
if (index === -1) {
|
|
29
34
|
return null;
|
|
35
|
+
}
|
|
30
36
|
}
|
|
31
37
|
return value.slice(0, index) + replacement + value.slice(index + find.length);
|
|
32
38
|
}
|
|
39
|
+
export function redactSensitiveInputData(value, sensitiveValues) {
|
|
40
|
+
const secrets = [...new Set(sensitiveValues.filter((item) => item.length > 0))].sort((left, right) => right.length - left.length);
|
|
41
|
+
if (typeof value === 'string') {
|
|
42
|
+
return secrets.reduce((text, secret) => text.replaceAll(secret, '[REDACTED]'), value);
|
|
43
|
+
}
|
|
44
|
+
if (Array.isArray(value)) {
|
|
45
|
+
return value.map((item) => redactSensitiveInputData(item, secrets));
|
|
46
|
+
}
|
|
47
|
+
if (value && typeof value === 'object') {
|
|
48
|
+
return Object.fromEntries(Object.entries(value).map(([key, item]) => [key, redactSensitiveInputData(item, secrets)]));
|
|
49
|
+
}
|
|
50
|
+
return value;
|
|
51
|
+
}
|
|
33
52
|
/**
|
|
34
53
|
* InputEvent schema
|
|
35
54
|
*/
|
|
@@ -60,7 +79,8 @@ const inputEventSchema = z.object({
|
|
|
60
79
|
commands: z
|
|
61
80
|
.array(z.string())
|
|
62
81
|
.optional()
|
|
63
|
-
.describe('浏览器编辑命令(keydown 专用),如
|
|
82
|
+
.describe('浏览器编辑命令(keydown 专用),如 selectAll、copy、paste、cut、undo、redo;' +
|
|
83
|
+
'成功表示命令已分发,页面结果需用 postCondition 或 extract 验证')
|
|
64
84
|
.describe('用于跨平台快捷键场景,需要 inputMode=precise'),
|
|
65
85
|
button: z.enum(['left', 'middle', 'right', 'back', 'forward']).optional().describe('鼠标按钮'),
|
|
66
86
|
clickCount: z
|
|
@@ -108,7 +128,10 @@ const inputEventSchema = z.object({
|
|
|
108
128
|
const inputSchema = z.object({
|
|
109
129
|
events: z.array(inputEventSchema).describe('事件序列'),
|
|
110
130
|
humanize: z.boolean().optional().describe('启用人类行为模拟(贝塞尔曲线移动、随机延迟)'),
|
|
111
|
-
diagnostics: z
|
|
131
|
+
diagnostics: z
|
|
132
|
+
.boolean()
|
|
133
|
+
.optional()
|
|
134
|
+
.describe('执行后返回新增 console error/warning 和失败网络请求摘要。访问 Cloudflare 页面时默认不传;开启后会启用 debugger 日志采集'),
|
|
112
135
|
postCondition: postConditionSchema
|
|
113
136
|
.optional()
|
|
114
137
|
.describe('动作执行后要验证的页面状态;不传时 success 只表示事件已发出,不表示业务结果已达成'),
|
|
@@ -116,7 +139,7 @@ const inputSchema = z.object({
|
|
|
116
139
|
.string()
|
|
117
140
|
.optional()
|
|
118
141
|
.describe('目标 Tab ID(可选,仅 Extension 模式),不指定则使用当前 attach 的 tab,可操作非当前 attach 的 tab,CDP 模式下不支持此参数'),
|
|
119
|
-
timeout: z.number().optional().describe('
|
|
142
|
+
timeout: z.number().optional().describe('超时毫秒,含 Cloudflare Challenge 等待和事件派发'),
|
|
120
143
|
frame: z
|
|
121
144
|
.union([z.string(), z.number()])
|
|
122
145
|
.optional()
|
|
@@ -133,6 +156,7 @@ class StructuredToolError extends Error {
|
|
|
133
156
|
this.context = context;
|
|
134
157
|
this.name = 'StructuredToolError';
|
|
135
158
|
}
|
|
159
|
+
// noinspection JSUnusedGlobalSymbols — formatErrorResponse 通过结构化 toJSON 调用
|
|
136
160
|
toJSON() {
|
|
137
161
|
return {
|
|
138
162
|
error: {
|
|
@@ -144,21 +168,57 @@ class StructuredToolError extends Error {
|
|
|
144
168
|
};
|
|
145
169
|
}
|
|
146
170
|
}
|
|
171
|
+
function sanitizeInputFailureResponse(response, events) {
|
|
172
|
+
const sensitiveValues = events
|
|
173
|
+
.flatMap((event) => [event.find, event.text])
|
|
174
|
+
.filter((value) => !!value);
|
|
175
|
+
if (sensitiveValues.length === 0) {
|
|
176
|
+
return response;
|
|
177
|
+
}
|
|
178
|
+
for (const item of response.content) {
|
|
179
|
+
try {
|
|
180
|
+
item.text = JSON.stringify(redactSensitiveInputData(JSON.parse(item.text), sensitiveValues), null, 2);
|
|
181
|
+
}
|
|
182
|
+
catch {
|
|
183
|
+
item.text = redactSensitiveInputData(item.text, sensitiveValues);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
return response;
|
|
187
|
+
}
|
|
147
188
|
/**
|
|
148
189
|
* input 工具处理器
|
|
149
190
|
*/
|
|
150
191
|
async function handleInput(args) {
|
|
151
192
|
try {
|
|
193
|
+
if (args.events.length === 0) {
|
|
194
|
+
return {
|
|
195
|
+
content: [
|
|
196
|
+
{
|
|
197
|
+
type: 'text',
|
|
198
|
+
text: JSON.stringify({
|
|
199
|
+
error: {
|
|
200
|
+
code: 'INVALID_ARGUMENT',
|
|
201
|
+
message: 'events 至少需要一个事件,未执行输入操作',
|
|
202
|
+
},
|
|
203
|
+
actionExecuted: false,
|
|
204
|
+
actionStatus: 'failed',
|
|
205
|
+
}),
|
|
206
|
+
},
|
|
207
|
+
],
|
|
208
|
+
isError: true,
|
|
209
|
+
};
|
|
210
|
+
}
|
|
152
211
|
const unifiedSession = getUnifiedSession();
|
|
153
|
-
const mode = unifiedSession.getMode();
|
|
154
212
|
const humanize = args.humanize ?? false;
|
|
155
213
|
return await unifiedSession.withTabId(args.tabId, async () => {
|
|
156
214
|
return await unifiedSession.withFrame(args.frame, async () => {
|
|
215
|
+
const mode = unifiedSession.getMode();
|
|
216
|
+
const inputStartedAt = Date.now();
|
|
217
|
+
await resolveChallenge(unifiedSession, 'input', 'not_started', args.timeout);
|
|
157
218
|
const diagnostics = await startDiagnostics(unifiedSession, args.diagnostics);
|
|
158
219
|
const warnings = [];
|
|
159
220
|
const eventResults = [];
|
|
160
|
-
const session = mode === '
|
|
161
|
-
const inputStartedAt = Date.now();
|
|
221
|
+
const session = mode === 'cdp' ? getSession() : undefined;
|
|
162
222
|
let eventsExecuted = 0;
|
|
163
223
|
try {
|
|
164
224
|
for (const event of args.events) {
|
|
@@ -166,7 +226,7 @@ async function handleInput(args) {
|
|
|
166
226
|
? undefined
|
|
167
227
|
: Math.max(0, args.timeout - (Date.now() - inputStartedAt));
|
|
168
228
|
if (eventTimeout !== undefined && eventTimeout <= 0) {
|
|
169
|
-
|
|
229
|
+
throwInputTimeoutError(args.timeout);
|
|
170
230
|
}
|
|
171
231
|
const result = await executeInputEvent({ unifiedSession, session, mode, humanize, timeout: eventTimeout, frame: args.frame }, event);
|
|
172
232
|
++eventsExecuted;
|
|
@@ -181,6 +241,22 @@ async function handleInput(args) {
|
|
|
181
241
|
catch (error) {
|
|
182
242
|
const response = formatErrorResponse(error);
|
|
183
243
|
const text = response.content[0]?.text;
|
|
244
|
+
if ((error instanceof ChallengeTimeoutError || error instanceof ChallengeDeniedError) && text) {
|
|
245
|
+
try {
|
|
246
|
+
const payload = JSON.parse(text);
|
|
247
|
+
Object.assign(payload, {
|
|
248
|
+
eventsExecuted,
|
|
249
|
+
remainingEventsNotExecuted: args.events.length - eventsExecuted,
|
|
250
|
+
verificationRequested: Boolean(args.postCondition),
|
|
251
|
+
});
|
|
252
|
+
appendDiagnostics(payload, await finishDiagnostics(unifiedSession, diagnostics));
|
|
253
|
+
response.content[0].text = JSON.stringify(payload, null, 2);
|
|
254
|
+
}
|
|
255
|
+
catch {
|
|
256
|
+
// 保留原始 Challenge 错误响应
|
|
257
|
+
}
|
|
258
|
+
return sanitizeInputFailureResponse(response, args.events);
|
|
259
|
+
}
|
|
184
260
|
if (text) {
|
|
185
261
|
try {
|
|
186
262
|
const payload = JSON.parse(text);
|
|
@@ -202,7 +278,7 @@ async function handleInput(args) {
|
|
|
202
278
|
// 保留原始错误响应
|
|
203
279
|
}
|
|
204
280
|
}
|
|
205
|
-
return response;
|
|
281
|
+
return sanitizeInputFailureResponse(response, args.events);
|
|
206
282
|
}
|
|
207
283
|
const result = {
|
|
208
284
|
success: true,
|
|
@@ -260,7 +336,7 @@ async function handleInput(args) {
|
|
|
260
336
|
// 保留原始错误响应
|
|
261
337
|
}
|
|
262
338
|
}
|
|
263
|
-
return response;
|
|
339
|
+
return sanitizeInputFailureResponse(response, args.events);
|
|
264
340
|
}
|
|
265
341
|
}
|
|
266
342
|
const unifiedOnlyEventTypes = new Set([
|
|
@@ -272,7 +348,14 @@ const unifiedOnlyEventTypes = new Set([
|
|
|
272
348
|
]);
|
|
273
349
|
async function executeInputEvent(context, event) {
|
|
274
350
|
if (context.mode === 'extension' || unifiedOnlyEventTypes.has(event.type)) {
|
|
275
|
-
return executeUnifiedEvent(
|
|
351
|
+
return executeUnifiedEvent({
|
|
352
|
+
unifiedSession: context.unifiedSession,
|
|
353
|
+
session: context.session,
|
|
354
|
+
mode: context.mode,
|
|
355
|
+
humanize: context.humanize,
|
|
356
|
+
timeout: context.timeout,
|
|
357
|
+
frame: context.frame,
|
|
358
|
+
}, event);
|
|
276
359
|
}
|
|
277
360
|
if (!context.session) {
|
|
278
361
|
throw new Error('CDP 输入事件缺少 session');
|
|
@@ -321,7 +404,18 @@ async function focusTargetForCommands(unifiedSession, target, timeout, frame) {
|
|
|
321
404
|
throw new StructuredToolError('TARGET_FOCUS_FAILED', '命令目标未成功聚焦', '请检查目标是否可编辑、未 disabled 并处于正确 frame', await buildTargetDiagnosticContext(unifiedSession, target, els.length, nth0, timeout, '命令目标聚焦失败', frame));
|
|
322
405
|
}
|
|
323
406
|
}
|
|
324
|
-
async function
|
|
407
|
+
export async function focusCdpTarget(session, target, timeout) {
|
|
408
|
+
const locator = session.createLocator(target, timeout !== undefined ? { timeout } : undefined);
|
|
409
|
+
const focused = await locator.evaluateOn(`function() {
|
|
410
|
+
if (!(this instanceof HTMLElement)) return false;
|
|
411
|
+
this.focus({preventScroll: true});
|
|
412
|
+
return document.activeElement === this;
|
|
413
|
+
}`);
|
|
414
|
+
if (!focused) {
|
|
415
|
+
throw new StructuredToolError('TARGET_FOCUS_FAILED', '目标元素未成功聚焦', '请检查目标是否可编辑、未 disabled,并确认 locator 指向 input、textarea 或 contenteditable 元素', { target });
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
async function focusTargetIfNeeded(unifiedSession, session, mode, target, timeout, frame) {
|
|
325
419
|
if (!target) {
|
|
326
420
|
return;
|
|
327
421
|
}
|
|
@@ -331,6 +425,13 @@ async function focusTargetIfNeeded(unifiedSession, target, timeout, frame) {
|
|
|
331
425
|
await unifiedSession.mouseClick('left');
|
|
332
426
|
return;
|
|
333
427
|
}
|
|
428
|
+
if (mode === 'cdp') {
|
|
429
|
+
if (!session) {
|
|
430
|
+
throw new Error('CDP locator 聚焦缺少 session');
|
|
431
|
+
}
|
|
432
|
+
await focusCdpTarget(session, target, timeout);
|
|
433
|
+
return;
|
|
434
|
+
}
|
|
334
435
|
const params = targetToFindParams(target);
|
|
335
436
|
const els = await unifiedSession.find(params.selector, params.text, params.xpath, timeout);
|
|
336
437
|
const nth0 = params.nth ?? 0;
|
|
@@ -357,11 +458,16 @@ async function collectTextSelectionDiagnostics(unifiedSession, findText, scopeTa
|
|
|
357
458
|
while (current && current.nodeType === Node.ELEMENT_NODE && parts.length < 4) {
|
|
358
459
|
var part = current.tagName.toLowerCase();
|
|
359
460
|
if (current.classList && current.classList.length) {
|
|
360
|
-
part += '.' + Array.from(current.classList)
|
|
461
|
+
part += '.' + Array.from(current.classList)
|
|
462
|
+
.slice(0, 2)
|
|
463
|
+
.map(function(cls) { return CSS.escape(cls); })
|
|
464
|
+
.join('.');
|
|
361
465
|
}
|
|
362
466
|
var parent = current.parentElement;
|
|
363
467
|
if (parent) {
|
|
364
|
-
var siblings = Array.from(parent.children).filter(function(child) {
|
|
468
|
+
var siblings = Array.from(parent.children).filter(function(child) {
|
|
469
|
+
return child.tagName === current.tagName;
|
|
470
|
+
});
|
|
365
471
|
if (siblings.length > 1) part += ':nth-of-type(' + (siblings.indexOf(current) + 1) + ')';
|
|
366
472
|
}
|
|
367
473
|
parts.unshift(part);
|
|
@@ -373,13 +479,22 @@ async function collectTextSelectionDiagnostics(unifiedSession, findText, scopeTa
|
|
|
373
479
|
if (!(el instanceof Element)) return null;
|
|
374
480
|
var rect = el.getBoundingClientRect();
|
|
375
481
|
var value = 'value' in el ? String(el.value || '') : '';
|
|
376
|
-
var
|
|
482
|
+
var inputType = el instanceof HTMLInputElement ? (el.type || 'text').toLowerCase() : undefined;
|
|
483
|
+
var sensitive = inputType === 'password';
|
|
484
|
+
var text = sensitive
|
|
485
|
+
? (el.innerText || el.textContent || '')
|
|
486
|
+
: (el.innerText || el.textContent || value || '');
|
|
487
|
+
text = text.replace(new RegExp('\\\\s+', 'g'), ' ').trim();
|
|
377
488
|
return {
|
|
378
489
|
tag: el.tagName.toLowerCase(),
|
|
379
490
|
id: el.id || undefined,
|
|
380
491
|
selector: selectorFor(el),
|
|
381
|
-
|
|
382
|
-
|
|
492
|
+
inputType: inputType,
|
|
493
|
+
text: text ? text.slice(0, 160) : undefined,
|
|
494
|
+
valuePreview: !sensitive && value ? value.slice(0, 160) : undefined,
|
|
495
|
+
valueLength: value.length,
|
|
496
|
+
hasValue: value.length > 0,
|
|
497
|
+
redacted: sensitive || undefined,
|
|
383
498
|
visible: rect.width > 0 && rect.height > 0,
|
|
384
499
|
disabled: Boolean(el.disabled),
|
|
385
500
|
readOnly: Boolean(el.readOnly),
|
|
@@ -408,16 +523,23 @@ async function collectTextSelectionDiagnostics(unifiedSession, findText, scopeTa
|
|
|
408
523
|
var candidateElements = [];
|
|
409
524
|
if (root instanceof Element) {
|
|
410
525
|
candidateElements.push(root);
|
|
411
|
-
|
|
526
|
+
var candidateSelector =
|
|
527
|
+
'input, textarea, [contenteditable="true"], [role="textbox"], button, a';
|
|
528
|
+
candidateElements = candidateElements.concat(
|
|
529
|
+
Array.from(root.querySelectorAll(candidateSelector)).slice(0, 12)
|
|
530
|
+
);
|
|
412
531
|
}
|
|
413
532
|
var active = document.activeElement;
|
|
533
|
+
var activeIsPassword =
|
|
534
|
+
active instanceof HTMLInputElement &&
|
|
535
|
+
(active.type || 'text').toLowerCase() === 'password';
|
|
414
536
|
var selection = window.getSelection();
|
|
415
537
|
return {
|
|
416
538
|
scope: {
|
|
417
539
|
target: scopeTarget,
|
|
418
540
|
targetNth: scopeNth,
|
|
419
541
|
occurrenceNth: occurrenceNth,
|
|
420
|
-
|
|
542
|
+
findLength: findText.length,
|
|
421
543
|
selector: scopeSelector,
|
|
422
544
|
text: scopeText,
|
|
423
545
|
xpath: scopeXpath,
|
|
@@ -425,14 +547,18 @@ async function collectTextSelectionDiagnostics(unifiedSession, findText, scopeTa
|
|
|
425
547
|
},
|
|
426
548
|
activeElement: summarizeElement(active),
|
|
427
549
|
selection: {
|
|
428
|
-
text: selection ? selection.toString().slice(0, 160) : '',
|
|
550
|
+
text: activeIsPassword ? undefined : (selection ? selection.toString().slice(0, 160) : ''),
|
|
551
|
+
textLength: selection ? selection.toString().length : 0,
|
|
552
|
+
redacted: activeIsPassword || undefined,
|
|
429
553
|
collapsed: selection ? selection.isCollapsed : true,
|
|
430
554
|
anchorNode: selection && selection.anchorNode ? selection.anchorNode.nodeName : null,
|
|
431
555
|
focusNode: selection && selection.focusNode ? selection.focusNode.nodeName : null,
|
|
432
556
|
inputSelection: active && (active.tagName === 'INPUT' || active.tagName === 'TEXTAREA') ? {
|
|
433
557
|
selectionStart: active.selectionStart,
|
|
434
558
|
selectionEnd: active.selectionEnd,
|
|
435
|
-
valuePreview: String(active.value || '').slice(0, 160)
|
|
559
|
+
valuePreview: activeIsPassword ? undefined : String(active.value || '').slice(0, 160),
|
|
560
|
+
valueLength: String(active.value || '').length,
|
|
561
|
+
redacted: activeIsPassword || undefined
|
|
436
562
|
} : undefined
|
|
437
563
|
},
|
|
438
564
|
candidates: candidateElements.map(summarizeElement).filter(Boolean)
|
|
@@ -453,6 +579,7 @@ async function selectText(unifiedSession, findText, scopeTarget, nth = 0, timeou
|
|
|
453
579
|
scopeNth = params.nth ?? 0;
|
|
454
580
|
}
|
|
455
581
|
// Step 1: 注入脚本定位文本
|
|
582
|
+
//noinspection SpellCheckingInspection -- 模板中的坐标字段名
|
|
456
583
|
const result = await unifiedSession.evaluate(`function(findText, nth, scopeNth, scopeSelector, scopeText, scopeXpath) {
|
|
457
584
|
// 确定搜索根节点
|
|
458
585
|
var root = document.body;
|
|
@@ -480,9 +607,16 @@ async function selectText(unifiedSession, findText, scopeTarget, nth = 0, timeou
|
|
|
480
607
|
if (tag === 'INPUT' || tag === 'TEXTAREA') {
|
|
481
608
|
var val = root.value || '';
|
|
482
609
|
var inputType = tag === 'INPUT' ? (root.type || 'text').toLowerCase() : 'textarea';
|
|
483
|
-
var selectionCapable =
|
|
610
|
+
var selectionCapable =
|
|
611
|
+
tag === 'TEXTAREA' ||
|
|
612
|
+
['text', 'search', 'tel', 'url', 'password'].indexOf(inputType) !== -1;
|
|
484
613
|
if (!selectionCapable) {
|
|
485
|
-
return {
|
|
614
|
+
return {
|
|
615
|
+
type: 'unsupported',
|
|
616
|
+
tag: tag.toLowerCase(),
|
|
617
|
+
inputType: inputType,
|
|
618
|
+
currentValue: String(val).slice(0, 160)
|
|
619
|
+
};
|
|
486
620
|
}
|
|
487
621
|
var pos = -1;
|
|
488
622
|
for (var n = 0; n <= nth; n++) {
|
|
@@ -501,14 +635,21 @@ async function selectText(unifiedSession, findText, scopeTarget, nth = 0, timeou
|
|
|
501
635
|
var inp = inputs[k];
|
|
502
636
|
var v = inp.value || '';
|
|
503
637
|
var childType = inp.tagName === 'INPUT' ? (inp.type || 'text').toLowerCase() : 'textarea';
|
|
504
|
-
var childSelectionCapable =
|
|
638
|
+
var childSelectionCapable =
|
|
639
|
+
inp.tagName === 'TEXTAREA' ||
|
|
640
|
+
['text', 'search', 'tel', 'url', 'password'].indexOf(childType) !== -1;
|
|
505
641
|
var ip = -1;
|
|
506
642
|
for (var n2 = 0; n2 <= nth; n2++) {
|
|
507
643
|
ip = v.indexOf(findText, ip + (n2 > 0 ? 1 : 0));
|
|
508
644
|
if (ip === -1) break;
|
|
509
645
|
}
|
|
510
646
|
if (ip !== -1 && !childSelectionCapable) {
|
|
511
|
-
return {
|
|
647
|
+
return {
|
|
648
|
+
type: 'unsupported',
|
|
649
|
+
tag: inp.tagName.toLowerCase(),
|
|
650
|
+
inputType: childType,
|
|
651
|
+
currentValue: String(v).slice(0, 160)
|
|
652
|
+
};
|
|
512
653
|
}
|
|
513
654
|
if (ip !== -1) {
|
|
514
655
|
inp.focus();
|
|
@@ -569,6 +710,19 @@ async function selectText(unifiedSession, findText, scopeTarget, nth = 0, timeou
|
|
|
569
710
|
formatted = startNode.tagName.toLowerCase();
|
|
570
711
|
}
|
|
571
712
|
|
|
713
|
+
// contenteditable 使用 DOM Range 建立精确选区;CDP 的 Shift+Click 会把整个编辑区选中。
|
|
714
|
+
if (root instanceof HTMLElement && root.isContentEditable) {
|
|
715
|
+
var selection = window.getSelection();
|
|
716
|
+
if (!selection) return {type: 'notfound'};
|
|
717
|
+
var selectionRange = document.createRange();
|
|
718
|
+
selectionRange.setStart(s.node, s.offset);
|
|
719
|
+
selectionRange.setEnd(e.node, Math.min(e.offset + 1, e.node.textContent.length));
|
|
720
|
+
root.focus();
|
|
721
|
+
selection.removeAllRanges();
|
|
722
|
+
selection.addRange(selectionRange);
|
|
723
|
+
return {type: 'range', formatted: formatted || undefined};
|
|
724
|
+
}
|
|
725
|
+
|
|
572
726
|
return {
|
|
573
727
|
type: 'coords',
|
|
574
728
|
startX: sr.x + 1,
|
|
@@ -601,6 +755,9 @@ async function selectText(unifiedSession, findText, scopeTarget, nth = 0, timeou
|
|
|
601
755
|
// 注入脚本已完成 focus + setSelectionRange(原子化,避免外层 mouseClick 聚焦不可靠)
|
|
602
756
|
return undefined;
|
|
603
757
|
}
|
|
758
|
+
if (result.type === 'range') {
|
|
759
|
+
return result.formatted;
|
|
760
|
+
}
|
|
604
761
|
// DOM 文本节点:鼠标选择
|
|
605
762
|
const coords = result;
|
|
606
763
|
// iframe 坐标修正(precise 模式需要视口绝对坐标)
|
|
@@ -620,59 +777,6 @@ async function selectText(unifiedSession, findText, scopeTarget, nth = 0, timeou
|
|
|
620
777
|
await unifiedSession.keyUp('Shift');
|
|
621
778
|
return coords.formatted;
|
|
622
779
|
}
|
|
623
|
-
async function executeEditingCommands(unifiedSession, commands, timeout) {
|
|
624
|
-
const result = await unifiedSession.evaluate(`function(commands) {
|
|
625
|
-
function summarizeSelection() {
|
|
626
|
-
var active = document.activeElement;
|
|
627
|
-
if (active && (active.tagName === 'INPUT' || active.tagName === 'TEXTAREA')) {
|
|
628
|
-
return {
|
|
629
|
-
type: active.tagName.toLowerCase(),
|
|
630
|
-
selectionStart: active.selectionStart,
|
|
631
|
-
selectionEnd: active.selectionEnd,
|
|
632
|
-
selectedText: String(active.value || '').slice(active.selectionStart || 0, active.selectionEnd || 0)
|
|
633
|
-
};
|
|
634
|
-
}
|
|
635
|
-
var selection = window.getSelection();
|
|
636
|
-
return {
|
|
637
|
-
type: 'document',
|
|
638
|
-
text: selection ? selection.toString() : '',
|
|
639
|
-
collapsed: selection ? selection.isCollapsed : true
|
|
640
|
-
};
|
|
641
|
-
}
|
|
642
|
-
function commandSelectAll() {
|
|
643
|
-
var active = document.activeElement;
|
|
644
|
-
if (active && (active.tagName === 'INPUT' || active.tagName === 'TEXTAREA') && typeof active.select === 'function') {
|
|
645
|
-
active.select();
|
|
646
|
-
active.dispatchEvent(new Event('select', {bubbles: true}));
|
|
647
|
-
return true;
|
|
648
|
-
}
|
|
649
|
-
if (active && active.isContentEditable) {
|
|
650
|
-
var range = document.createRange();
|
|
651
|
-
range.selectNodeContents(active);
|
|
652
|
-
var selection = window.getSelection();
|
|
653
|
-
if (!selection) return false;
|
|
654
|
-
selection.removeAllRanges();
|
|
655
|
-
selection.addRange(range);
|
|
656
|
-
return true;
|
|
657
|
-
}
|
|
658
|
-
return document.execCommand('selectAll');
|
|
659
|
-
}
|
|
660
|
-
var executed = commands.map(function(command) {
|
|
661
|
-
if (command === 'selectAll') {
|
|
662
|
-
return {command: command, success: commandSelectAll()};
|
|
663
|
-
}
|
|
664
|
-
if (['copy', 'cut', 'paste', 'undo', 'redo'].indexOf(command) !== -1) {
|
|
665
|
-
return {command: command, success: document.execCommand(command)};
|
|
666
|
-
}
|
|
667
|
-
return {command: command, success: false};
|
|
668
|
-
});
|
|
669
|
-
return {success: executed.every(function(item) { return item.success; }), executed: executed, selection: summarizeSelection()};
|
|
670
|
-
}`, undefined, timeout, [commands]);
|
|
671
|
-
if (!result.success) {
|
|
672
|
-
throw new StructuredToolError('EDIT_COMMAND_FAILED', `编辑命令执行失败: ${commands.join(', ')}`, '请确认当前页面已有可编辑焦点,或给 keydown 事件提供 target 参数', result);
|
|
673
|
-
}
|
|
674
|
-
return result;
|
|
675
|
-
}
|
|
676
780
|
/**
|
|
677
781
|
* 验证事件参数(两种执行模式共享),避免在 Extension/CDP 两个 switch 中重复校验
|
|
678
782
|
*/
|
|
@@ -743,13 +847,13 @@ function validateEvent(event) {
|
|
|
743
847
|
break;
|
|
744
848
|
}
|
|
745
849
|
}
|
|
746
|
-
async function executeUnifiedEvent(
|
|
850
|
+
async function executeUnifiedEvent(context, event) {
|
|
747
851
|
validateEvent(event);
|
|
748
852
|
const handler = unifiedInputHandlers[event.type];
|
|
749
853
|
if (!handler) {
|
|
750
854
|
throw new Error(`未知事件类型: ${event.type}`);
|
|
751
855
|
}
|
|
752
|
-
return handler(
|
|
856
|
+
return handler(context, event);
|
|
753
857
|
}
|
|
754
858
|
const unifiedInputHandlers = {
|
|
755
859
|
keydown: handleUnifiedKeyDown,
|
|
@@ -779,9 +883,6 @@ async function handleUnifiedKeyDown({ unifiedSession, timeout, frame }, event) {
|
|
|
779
883
|
await focusTargetForCommands(unifiedSession, event.target, timeout, frame);
|
|
780
884
|
}
|
|
781
885
|
await unifiedSession.keyDown(event.key, event.commands);
|
|
782
|
-
if (event.commands && event.commands.length > 0) {
|
|
783
|
-
return await executeEditingCommands(unifiedSession, event.commands, timeout);
|
|
784
|
-
}
|
|
785
886
|
return undefined;
|
|
786
887
|
}
|
|
787
888
|
async function handleUnifiedKeyUp({ unifiedSession }, event) {
|
|
@@ -914,12 +1015,16 @@ async function handleUnifiedWait({ timeout }, event) {
|
|
|
914
1015
|
await new Promise((resolve) => setTimeout(resolve, event.ms));
|
|
915
1016
|
return undefined;
|
|
916
1017
|
}
|
|
917
|
-
async function handleUnifiedSelect({ unifiedSession, timeout, frame }, event) {
|
|
918
|
-
await focusTargetIfNeeded(unifiedSession, event.target, timeout, frame);
|
|
1018
|
+
async function handleUnifiedSelect({ unifiedSession, session, mode, timeout, frame }, event) {
|
|
1019
|
+
await focusTargetIfNeeded(unifiedSession, session, mode, event.target, timeout, frame);
|
|
919
1020
|
const selectionTarget = await unifiedSession.evaluate(`(() => {
|
|
920
1021
|
const el = document.activeElement;
|
|
921
1022
|
if (!(el instanceof HTMLInputElement)) return null;
|
|
922
|
-
return {
|
|
1023
|
+
return {
|
|
1024
|
+
tag: 'input',
|
|
1025
|
+
inputType: (el.type || 'text').toLowerCase(),
|
|
1026
|
+
value: String(el.value || '').slice(0, 160)
|
|
1027
|
+
};
|
|
923
1028
|
})()`, undefined, timeout);
|
|
924
1029
|
if (selectionTarget && !supportsTextSelection(selectionTarget.tag, selectionTarget.inputType)) {
|
|
925
1030
|
throw new StructuredToolError('UNSUPPORTED_SELECTION', `input[type=${selectionTarget.inputType}] 不支持文本 selection`, '请改用 type mode="controlled" 设置完整 value,replace 事件会对这类 input 使用完整 value 更新路径', {
|
|
@@ -933,8 +1038,8 @@ async function handleUnifiedSelect({ unifiedSession, timeout, frame }, event) {
|
|
|
933
1038
|
await selectText(unifiedSession, event.find, event.target, event.nth, timeout);
|
|
934
1039
|
return undefined;
|
|
935
1040
|
}
|
|
936
|
-
async function handleUnifiedReplace({ unifiedSession, timeout, frame }, event) {
|
|
937
|
-
await focusTargetIfNeeded(unifiedSession, event.target, timeout, frame);
|
|
1041
|
+
async function handleUnifiedReplace({ unifiedSession, session, mode, timeout, frame }, event) {
|
|
1042
|
+
await focusTargetIfNeeded(unifiedSession, session, mode, event.target, timeout, frame);
|
|
938
1043
|
const directResult = await unifiedSession.evaluate(`function(findText, replacementText, nth) {
|
|
939
1044
|
var el = document.activeElement;
|
|
940
1045
|
if (!(el instanceof HTMLInputElement)) return {handled: false};
|
|
@@ -944,17 +1049,54 @@ async function handleUnifiedReplace({ unifiedSession, timeout, frame }, event) {
|
|
|
944
1049
|
var pos = -1;
|
|
945
1050
|
for (var i = 0; i <= nth; i++) {
|
|
946
1051
|
pos = current.indexOf(findText, pos + (i > 0 ? 1 : 0));
|
|
947
|
-
if (pos === -1)
|
|
1052
|
+
if (pos === -1) {
|
|
1053
|
+
return {
|
|
1054
|
+
handled: true,
|
|
1055
|
+
success: false,
|
|
1056
|
+
tag: 'input',
|
|
1057
|
+
inputType: type,
|
|
1058
|
+
currentValue: current.slice(0, 160),
|
|
1059
|
+
reason: 'text_not_found'
|
|
1060
|
+
};
|
|
1061
|
+
}
|
|
1062
|
+
}
|
|
1063
|
+
if (el.disabled || el.readOnly) {
|
|
1064
|
+
return {
|
|
1065
|
+
handled: true,
|
|
1066
|
+
success: false,
|
|
1067
|
+
tag: 'input',
|
|
1068
|
+
inputType: type,
|
|
1069
|
+
currentValue: current.slice(0, 160),
|
|
1070
|
+
reason: 'not_editable'
|
|
1071
|
+
};
|
|
948
1072
|
}
|
|
949
|
-
if (el.disabled || el.readOnly) return {handled: true, success: false, tag: 'input', inputType: type, currentValue: current.slice(0, 160), reason: 'not_editable'};
|
|
950
1073
|
var requested = current.slice(0, pos) + replacementText + current.slice(pos + findText.length);
|
|
951
1074
|
var setter = Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, 'value')?.set;
|
|
952
|
-
if (!setter)
|
|
1075
|
+
if (!setter) {
|
|
1076
|
+
return {
|
|
1077
|
+
handled: true,
|
|
1078
|
+
success: false,
|
|
1079
|
+
tag: 'input',
|
|
1080
|
+
inputType: type,
|
|
1081
|
+
currentValue: current.slice(0, 160),
|
|
1082
|
+
requestedValue: requested.slice(0, 160),
|
|
1083
|
+
reason: 'no_native_setter'
|
|
1084
|
+
};
|
|
1085
|
+
}
|
|
953
1086
|
setter.call(el, requested);
|
|
954
1087
|
el.dispatchEvent(new Event('input', {bubbles: true}));
|
|
955
1088
|
el.dispatchEvent(new Event('change', {bubbles: true}));
|
|
956
1089
|
var actual = String(el.value || '');
|
|
957
|
-
return {
|
|
1090
|
+
return {
|
|
1091
|
+
handled: true,
|
|
1092
|
+
success: actual === requested,
|
|
1093
|
+
tag: 'input',
|
|
1094
|
+
inputType: type,
|
|
1095
|
+
currentValue: current.slice(0, 160),
|
|
1096
|
+
requestedValue: requested.slice(0, 160),
|
|
1097
|
+
actualValue: actual.slice(0, 160),
|
|
1098
|
+
reason: actual === requested ? undefined : 'value_not_applied'
|
|
1099
|
+
};
|
|
958
1100
|
}`, undefined, timeout, [event.find, event.text, event.nth ?? 0]);
|
|
959
1101
|
if (directResult.handled) {
|
|
960
1102
|
if (!directResult.success) {
|
|
@@ -1169,7 +1311,9 @@ async function editorAction(unifiedSession, action, event, timeout, frame) {
|
|
|
1169
1311
|
isContentEditable: !!editable.isContentEditable
|
|
1170
1312
|
} : null,
|
|
1171
1313
|
selectedText: selectedText,
|
|
1172
|
-
selectionCollapsed: isInput
|
|
1314
|
+
selectionCollapsed: isInput
|
|
1315
|
+
? active.selectionStart === active.selectionEnd
|
|
1316
|
+
: !sel || sel.isCollapsed
|
|
1173
1317
|
};
|
|
1174
1318
|
}
|
|
1175
1319
|
|
|
@@ -1429,7 +1573,16 @@ async function getTargetPoint(session, target, timeout, waitType = 'click') {
|
|
|
1429
1573
|
*/
|
|
1430
1574
|
export function registerInputTool(server) {
|
|
1431
1575
|
server.registerTool('input', {
|
|
1432
|
-
description:
|
|
1576
|
+
description: [
|
|
1577
|
+
'键鼠输入:键盘、鼠标及任意组合。默认优先使用已连接的 Chrome Extension;',
|
|
1578
|
+
'只有 Extension 不可用或 browse(action="connect", port=…) 已显式选择 CDP 时才使用 CDP。',
|
|
1579
|
+
'访问 Cloudflare 时默认不传 diagnostics,Extension 先被动等待真实 tab 标题恢复,',
|
|
1580
|
+
'持续存在可点击 Turnstile 时才点击。同页测试按钮走页面点击;',
|
|
1581
|
+
'Extension inspect 走 isolated world,不先挂 debugger。',
|
|
1582
|
+
'Cloudflare iframe 或宿主页验证按钮只在已聚焦窗口的活动受控 tab 上用系统鼠标,',
|
|
1583
|
+
'Linux 用窗口原点加边距,沿路径移动、悬停后再按下抬起,混合窗口不会调用 focusWindow',
|
|
1584
|
+
].join('') +
|
|
1585
|
+
`
|
|
1433
1586
|
|
|
1434
1587
|
推荐操作顺序:
|
|
1435
1588
|
1. 先用 extract type=state 或 type=html 了解页面结构
|
|
@@ -1457,6 +1610,7 @@ export function registerInputTool(server) {
|
|
|
1457
1610
|
replace: {events:[{type:"replace",target:{css:"textarea"},find:"old",text:"new"}]}
|
|
1458
1611
|
注意:纯键盘事件(不带 commands)仅保证 JS keyboard event 可被监听,不保证触发浏览器原生编辑行为;
|
|
1459
1612
|
全选/复制/粘贴等语义用 commands;"全选并替换文本"用 select/replace 事件更简洁;
|
|
1613
|
+
commands 成功表示浏览器已接受命令分发,页面结果需用 postCondition 或 extract 验证;
|
|
1460
1614
|
commands 仅支持 inputMode=precise,stealth 模式下会报错`,
|
|
1461
1615
|
inputSchema: inputSchema,
|
|
1462
1616
|
}, (args) => handleInput(args));
|