@pyrokine/mcp-chrome 3.1.0 → 3.1.2

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.
Files changed (108) hide show
  1. package/README.md +599 -307
  2. package/README_zh.md +526 -341
  3. package/dist/cdp/client.d.ts +1 -0
  4. package/dist/cdp/client.d.ts.map +1 -1
  5. package/dist/cdp/client.js +16 -8
  6. package/dist/cdp/client.js.map +1 -1
  7. package/dist/core/browser-driver.d.ts +2 -1
  8. package/dist/core/browser-driver.d.ts.map +1 -1
  9. package/dist/core/browser-driver.js.map +1 -1
  10. package/dist/core/browser-events.d.ts +9 -3
  11. package/dist/core/browser-events.d.ts.map +1 -1
  12. package/dist/core/browser-events.js +365 -195
  13. package/dist/core/browser-events.js.map +1 -1
  14. package/dist/core/challenge-page.d.ts.map +1 -1
  15. package/dist/core/challenge-page.js +6 -1
  16. package/dist/core/challenge-page.js.map +1 -1
  17. package/dist/core/error-sanitizer.d.ts.map +1 -1
  18. package/dist/core/error-sanitizer.js +2 -1
  19. package/dist/core/error-sanitizer.js.map +1 -1
  20. package/dist/core/errors.d.ts.map +1 -1
  21. package/dist/core/errors.js +7 -3
  22. package/dist/core/errors.js.map +1 -1
  23. package/dist/core/file-upload.js +1 -1
  24. package/dist/core/file-upload.js.map +1 -1
  25. package/dist/core/locator.d.ts +1 -0
  26. package/dist/core/locator.d.ts.map +1 -1
  27. package/dist/core/locator.js +19 -11
  28. package/dist/core/locator.js.map +1 -1
  29. package/dist/core/os-mouse.d.ts +1 -0
  30. package/dist/core/os-mouse.d.ts.map +1 -1
  31. package/dist/core/os-mouse.js +10 -4
  32. package/dist/core/os-mouse.js.map +1 -1
  33. package/dist/core/session.d.ts +4 -3
  34. package/dist/core/session.d.ts.map +1 -1
  35. package/dist/core/session.js +164 -75
  36. package/dist/core/session.js.map +1 -1
  37. package/dist/core/unified-session.d.ts +10 -5
  38. package/dist/core/unified-session.d.ts.map +1 -1
  39. package/dist/core/unified-session.js +138 -59
  40. package/dist/core/unified-session.js.map +1 -1
  41. package/dist/core/utils.d.ts +5 -1
  42. package/dist/core/utils.d.ts.map +1 -1
  43. package/dist/core/utils.js +8 -2
  44. package/dist/core/utils.js.map +1 -1
  45. package/dist/extension/bridge.d.ts +3 -3
  46. package/dist/extension/bridge.d.ts.map +1 -1
  47. package/dist/extension/bridge.js +58 -17
  48. package/dist/extension/bridge.js.map +1 -1
  49. package/dist/extension/http-server.d.ts +1 -0
  50. package/dist/extension/http-server.d.ts.map +1 -1
  51. package/dist/extension/http-server.js +14 -14
  52. package/dist/extension/http-server.js.map +1 -1
  53. package/dist/index.js +2 -1
  54. package/dist/index.js.map +1 -1
  55. package/dist/tools/browser-event-output.js.map +1 -1
  56. package/dist/tools/challenge.d.ts +1 -0
  57. package/dist/tools/challenge.d.ts.map +1 -1
  58. package/dist/tools/challenge.js +113 -17
  59. package/dist/tools/challenge.js.map +1 -1
  60. package/dist/tools/cookies.d.ts.map +1 -1
  61. package/dist/tools/cookies.js +2 -1
  62. package/dist/tools/cookies.js.map +1 -1
  63. package/dist/tools/diagnostics.d.ts.map +1 -1
  64. package/dist/tools/diagnostics.js +2 -1
  65. package/dist/tools/diagnostics.js.map +1 -1
  66. package/dist/tools/evaluate.d.ts.map +1 -1
  67. package/dist/tools/evaluate.js +4 -2
  68. package/dist/tools/evaluate.js.map +1 -1
  69. package/dist/tools/extract.d.ts.map +1 -1
  70. package/dist/tools/extract.js +152 -58
  71. package/dist/tools/extract.js.map +1 -1
  72. package/dist/tools/input.d.ts.map +1 -1
  73. package/dist/tools/input.js +225 -86
  74. package/dist/tools/input.js.map +1 -1
  75. package/dist/tools/logs.js +1 -1
  76. package/dist/tools/logs.js.map +1 -1
  77. package/dist/tools/manage.d.ts.map +1 -1
  78. package/dist/tools/manage.js +18 -8
  79. package/dist/tools/manage.js.map +1 -1
  80. package/dist/tools/network-sanitizer.d.ts +1 -0
  81. package/dist/tools/network-sanitizer.d.ts.map +1 -1
  82. package/dist/tools/network-sanitizer.js +23 -9
  83. package/dist/tools/network-sanitizer.js.map +1 -1
  84. package/dist/tools/page-context.d.ts +1 -1
  85. package/dist/tools/page-context.d.ts.map +1 -1
  86. package/dist/tools/page-context.js +2 -1
  87. package/dist/tools/page-context.js.map +1 -1
  88. package/dist/tools/png.d.ts.map +1 -1
  89. package/dist/tools/png.js +12 -3
  90. package/dist/tools/png.js.map +1 -1
  91. package/dist/tools/post-condition.d.ts.map +1 -1
  92. package/dist/tools/post-condition.js +57 -33
  93. package/dist/tools/post-condition.js.map +1 -1
  94. package/dist/tools/schema.d.ts +1 -1
  95. package/dist/tools/schema.d.ts.map +1 -1
  96. package/dist/tools/target-diagnostics.d.ts.map +1 -1
  97. package/dist/tools/target-diagnostics.js +53 -33
  98. package/dist/tools/target-diagnostics.js.map +1 -1
  99. package/dist/tools/wait.d.ts.map +1 -1
  100. package/dist/tools/wait.js +4 -4
  101. package/dist/tools/wait.js.map +1 -1
  102. package/extension/dist/assets/{content.ts-BAMOcF3R.js → content.ts-C9QgtUYe.js} +1 -1
  103. package/extension/dist/assets/{content.ts-loader-4F9a1EWZ.js → content.ts-loader-yqB_P8vu.js} +1 -1
  104. package/extension/dist/assets/{index.ts-B79g2ikq.js → index.ts-DBFNh8mZ.js} +8650 -8195
  105. package/extension/dist/assets/{page-scripts-CSxn58h1.js → page-scripts-BjqQ0zXt.js} +1 -1
  106. package/extension/dist/manifest.json +4 -4
  107. package/extension/dist/service-worker-loader.js +1 -1
  108. package/package.json +5 -5
@@ -9,10 +9,11 @@
9
9
  * - humanize 可选:行为模拟是可选功能
10
10
  */
11
11
  import { randomUUID } from 'node:crypto';
12
- import { prepareUploadFiles, uploadInPage } from '../core/file-upload.js';
13
12
  import { z } from 'zod';
14
13
  import { generateBezierPath, getMouseMoveDelay, getTypingDelay, randomDelay } from '../anti-detection/index.js';
14
+ import { installDomHelpers } from '../core/dom-helpers.js';
15
15
  import { BrowserError, ElementNotFoundError, getErrorPageContext, isNativeExecutionResultUnknown, StructuredToolError, TimeoutError, } from '../core/errors.js';
16
+ import { prepareUploadFiles, uploadInPage } from '../core/file-upload.js';
16
17
  import { formatErrorResponse, formatResponse, getSession, getUnifiedSession } from '../core/index.js';
17
18
  import { DEFAULT_TIMEOUT } from '../core/types.js';
18
19
  import { ChallengeDeniedError, challengeResultFields, ChallengeTimeoutError, resolveChallenge } from './challenge.js';
@@ -21,7 +22,6 @@ import { withPageContext } from './page-context.js';
21
22
  import { postConditionSchema, waitForPostCondition } from './post-condition.js';
22
23
  import { frameZodSchema, pageConditionSchema, pageTargetIdSchema, resolvePageTargetId, targetToFindParams, targetZodSchema, } from './schema.js';
23
24
  import { buildTargetDiagnostics, enrichTargetError, targetLookupTimeout } from './target-diagnostics.js';
24
- import { installDomHelpers } from '../core/dom-helpers.js';
25
25
  const INPUT_WAIT_MAX_MS = 60_000;
26
26
  const TEXT_SELECTION_INPUT_TYPES = new Set(['text', 'search', 'tel', 'url', 'password']);
27
27
  function throwInputTimeoutError(timeout) {
@@ -333,11 +333,13 @@ async function handleInput(args, signal) {
333
333
  try {
334
334
  const payload = JSON.parse(text);
335
335
  const message = error instanceof Error ? error.message : String(error);
336
+ const inputActionAcknowledged = getErrorPageContext(error)?.inputActionAcknowledged === true;
336
337
  const timedOut = /timeout|超时/i.test(message);
337
338
  const preActionTimeout = /timeout.*before|超过 input 剩余 timeout/i.test(message);
338
339
  actionMayHaveExecuted =
339
340
  isNativeExecutionResultUnknown(error) || (timedOut && !preActionTimeout);
340
- const preconditionFailed = error !== null &&
341
+ const preconditionFailed = !inputActionAcknowledged &&
342
+ error !== null &&
341
343
  typeof error === 'object' &&
342
344
  'code' in error &&
343
345
  error.code === 'ACTION_PRECONDITION_FAILED';
@@ -345,14 +347,17 @@ async function handleInput(args, signal) {
345
347
  ? error.context
346
348
  : undefined;
347
349
  const contextPhase = getErrorPageContext(error)?.contextPhase;
348
- const preparationFailure = contextPhase === 'preparation' || contextPhase === 'precondition';
349
- const explicitNotStarted = preparationFailure || executionContext?.actionExecuted === false;
350
+ const preparationFailure = !inputActionAcknowledged &&
351
+ (contextPhase === 'preparation' || contextPhase === 'precondition');
352
+ const explicitNotStarted = !inputActionAcknowledged &&
353
+ (preparationFailure || executionContext?.actionExecuted === false);
350
354
  actionExecutedUnknown =
351
355
  !explicitNotStarted && executionContext?.actionExecuted === 'unknown';
352
356
  actionMayHaveExecuted =
353
357
  (actionMayHaveExecuted ||
354
358
  actionExecutedUnknown ||
355
- executionContext?.actionExecuted === true) &&
359
+ executionContext?.actionExecuted === true ||
360
+ inputActionAcknowledged) &&
356
361
  !preconditionFailed &&
357
362
  !explicitNotStarted;
358
363
  const cleanupFailure = progress.actionStatus === 'completed';
@@ -385,7 +390,7 @@ async function handleInput(args, signal) {
385
390
  : 'failed';
386
391
  payload.eventsExecuted = eventsExecuted;
387
392
  payload.actionExecuted =
388
- eventsExecuted > 0
393
+ eventsExecuted > 0 || inputActionAcknowledged
389
394
  ? true
390
395
  : actionExecutedUnknown
391
396
  ? 'unknown'
@@ -532,7 +537,7 @@ async function handleInput(args, signal) {
532
537
  payload.diagnosticsError =
533
538
  eventsExecuted > 0 || actionMayHaveExecuted
534
539
  ? 'input 执行后未能取得 diagnostics'
535
- : 'diagnostics 未能在 tab/frame 初始化失败前启动';
540
+ : 'input 开始前未能完成 diagnostics';
536
541
  }
537
542
  response.content[0].text = JSON.stringify(payload, null, 2);
538
543
  }
@@ -573,8 +578,8 @@ async function executeInputEvent(context, event) {
573
578
  // noinspection ExceptionCaughtLocallyJS -- Report failure
574
579
  throw new Error('CDP 输入事件缺少 session');
575
580
  }
576
- await executeCdpEvent(context.session, event, context.humanize, context.timeout);
577
- return undefined;
581
+ const warning = await executeCdpEvent(context.session, event, context.humanize, context.timeout);
582
+ return typeof warning === 'string' ? warning : undefined;
578
583
  }
579
584
  catch (error) {
580
585
  if (context.mode === 'cdp' && error instanceof ElementNotFoundError) {
@@ -605,8 +610,8 @@ async function focusTargetForCommands(unifiedSession, target, timeout, frame) {
605
610
  const params = targetToFindParams(target);
606
611
  const nth0 = params.nth ?? 0;
607
612
  const els = 'x' in target && 'y' in target
608
- ? await unifiedSession.findAtPoint(target.x, target.y, nth0, remainingInputTimeout(deadline))
609
- : await unifiedSession.find(params.selector, params.text, params.xpath, remainingInputTimeout(deadline), params.semantic, nth0);
613
+ ? await unifiedSession.findAtPoint(target.x, target.y, nth0, remainingInputTimeout(deadline), 'frame')
614
+ : await unifiedSession.find(params.selector, params.text, params.xpath, remainingInputTimeout(deadline), params.semantic, nth0, 'frame');
610
615
  if (els.length === 0) {
611
616
  throw new StructuredToolError(nth0 === 0 ? 'TARGET_NOT_FOUND' : 'TARGET_INDEX_OUT_OF_RANGE', `第 ${nth0} 个命令目标不存在: ${JSON.stringify(target)}`, '请检查 target、frame 和 target.nth,或先用 extract type=state 查看当前候选元素', await buildTargetDiagnosticContext(unifiedSession, target, null, nth0, remainingInputTimeout(deadline), '命令目标未找到', frame));
612
617
  }
@@ -676,7 +681,7 @@ async function focusTargetIfNeeded(unifiedSession, session, mode, target, timeou
676
681
  }
677
682
  const params = targetToFindParams(target);
678
683
  const nth0 = params.nth ?? 0;
679
- const els = await unifiedSession.find(params.selector, params.text, params.xpath, remainingInputTimeout(deadline), params.semantic, nth0);
684
+ const els = await unifiedSession.find(params.selector, params.text, params.xpath, remainingInputTimeout(deadline), params.semantic, nth0, 'frame');
680
685
  if (els.length === 0) {
681
686
  throw new StructuredToolError(nth0 === 0 ? 'TARGET_NOT_FOUND' : 'TARGET_INDEX_OUT_OF_RANGE', `第 ${nth0} 个匹配元素不存在: ${JSON.stringify(target)}`, '请检查 target、frame 和 target.nth,或先用 extract type=state 查看当前候选元素', await buildTargetDiagnosticContext(unifiedSession, target, null, nth0, timeout, '聚焦目标未找到', frame));
682
687
  }
@@ -739,14 +744,15 @@ async function collectTextSelectionDiagnostics(unifiedSession, findText, scopeTa
739
744
  hasValue: value.length > 0,
740
745
  redacted: sensitive || undefined,
741
746
  visible: rect.width > 0 && rect.height > 0,
742
- disabled: Boolean(el.disabled),
747
+ disabled: el.matches(':disabled') || Boolean(el.disabled),
743
748
  readOnly: Boolean(el.readOnly),
744
749
  contentEditable: Boolean(el.isContentEditable),
745
750
  bounds: {x: rect.x, y: rect.y, width: rect.width, height: rect.height}
746
751
  };
747
752
  }
748
- var root = document.body;
749
- if (scopeXpath || scopeSelector || scopeText || scopeSemantic) {
753
+ var point = scopeTarget && 'x' in scopeTarget && 'y' in scopeTarget ? scopeTarget : null;
754
+ var root = point ? helpers.deepElementFromPoint(point.x, point.y) : document.body;
755
+ if (!point && (scopeXpath || scopeSelector || scopeText || scopeSemantic)) {
750
756
  root = helpers.query({
751
757
  selector:scopeSelector, text:scopeText, xpath:scopeXpath, semantic:scopeSemantic
752
758
  })[scopeNth];
@@ -796,6 +802,39 @@ async function collectTextSelectionDiagnostics(unifiedSession, findText, scopeTa
796
802
  };
797
803
  }`, undefined, timeout, [findText, occurrenceNth, scopeNth, scopeTarget ?? null, scopeSelector, scopeText, scopeXpath, scopeSemantic]);
798
804
  }
805
+ function ownsTextSelection(scopeRefId, findText) {
806
+ const reference = window.__mcpElementMap?.[scopeRefId];
807
+ const scope = reference?.deref();
808
+ const original = reference?.selection;
809
+ if (!scope || !scope.isConnected || !original) {
810
+ return false;
811
+ }
812
+ const helpers = globalThis.__mcpDomHelpers;
813
+ const active = helpers.activeElement();
814
+ if (active instanceof HTMLInputElement || active instanceof HTMLTextAreaElement) {
815
+ return (active === scope &&
816
+ active.selectionStart !== null &&
817
+ active.selectionEnd !== null &&
818
+ active.selectionStart === original.startOffset &&
819
+ active.selectionEnd === original.endOffset &&
820
+ active.selectionStart !== active.selectionEnd &&
821
+ active.value.slice(active.selectionStart, active.selectionEnd) === findText);
822
+ }
823
+ const selection = window.getSelection();
824
+ const range = selection?.rangeCount === 1 ? selection.getRangeAt(0) : null;
825
+ const anchor = selection?.anchorNode;
826
+ const focus = selection?.focusNode;
827
+ return Boolean(selection &&
828
+ !selection.isCollapsed &&
829
+ range &&
830
+ range.startContainer === original.startContainer?.deref() &&
831
+ range.endContainer === original.endContainer?.deref() &&
832
+ range.startOffset === original.startOffset &&
833
+ range.endOffset === original.endOffset &&
834
+ range.toString() === findText &&
835
+ helpers.containsComposed(scope, anchor instanceof Element ? anchor : (anchor?.parentElement ?? null)) &&
836
+ helpers.containsComposed(scope, focus instanceof Element ? focus : (focus?.parentElement ?? null)));
837
+ }
799
838
  async function selectText(unifiedSession, findText, scopeTarget, nth = 0, timeout, privacy) {
800
839
  const deadline = Date.now() + (timeout ?? DEFAULT_TIMEOUT);
801
840
  const sensitiveBefore = privacy?.sensitive ?? false;
@@ -818,12 +857,35 @@ async function selectText(unifiedSession, findText, scopeTarget, nth = 0, timeou
818
857
  }
819
858
  // Step 1: 注入脚本定位文本
820
859
  //noinspection SpellCheckingInspection -- 模板中的坐标字段名
821
- const result = await unifiedSession.evaluate(`function(findText, nth, scopeNth, scopeSelector, scopeText, scopeXpath, scopeSemantic) {
860
+ const result = await unifiedSession.evaluate(`function(findText, nth, scopeNth, scopeSelector, scopeText, scopeXpath,
861
+ scopeSemantic, scopePoint, selectionId) {
822
862
  (${installDomHelpers.toString()})();
823
863
  var helpers = globalThis.__mcpDomHelpers;
824
- // 确定搜索根节点
825
- var root = document.body;
826
- if (scopeXpath || scopeSelector || scopeText || scopeSemantic) {
864
+ function rememberScope(element, selectionRange) {
865
+ var refs = window.__mcpElementMap || (window.__mcpElementMap = {});
866
+ var reverse = window.__mcpElementToRefId || (window.__mcpElementToRefId = new WeakMap());
867
+ var refId = reverse.get(element) || selectionId;
868
+ reverse.set(element, refId);
869
+ var reference = new WeakRef(element);
870
+ if (typeof element.selectionStart === 'number' && typeof element.selectionEnd === 'number') {
871
+ reference.selection = {
872
+ startOffset: element.selectionStart,
873
+ endOffset: element.selectionEnd
874
+ };
875
+ } else if (selectionRange) {
876
+ reference.selection = {
877
+ startContainer: new WeakRef(selectionRange.startContainer),
878
+ endContainer: new WeakRef(selectionRange.endContainer),
879
+ startOffset: selectionRange.startOffset,
880
+ endOffset: selectionRange.endOffset
881
+ };
882
+ }
883
+ refs[refId] = reference;
884
+ return refId;
885
+ }
886
+ // 坐标目标与选择器目标分别限定搜索范围
887
+ var root = scopePoint ? helpers.deepElementFromPoint(scopePoint.x, scopePoint.y) : document.body;
888
+ if (!scopePoint && (scopeXpath || scopeSelector || scopeText || scopeSemantic)) {
827
889
  root = helpers.query({
828
890
  selector:scopeSelector, text:scopeText, xpath:scopeXpath, semantic:scopeSemantic
829
891
  })[scopeNth];
@@ -853,8 +915,12 @@ async function selectText(unifiedSession, findText, scopeTarget, nth = 0, timeou
853
915
  }
854
916
  // 原子化:定位到 input 同时完成 focus + setSelectionRange,避免外层 mouseClick 聚焦不可靠
855
917
  root.focus();
918
+ if (root.isConnected === false || helpers.activeElement() !== root) return {type: 'noscope'};
856
919
  root.setSelectionRange(pos, pos + findText.length);
857
- return {type: 'input', inputType: inputType, selectionStart: pos, selectionEnd: pos + findText.length};
920
+ return {
921
+ type: 'input', inputType: inputType, scopeRefId: rememberScope(root),
922
+ selectionStart: pos, selectionEnd: pos + findText.length
923
+ };
858
924
  }
859
925
 
860
926
  // 在子树中查找 input/textarea
@@ -881,8 +947,12 @@ async function selectText(unifiedSession, findText, scopeTarget, nth = 0, timeou
881
947
  }
882
948
  if (ip !== -1) {
883
949
  inp.focus();
950
+ if (inp.isConnected === false || helpers.activeElement() !== inp) return {type: 'noscope'};
884
951
  inp.setSelectionRange(ip, ip + findText.length);
885
- return {type: 'input', inputType: childType, selectionStart: ip, selectionEnd: ip + findText.length};
952
+ return {
953
+ type: 'input', inputType: childType, scopeRefId: rememberScope(inp),
954
+ selectionStart: ip, selectionEnd: ip + findText.length
955
+ };
886
956
  }
887
957
  }
888
958
 
@@ -944,28 +1014,39 @@ async function selectText(unifiedSession, findText, scopeTarget, nth = 0, timeou
944
1014
  formatted = startNode.tagName.toLowerCase();
945
1015
  }
946
1016
 
1017
+ var selectionRange = document.createRange();
1018
+ selectionRange.setStart(s.node, s.offset);
1019
+ selectionRange.setEnd(e.node, Math.min(e.offset + 1, e.node.textContent.length));
947
1020
  // contenteditable 使用 DOM Range 建立精确选区;CDP 的 Shift+Click 会把整个编辑区选中。
948
1021
  if (root instanceof HTMLElement && root.isContentEditable) {
949
1022
  var selection = window.getSelection();
950
1023
  if (!selection) return {type: 'notfound'};
951
- var selectionRange = document.createRange();
952
- selectionRange.setStart(s.node, s.offset);
953
- selectionRange.setEnd(e.node, Math.min(e.offset + 1, e.node.textContent.length));
954
1024
  root.focus();
955
1025
  selection.removeAllRanges();
956
1026
  selection.addRange(selectionRange);
957
- return {type: 'range', formatted: formatted || undefined};
1027
+ return {type: 'range', scopeRefId: rememberScope(root, selectionRange), formatted: formatted || undefined};
958
1028
  }
959
1029
 
960
1030
  return {
961
1031
  type: 'coords',
1032
+ scopeRefId: rememberScope(root, selectionRange),
962
1033
  startX: sr.x + 1,
963
1034
  startY: sr.y + sr.height / 2,
964
1035
  endX: er.x + er.width - 1,
965
1036
  endY: er.y + er.height / 2,
966
1037
  formatted: formatted || undefined
967
1038
  };
968
- }`, undefined, remainingInputTimeout(deadline), [findText, nth, scopeNth, scopeSelector, scopeText, scopeXpath, scopeSemantic], { userAction: 'input' });
1039
+ }`, undefined, remainingInputTimeout(deadline), [
1040
+ findText,
1041
+ nth,
1042
+ scopeNth,
1043
+ scopeSelector,
1044
+ scopeText,
1045
+ scopeXpath,
1046
+ scopeSemantic,
1047
+ scopeTarget && 'x' in scopeTarget && 'y' in scopeTarget ? scopeTarget : null,
1048
+ `selection-${randomUUID()}`,
1049
+ ], { userAction: 'input' });
969
1050
  if (privacy &&
970
1051
  result &&
971
1052
  result.type !== 'noscope' &&
@@ -991,12 +1072,15 @@ async function selectText(unifiedSession, findText, scopeTarget, nth = 0, timeou
991
1072
  ? `目标元素内未找到文本 "${findText}"${nth > 0 ? `(第 ${nth} 个匹配)` : ''}`
992
1073
  : `未找到文本: "${findText}"${nth > 0 ? `(第 ${nth} 个匹配)` : ''}`, '请检查 find 文本、nth 序号和 target 范围;context.candidates 提供了当前范围内的候选文本和 selector', diagnostics);
993
1074
  }
1075
+ if (result.type === 'input' || result.type === 'range') {
1076
+ unifiedSession.acknowledgeInputAction();
1077
+ }
994
1078
  if (result.type === 'input') {
995
- // 注入脚本已完成 focus + setSelectionRange(原子化,避免外层 mouseClick 聚焦不可靠)
996
- return undefined;
1079
+ // 注入脚本已完成 focus + setSelectionRange,并保留后续写入所需的目标身份
1080
+ return { scopeRefId: result.scopeRefId };
997
1081
  }
998
1082
  if (result.type === 'range') {
999
- return result.formatted;
1083
+ return { scopeRefId: result.scopeRefId, formatted: result.formatted };
1000
1084
  }
1001
1085
  // DOM 文本节点:鼠标选择
1002
1086
  const coords = result;
@@ -1021,7 +1105,7 @@ async function selectText(unifiedSession, findText, scopeTarget, nth = 0, timeou
1021
1105
  throw error;
1022
1106
  }
1023
1107
  await unifiedSession.keyUp('Shift');
1024
- return coords.formatted;
1108
+ return { scopeRefId: coords.scopeRefId, formatted: coords.formatted };
1025
1109
  }
1026
1110
  /**
1027
1111
  * 验证事件参数(两种执行模式共享),避免在 Extension/CDP 两个 switch 中重复校验
@@ -1228,7 +1312,7 @@ async function handleUnifiedWheel({ unifiedSession, timeout, frame }, event) {
1228
1312
  else if (event.target) {
1229
1313
  const { selector, text, xpath, nth: nthParam, semantic, } = targetToFindParams(event.target);
1230
1314
  const nth = nthParam ?? 0;
1231
- const elements = await unifiedSession.find(selector, text, xpath, remainingInputTimeout(deadline), semantic, nth);
1315
+ const elements = await unifiedSession.find(selector, text, xpath, remainingInputTimeout(deadline), semantic, nth, 'frame');
1232
1316
  if (elements.length > 0) {
1233
1317
  remainingInputTimeout(deadline);
1234
1318
  await unifiedSession.scroll(event.deltaX ?? 0, event.deltaY ?? 0, elements[0].refId);
@@ -1312,7 +1396,7 @@ async function handleUnifiedUpload({ unifiedSession, session, mode, timeout, sig
1312
1396
  }
1313
1397
  else {
1314
1398
  const params = targetToFindParams(event.target);
1315
- const elements = await unifiedSession.find(params.selector, params.text, params.xpath, active(), params.semantic, params.nth ?? 0);
1399
+ const elements = await unifiedSession.find(params.selector, params.text, params.xpath, active(), params.semantic, params.nth ?? 0, 'frame');
1316
1400
  if (!elements.length) {
1317
1401
  // noinspection ExceptionCaughtLocallyJS
1318
1402
  throw new StructuredToolError('TARGET_NOT_FOUND', '未找到上传控件', '请检查 target、frame 和 target.nth', {});
@@ -1371,13 +1455,7 @@ async function handleUnifiedType(context, event) {
1371
1455
  return undefined;
1372
1456
  }
1373
1457
  const deadline = Date.now() + (timeout ?? DEFAULT_TIMEOUT);
1374
- if (event.target) {
1375
- await handleUnifiedClick({ ...context, timeout: remainingInputTimeout(deadline) }, { type: 'click', target: event.target });
1376
- if (!('x' in event.target)) {
1377
- await focusTargetForCommands(unifiedSession, event.target, remainingInputTimeout(deadline), frame);
1378
- }
1379
- }
1380
- else {
1458
+ if (!event.target) {
1381
1459
  const hasActiveFocus = await unifiedSession.evaluate(`(() => {
1382
1460
  (${installDomHelpers.toString()})();
1383
1461
  const active = globalThis.__mcpDomHelpers.activeElement();
@@ -1387,6 +1465,13 @@ async function handleUnifiedType(context, event) {
1387
1465
  throw new Error('type 事件在无 target 时需要页面已有焦点元素,请提供 target 或先 click 目标元素');
1388
1466
  }
1389
1467
  }
1468
+ const warning = await unifiedSession.prepareKeyboardFocus(remainingInputTimeout(deadline));
1469
+ if (event.target) {
1470
+ await handleUnifiedClick({ ...context, timeout: remainingInputTimeout(deadline) }, { type: 'click', target: event.target });
1471
+ if (!('x' in event.target)) {
1472
+ await focusTargetForCommands(unifiedSession, event.target, remainingInputTimeout(deadline), frame);
1473
+ }
1474
+ }
1390
1475
  const delay = event.delay ?? 0;
1391
1476
  if (humanize) {
1392
1477
  for (const char of event.text.replace(/\r\n?/g, '\n')) {
@@ -1399,7 +1484,7 @@ async function handleUnifiedType(context, event) {
1399
1484
  else {
1400
1485
  await unifiedSession.typeText(event.text, delay, remainingInputTimeout(deadline));
1401
1486
  }
1402
- return undefined;
1487
+ return warning;
1403
1488
  }
1404
1489
  function ensureWaitFitsTimeout(ms, timeout) {
1405
1490
  if (timeout !== undefined && ms > timeout) {
@@ -1450,14 +1535,20 @@ async function handleUnifiedReplace({ unifiedSession, session, mode, timeout, fr
1450
1535
  privacy.sensitive = true;
1451
1536
  }
1452
1537
  await focusTargetIfNeeded(unifiedSession, session, mode, event.target, remainingInputTimeout(deadline), frame);
1453
- const directResult = await unifiedSession.evaluate(`function(findText, replacementText, nth) {
1538
+ const directResult = await unifiedSession.evaluate(`function(findText, replacementText, nth, point, scopeQuery) {
1454
1539
  (${installDomHelpers.toString()})();
1455
- var el = globalThis.__mcpDomHelpers.activeElement();
1540
+ var helpers = globalThis.__mcpDomHelpers;
1541
+ var el = helpers.activeElement();
1456
1542
  if (!(el instanceof HTMLInputElement)) return {handled: false};
1457
1543
  var type = (el.type || 'text').toLowerCase();
1458
1544
  if (['text', 'search', 'tel', 'url', 'password'].indexOf(type) !== -1) {
1459
1545
  return {handled: false, inputType: type};
1460
1546
  }
1547
+ var scope = point ? helpers.deepElementFromPoint(point.x, point.y) :
1548
+ scopeQuery ? helpers.query(scopeQuery)[scopeQuery.nth || 0] : el;
1549
+ if (!scope || scope.isConnected === false || !helpers.containsComposed(scope, el)) {
1550
+ return {handled: true, success: false, tag: 'input', inputType: type, reason: 'target_changed'};
1551
+ }
1461
1552
  var current = String(el.value || '');
1462
1553
  var pos = -1;
1463
1554
  for (var i = 0; i <= nth; i++) {
@@ -1510,12 +1601,21 @@ async function handleUnifiedReplace({ unifiedSession, session, mode, timeout, fr
1510
1601
  actualValue: actual.slice(0, 160),
1511
1602
  reason: actual === requested ? undefined : 'value_not_applied'
1512
1603
  };
1513
- }`, undefined, remainingInputTimeout(deadline), [event.find, event.text, event.nth ?? 0], { userAction: 'input' });
1604
+ }`, undefined, remainingInputTimeout(deadline), [
1605
+ event.find,
1606
+ event.text,
1607
+ event.nth ?? 0,
1608
+ event.target && 'x' in event.target && 'y' in event.target ? event.target : null,
1609
+ event.target && !('x' in event.target) ? targetToFindParams(event.target) : null,
1610
+ ], { userAction: 'input' });
1514
1611
  if (privacy) {
1515
1612
  privacy.sensitive = sensitiveBefore || directResult.inputType === 'password';
1516
1613
  }
1517
1614
  if (directResult.handled) {
1518
1615
  if (!directResult.success) {
1616
+ if (directResult.reason === 'target_changed') {
1617
+ throw new StructuredToolError('TARGET_FOCUS_FAILED', '目标元素的聚焦已变化,已停止替换', '读取当前目标内容后重新定位元素', { target: event.target, reason: directResult.reason });
1618
+ }
1519
1619
  const valueNotApplied = directResult.reason === 'value_not_applied';
1520
1620
  throw new StructuredToolError(directResult.reason === 'text_not_found'
1521
1621
  ? 'TEXT_NOT_FOUND'
@@ -1546,18 +1646,13 @@ async function handleUnifiedReplace({ unifiedSession, session, mode, timeout, fr
1546
1646
  normalized: directResult.actualValue !== directResult.requestedValue,
1547
1647
  };
1548
1648
  }
1549
- const formatted = await selectText(unifiedSession, event.find, event.target, event.nth, remainingInputTimeout(deadline), privacy);
1649
+ const selection = await selectText(unifiedSession, event.find, event.target, event.nth, remainingInputTimeout(deadline), privacy);
1550
1650
  let selectionConfirmed = false;
1551
1651
  for (let i = 0; i < 25; i++) {
1552
- const hasSelection = await unifiedSession.evaluate(`(function() {
1652
+ const hasSelection = await unifiedSession.evaluate(`function(scopeRefId, findText) {
1553
1653
  (${installDomHelpers.toString()})();
1554
- var el = globalThis.__mcpDomHelpers.activeElement();
1555
- if (el && (el.tagName === 'INPUT' || el.tagName === 'TEXTAREA')) {
1556
- return el.selectionStart !== el.selectionEnd;
1557
- }
1558
- var sel = window.getSelection();
1559
- return sel && !sel.isCollapsed;
1560
- })()`, undefined, remainingInputTimeout(deadline));
1654
+ return (${ownsTextSelection.toString()})(scopeRefId, findText);
1655
+ }`, undefined, remainingInputTimeout(deadline), [selection.scopeRefId, event.find]);
1561
1656
  if (hasSelection) {
1562
1657
  selectionConfirmed = true;
1563
1658
  break;
@@ -1567,8 +1662,9 @@ async function handleUnifiedReplace({ unifiedSession, session, mode, timeout, fr
1567
1662
  if (!selectionConfirmed) {
1568
1663
  throw new Error(`选区同步失败:文本 "${event.find}" 已定位但未能建立选区,无法执行替换`);
1569
1664
  }
1570
- const replaceResult = await unifiedSession.evaluate(`function(replacementText) {
1665
+ const replaceResult = await unifiedSession.evaluate(`function(replacementText, scopeRefId, findText) {
1571
1666
  (${installDomHelpers.toString()})();
1667
+ if (!(${ownsTextSelection.toString()})(scopeRefId, findText)) return 'stale';
1572
1668
  var el = globalThis.__mcpDomHelpers.activeElement();
1573
1669
  if (el && (el.tagName === 'INPUT' || el.tagName === 'TEXTAREA') && el.setRangeText) {
1574
1670
  if (el.readOnly || el.disabled) return 'readonly';
@@ -1586,17 +1682,31 @@ async function handleUnifiedReplace({ unifiedSession, session, mode, timeout, fr
1586
1682
  if (!editable || !editable.isContentEditable) return 'readonly';
1587
1683
  if (document.execCommand('insertText', false, replacementText)) return 'ok';
1588
1684
  return 'fallback';
1589
- }`, undefined, remainingInputTimeout(deadline), [event.text], { userAction: 'input' });
1685
+ }`, undefined, remainingInputTimeout(deadline), [event.text, selection.scopeRefId, event.find], { userAction: 'input' });
1686
+ if (replaceResult === 'stale') {
1687
+ throw new StructuredToolError('TEXT_SELECTION_CHANGED', '替换前目标元素或选区已变化,已停止写入', '读取当前目标内容后重新定位文本', { target: event.target });
1688
+ }
1590
1689
  if (replaceResult === 'readonly') {
1591
1690
  throw new Error(`目标元素不可编辑,已选中文本 "${event.find}" 但无法替换`);
1592
1691
  }
1692
+ let warning;
1593
1693
  if (replaceResult === 'fallback') {
1694
+ warning = await unifiedSession.prepareKeyboardFocus(remainingInputTimeout(deadline));
1695
+ const owned = await unifiedSession.evaluate(`function(scopeRefId, findText) {
1696
+ (${installDomHelpers.toString()})();
1697
+ return (${ownsTextSelection.toString()})(scopeRefId, findText);
1698
+ }`, undefined, remainingInputTimeout(deadline), [selection.scopeRefId, event.find]);
1699
+ if (!owned) {
1700
+ throw new StructuredToolError('TEXT_SELECTION_CHANGED', '继续输入前目标元素或选区已变化,已停止写入', '读取当前目标内容后重新定位文本', { target: event.target });
1701
+ }
1594
1702
  await unifiedSession.typeText(event.text, 0, remainingInputTimeout(deadline));
1595
1703
  }
1596
- if (formatted) {
1597
- return `替换的文本原在 <${formatted}> 标签内,替换后格式可能丢失`;
1704
+ if (selection.formatted) {
1705
+ return [warning, `替换的文本原在 <${selection.formatted}> 标签内,替换后格式可能丢失`]
1706
+ .filter(Boolean)
1707
+ .join('\n');
1598
1708
  }
1599
- return undefined;
1709
+ return warning;
1600
1710
  }
1601
1711
  async function handleUnifiedEditorContext({ unifiedSession, timeout, frame }, event) {
1602
1712
  return (await editorAction(unifiedSession, 'context', event, timeout, frame));
@@ -1627,8 +1737,8 @@ async function handleUnifiedDrag({ unifiedSession, timeout, frame }, event) {
1627
1737
  const srcNth = srcParams.nth ?? 0;
1628
1738
  const dstNth = dstParams.nth ?? 0;
1629
1739
  const attemptDrag = async () => {
1630
- const srcEls = await unifiedSession.find(srcParams.selector, srcParams.text, srcParams.xpath, timeout, srcParams.semantic, srcNth);
1631
- const dstEls = await unifiedSession.find(dstParams.selector, dstParams.text, dstParams.xpath, timeout, dstParams.semantic, dstNth);
1740
+ const srcEls = await unifiedSession.find(srcParams.selector, srcParams.text, srcParams.xpath, timeout, srcParams.semantic, srcNth, 'frame');
1741
+ const dstEls = await unifiedSession.find(dstParams.selector, dstParams.text, dstParams.xpath, timeout, dstParams.semantic, dstNth, 'frame');
1632
1742
  if (srcEls.length === 0) {
1633
1743
  throw new StructuredToolError(srcNth === 0 ? 'TARGET_NOT_FOUND' : 'TARGET_INDEX_OUT_OF_RANGE', `drag 源元素未找到: ${JSON.stringify(event.target)}`, '请检查 drag 源 target、frame 和 nth', await buildTargetDiagnosticContext(unifiedSession, event.target, null, srcNth, timeout, 'drag 源元素未找到', frame));
1634
1744
  }
@@ -1650,15 +1760,7 @@ async function handleUnifiedDrag({ unifiedSession, timeout, frame }, event) {
1650
1760
  return retried ? 'drag 因 refId 失效已自动重试一次(可能是 React 等框架重渲染导致)' : undefined;
1651
1761
  }
1652
1762
  /**
1653
- * Extension 模式:获取目标点坐标
1654
- *
1655
- * iframe 坐标系修正:
1656
- * - 原始坐标 {x, y}:用户意图为 iframe 相对坐标
1657
- * - precise 模式需加 offset 转为视口绝对坐标(CDP 用绝对坐标)
1658
- * - stealth 模式直接使用(在 iframe 内派发,本身就是相对坐标)
1659
- * - 元素定位:find() 返回视口绝对坐标
1660
- * - precise 模式直接使用
1661
- * - stealth 模式需减 offset 转为 iframe 相对坐标
1763
+ * 通过当前 frame 内的元素引用写入 controlled 文本
1662
1764
  */
1663
1765
  async function inputControlled(unifiedSession, event, timeout, frame) {
1664
1766
  if (!event.target) {
@@ -1669,7 +1771,7 @@ async function inputControlled(unifiedSession, event, timeout, frame) {
1669
1771
  }
1670
1772
  const { selector, text: searchText, xpath, nth: nthParam, semantic, } = targetToFindParams(event.target);
1671
1773
  const nth = nthParam ?? 0;
1672
- const elements = await unifiedSession.find(selector, searchText, xpath, timeout, semantic, nth);
1774
+ const elements = await unifiedSession.find(selector, searchText, xpath, timeout, semantic, nth, 'frame');
1673
1775
  if (elements.length === 0) {
1674
1776
  throw new StructuredToolError('TARGET_NOT_FOUND', `controlled 输入目标未找到: ${JSON.stringify(event.target)}`, '请检查 target 是否能定位到 input、textarea、select 或 contenteditable 元素;context.candidates 提供当前页面候选元素', await buildTargetDiagnosticContext(unifiedSession, event.target, null, nth, timeout, 'controlled 输入目标未找到', frame));
1675
1777
  }
@@ -1771,20 +1873,20 @@ async function getTargetPointExtension(unifiedSession, target, timeout, frame, s
1771
1873
  }
1772
1874
  const { selector, text, xpath, nth: nthParam, semantic } = targetToFindParams(target);
1773
1875
  const nth = nthParam ?? 0;
1774
- const elements = await unifiedSession.find(selector, text, xpath, remaining(), semantic, nth);
1876
+ const elements = await unifiedSession.find(selector, text, xpath, remaining(), semantic, nth, 'frame');
1775
1877
  if (elements.length === 0) {
1776
1878
  throw new StructuredToolError(nth === 0 ? 'TARGET_NOT_FOUND' : 'TARGET_INDEX_OUT_OF_RANGE', `未找到目标元素: ${JSON.stringify(target)}`, '请检查 target 是否正确,或先用 extract type=state 查看 interactiveElements 候选', await buildTargetDiagnosticContext(unifiedSession, target, null, nth, timeout, '目标元素未找到', frame));
1777
1879
  }
1778
- // 视口外时滚动后重新取 rect:与 actionableClick (left+single) 行为对齐,
1779
- // 否则非左键 / 多击的坐标路径在视口外坐标 dispatch,浏览器找不到元素,事件丢失
1880
+ // 由原生滚动处理祖先裁剪;视口外保留居中意图,可见目标使用 nearest 保持位置
1780
1881
  const refId = elements[0].refId;
1781
1882
  let rect = await unifiedSession.evaluate(`(async () => {
1782
1883
  const ref = window.__mcpElementMap?.[${JSON.stringify(refId)}];
1783
1884
  const el = ref?.deref();
1784
1885
  if (!el || !el.isConnected || el.ownerDocument !== document) return null;
1785
1886
  const r = el.getBoundingClientRect();
1786
- if (${scroll} && (r.top < 0 || r.bottom > window.innerHeight ||
1787
- r.left < 0 || r.right > window.innerWidth)) {
1887
+ if (${scroll}) {
1888
+ const alignment = r.top < 0 || r.bottom > window.innerHeight ||
1889
+ r.left < 0 || r.right > window.innerWidth ? 'center' : 'nearest';
1788
1890
  const positions = [];
1789
1891
  for (let node = el; node; node = node.assignedSlot || node.parentElement || node.getRootNode?.().host) {
1790
1892
  if (node !== document.scrollingElement) {
@@ -1792,7 +1894,7 @@ async function getTargetPointExtension(unifiedSession, target, timeout, frame, s
1792
1894
  }
1793
1895
  }
1794
1896
  positions.push({ target: document, x: window.scrollX, y: window.scrollY });
1795
- el.scrollIntoView({ block: 'center', inline: 'center', behavior: 'instant' });
1897
+ el.scrollIntoView({ block: alignment, inline: alignment, behavior: 'instant' });
1796
1898
  const changed = positions.filter(({ target, x, y }) => target === document
1797
1899
  ? x !== window.scrollX || y !== window.scrollY
1798
1900
  : x !== target.scrollLeft || y !== target.scrollTop);
@@ -1800,7 +1902,8 @@ async function getTargetPointExtension(unifiedSession, target, timeout, frame, s
1800
1902
  const channel = new MessageChannel();
1801
1903
  const scrollTargets = new Set([window, document, ...positions.flatMap(({ target }) =>
1802
1904
  [target, target.getRootNode?.() || document]),
1803
- ...globalThis.__mcpDomHelpers.elements().flatMap(node => node.shadowRoot ? [node.shadowRoot] : [])]);
1905
+ ...globalThis.__mcpDomHelpers.elements()
1906
+ .flatMap(node => node.shadowRoot ? [node.shadowRoot] : [])]);
1804
1907
  let timer, frame, visibilityChanged, scrollChanged, scrollEnded;
1805
1908
  try {
1806
1909
  await new Promise((resolve, reject) => {
@@ -1811,16 +1914,21 @@ async function getTargetPointExtension(unifiedSession, target, timeout, frame, s
1811
1914
  scrollChanged = event => {
1812
1915
  const { target } = event, last = observedScroll.get(target);
1813
1916
  if (last?.event === event && last.listenerTarget !== event.currentTarget) return;
1814
- if (!last || positions.some(position => position.target === target)) previous = undefined;
1917
+ if (!last || positions.some(position => position.target === target)) {
1918
+ previous = undefined;
1919
+ }
1815
1920
  const offset = scrollPosition(target);
1816
1921
  const offsets = last?.offsets || new Set();
1817
1922
  offsets.add(offset.join(','));
1818
- observedScroll.set(target, { event, listenerTarget: event.currentTarget, offset, offsets });
1923
+ observedScroll.set(target, {
1924
+ event, listenerTarget: event.currentTarget, offset, offsets
1925
+ });
1819
1926
  };
1820
1927
  scrollEnded = ({ target }) => {
1821
1928
  const last = observedScroll.get(target), offset = scrollPosition(target);
1822
1929
  if (last && offset.some((value, index) => value !== last.offset[index]) &&
1823
- (positions.some(position => position.target === target) || !last.offsets.has(offset.join(',')))) {
1930
+ (positions.some(position => position.target === target) ||
1931
+ !last.offsets.has(offset.join(',')))) {
1824
1932
  previous = undefined;
1825
1933
  }
1826
1934
  };
@@ -1843,6 +1951,10 @@ async function getTargetPointExtension(unifiedSession, target, timeout, frame, s
1843
1951
  };
1844
1952
  channel.port1.onmessage = ({ data }) => {
1845
1953
  if (data.turn !== turn) return;
1954
+ if (!el.isConnected || el.ownerDocument !== document) {
1955
+ resolve();
1956
+ return;
1957
+ }
1846
1958
  for (const node of globalThis.__mcpDomHelpers.elements()) {
1847
1959
  if (node.shadowRoot && !scrollTargets.has(node.shadowRoot)) {
1848
1960
  scrollTargets.add(node.shadowRoot);
@@ -1864,6 +1976,20 @@ async function getTargetPointExtension(unifiedSession, target, timeout, frame, s
1864
1976
  return;
1865
1977
  }
1866
1978
  }
1979
+ if (Date.now() >= ${deadline}) {
1980
+ reject(new Error(
1981
+ 'SCROLL_SETTLEMENT_TIMEOUT: rendering did not finish before the request deadline'
1982
+ ));
1983
+ return;
1984
+ }
1985
+ const before = positions.flatMap(({ target }) => scrollPosition(target));
1986
+ el.scrollIntoView({ block: 'nearest', inline: 'nearest', behavior: 'instant' });
1987
+ if (positions.flatMap(({ target }) => scrollPosition(target))
1988
+ .some((value, index) => value !== before[index])) {
1989
+ previous = undefined;
1990
+ schedule();
1991
+ return;
1992
+ }
1867
1993
  resolve();
1868
1994
  };
1869
1995
  visibilityChanged = schedule;
@@ -1897,7 +2023,7 @@ async function getTargetPointExtension(unifiedSession, target, timeout, frame, s
1897
2023
  throw new StructuredToolError('TARGET_NOT_FOUND', '目标元素引用已失效,未使用旧坐标派发事件', '请重新读取页面并确认 target', { target, refId, actionExecuted: false });
1898
2024
  }
1899
2025
  if (frame !== undefined && scroll) {
1900
- const refreshed = await unifiedSession.find(selector, text, xpath, remaining(), semantic, nth);
2026
+ const refreshed = await unifiedSession.find(selector, text, xpath, remaining(), semantic, nth, 'frame');
1901
2027
  if (!refreshed[0] || refreshed[0].refId !== refId) {
1902
2028
  throw new StructuredToolError('TARGET_NOT_FOUND', '滚动后目标元素已失效', '重新读取目标元素', {
1903
2029
  actionExecuted: false,
@@ -1929,7 +2055,7 @@ async function executeCdpEvent(session, event, humanize, timeout) {
1929
2055
  if (!handler) {
1930
2056
  throw new Error(`未知事件类型: ${event.type}`);
1931
2057
  }
1932
- await handler({ session, humanize, timeout }, event);
2058
+ return handler({ session, humanize, timeout }, event);
1933
2059
  }
1934
2060
  const cdpInputHandlers = {
1935
2061
  click: handleCdpClick,
@@ -1945,9 +2071,22 @@ const cdpInputHandlers = {
1945
2071
  drag: handleCdpDrag,
1946
2072
  };
1947
2073
  async function handleCdpClick({ session, humanize, timeout }, event) {
1948
- if (event.force) {
2074
+ const deadline = Date.now() + (timeout ?? DEFAULT_TIMEOUT);
2075
+ const backgroundClick = !event.force &&
2076
+ !humanize &&
2077
+ event.target !== undefined &&
2078
+ !('x' in event.target) &&
2079
+ (event.button ?? 'left') === 'left' &&
2080
+ (event.clickCount ?? 1) === 1 &&
2081
+ session.getModifiers() === 0 &&
2082
+ (await session.evaluate('document.hidden', undefined, remainingInputTimeout(deadline)));
2083
+ if (event.force || backgroundClick) {
2084
+ if (backgroundClick) {
2085
+ const point = await getTargetPoint(session, event.target, remainingInputTimeout(deadline));
2086
+ session.getBehaviorSimulator().setCurrentPosition(point);
2087
+ }
1949
2088
  const locator = session.createLocator(event.target, {
1950
- timeout: targetLookupTimeout(timeout ?? DEFAULT_TIMEOUT),
2089
+ timeout: targetLookupTimeout(remainingInputTimeout(deadline)),
1951
2090
  });
1952
2091
  await locator.evaluateOn(`function() {
1953
2092
  if (!this.isConnected) throw new Error('目标元素已移除,未执行点击');
@@ -1971,10 +2110,10 @@ async function handleCdpClick({ session, humanize, timeout }, event) {
1971
2110
  }
1972
2111
  }
1973
2112
  }`);
1974
- return;
2113
+ return backgroundClick ? '后台元素单击使用合成事件,isTrusted=false,未激活标签页' : undefined;
1975
2114
  }
1976
2115
  if (event.target) {
1977
- await moveToTarget(session, event.target, humanize, timeout);
2116
+ await moveToTarget(session, event.target, humanize, remainingInputTimeout(deadline));
1978
2117
  }
1979
2118
  const cdpButton = event.button ?? 'left';
1980
2119
  const cdpClickCount = event.clickCount ?? 1;