@xbrowser/cli 1.30.1 → 1.30.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.
@@ -21,7 +21,7 @@ import {
21
21
  saveSessionDiskMeta,
22
22
  setActivePage,
23
23
  touchSession
24
- } from "./chunk-OSIDUT4H.js";
24
+ } from "./chunk-NBW3GGZB.js";
25
25
  import "./chunk-SMYSNO4M.js";
26
26
  import "./chunk-2KPI5GKP.js";
27
27
  import "./chunk-GDKLH7ZY.js";
@@ -21,8 +21,8 @@ import {
21
21
  saveSessionDiskMeta,
22
22
  setActivePage,
23
23
  touchSession
24
- } from "./chunk-NINETOM4.js";
25
- import "./chunk-OCECE22C.js";
24
+ } from "./chunk-JO6WHY3D.js";
25
+ import "./chunk-NDBOLP5Z.js";
26
26
  import "./chunk-A7NEA4PA.js";
27
27
  import "./chunk-SMYSNO4M.js";
28
28
  import "./chunk-2KPI5GKP.js";
@@ -21,8 +21,8 @@ import {
21
21
  saveSessionDiskMeta,
22
22
  setActivePage,
23
23
  touchSession
24
- } from "./chunk-BOZTIIBT.js";
25
- import "./chunk-SPNWUUHA.js";
24
+ } from "./chunk-BM6DDHA2.js";
25
+ import "./chunk-VYGK4X33.js";
26
26
  import "./chunk-SMYSNO4M.js";
27
27
  import "./chunk-2KPI5GKP.js";
28
28
  import "./chunk-GDKLH7ZY.js";
@@ -14,7 +14,7 @@ import {
14
14
  scrollIntoView,
15
15
  waitForActionable,
16
16
  waitForNetworkIdle
17
- } from "./chunk-SPNWUUHA.js";
17
+ } from "./chunk-VYGK4X33.js";
18
18
  import "./chunk-SMYSNO4M.js";
19
19
  import {
20
20
  connectToCDP,
@@ -2487,6 +2487,17 @@ var RuntimeEvaluationService = class {
2487
2487
  const mainId = tree.frameTree?.frame?.id;
2488
2488
  const target = all.find((f) => f.id !== mainId && f.url.includes(urlIncludes));
2489
2489
  if (target) {
2490
+ try {
2491
+ const world = await this.options.conn.send(
2492
+ "Page.createIsolatedWorld",
2493
+ { frameId: target.id, grantUniveralAccess: true, allowUniversalAccessFromFileURLs: true },
2494
+ this.options.sessionId
2495
+ );
2496
+ if (world?.executionContextId) {
2497
+ return await evalIn(this.options.sessionId, world.executionContextId);
2498
+ }
2499
+ } catch {
2500
+ }
2490
2501
  const contexts = [];
2491
2502
  const onCtx = (raw) => {
2492
2503
  const c = raw?.context;
@@ -2548,6 +2559,11 @@ var RuntimeEvaluationService = class {
2548
2559
  }
2549
2560
  throw new Error(`no OOPIF target for "${urlIncludes}"\uFF08\u7528 'xbrowser frames' \u67E5\u770B\u53EF\u89C1\u7684 iframe target\uFF1BOOPIF \u53EA\u5728 Target.getTargets \u91CC\u51FA\u73B0\uFF09`);
2550
2561
  }
2562
+ /** 完整 frame 树(递归含嵌套——同进程 iframe 祖先链构造用) */
2563
+ async frameTreeNodes() {
2564
+ const tree = await this.options.conn.send("Page.getFrameTree", void 0, this.options.sessionId);
2565
+ return tree.frameTree;
2566
+ }
2551
2567
  /** 浏览器级 iframe target 列表(OOPIF 可见性——Page.getFrameTree 看不到它们) */
2552
2568
  async listFrameTargets() {
2553
2569
  try {
@@ -2662,6 +2678,9 @@ var RuntimeEvaluationService = class {
2662
2678
  };
2663
2679
 
2664
2680
  // src/cdp-driver/page-frame-input.ts
2681
+ function isSameProcessError(err) {
2682
+ return /同进程/.test(String(err));
2683
+ }
2665
2684
  function isSessionDeadError(err) {
2666
2685
  return /session (with given id )?not found|no session with given id/i.test(errMsg(err));
2667
2686
  }
@@ -2709,31 +2728,105 @@ var FrameInputService = class {
2709
2728
  }
2710
2729
  /** 在 OOPIF 内点击选择器命中的元素(frame 局部坐标,stealth 落点偏移继承) */
2711
2730
  async clickInFrame(urlIncludes, selector, opts = {}) {
2731
+ try {
2732
+ const rect = await this.pollFrameRect(urlIncludes, selector, opts.timeoutMs ?? 5e3);
2733
+ const x = rect.x + rect.width / 2;
2734
+ const y = rect.y + rect.height / 2;
2735
+ await this.runFrameMouse(urlIncludes, (mouse) => mouse.click(x, y, { elementWidth: rect.width, elementHeight: rect.height }));
2736
+ return { x, y };
2737
+ } catch (err) {
2738
+ if (!isSameProcessError(err)) throw err;
2739
+ return this.clickInSameProcessFrame(urlIncludes, selector, opts);
2740
+ }
2741
+ }
2742
+ /**
2743
+ * 同进程跨源 iframe 点击(站点隔离按 eTLD+1 分组——qq.com 子域 iframe 同进程
2744
+ * 但跨源,DOM 进不去、也没有独立 target;输入派发到主 session,Input 事件按
2745
+ * hit-test 落进 iframe,isTrusted=true)。坐标 = 祖先 iframe 偏移累加 + 局部 rect。
2746
+ */
2747
+ async clickInSameProcessFrame(urlIncludes, selector, opts = {}) {
2748
+ this.cache.delete(urlIncludes);
2749
+ const { ox, oy } = await this.resolveFrameViewportOffset(urlIncludes);
2712
2750
  const rect = await this.pollFrameRect(urlIncludes, selector, opts.timeoutMs ?? 5e3);
2713
- const x = rect.x + rect.width / 2;
2714
- const y = rect.y + rect.height / 2;
2715
- await this.runFrameMouse(urlIncludes, (mouse) => mouse.click(x, y, { elementWidth: rect.width, elementHeight: rect.height }));
2751
+ const x = ox + rect.x + rect.width / 2;
2752
+ const y = oy + rect.y + rect.height / 2;
2753
+ await this.deps.pageMouse.click(x, y, { elementWidth: rect.width, elementHeight: rect.height });
2716
2754
  return { x, y };
2717
2755
  }
2756
+ /**
2757
+ * 目标帧在主页面视口中的偏移:沿 getFrameTree 祖先链逐帧查询
2758
+ * 「src 匹配子帧 URL 的 iframe 元素」的视口 rect 并累加(嵌套帧每层一次求值)。
2759
+ */
2760
+ async resolveFrameViewportOffset(urlIncludes) {
2761
+ const nodes = await this.deps.frameTreeNodes();
2762
+ const norm = (u) => (u || "").replace(/\/+$/, "");
2763
+ const findPath = (node, path2) => {
2764
+ const next = [...path2, node];
2765
+ if (norm(node.frame.url).includes(norm(urlIncludes))) return next;
2766
+ for (const child of node.childFrames ?? []) {
2767
+ const hit = findPath(child, next);
2768
+ if (hit) return hit;
2769
+ }
2770
+ return null;
2771
+ };
2772
+ const path = findPath(nodes, []);
2773
+ if (!path || path.length < 2) throw new Error(`no ancestor chain for frame "${urlIncludes}"`);
2774
+ let ox = 0;
2775
+ let oy = 0;
2776
+ for (let i = 0; i < path.length - 1; i++) {
2777
+ const parent = path[i];
2778
+ const child = path[i + 1];
2779
+ const isRoot = i === 0;
2780
+ const expr = `(function(){var u=${JSON.stringify(child.frame.url)};var hit=null;var ou='';try{ou=new URL(u).origin;}catch(e){}[...document.querySelectorAll('iframe')].forEach(function(f){if(hit)return;var s=f.src||'';if(!s)return;var os='';try{os=new URL(s).origin;}catch(e){}if(s.indexOf(u)>=0||u.indexOf(s)>=0||(ou&&os&&ou===os))hit=f;});if(!hit)return null;var r=hit.getBoundingClientRect();return JSON.stringify({x:r.x,y:r.y});})()`;
2781
+ const raw = isRoot ? await this.deps.evaluateMain(expr) : await this.deps.evaluateInFrame(parent.frame.url, expr);
2782
+ if (!raw) throw new Error(`ancestor frame offset not found for "${child.frame.url.slice(0, 60)}"`);
2783
+ const off = JSON.parse(String(raw));
2784
+ ox += off.x;
2785
+ oy += off.y;
2786
+ }
2787
+ return { ox, oy };
2788
+ }
2718
2789
  /** 在 OOPIF 内填输入框:点击聚焦 → el.select() 全选 → insertText 整体替换 → 回读验证 */
2719
2790
  async fillInFrame(urlIncludes, selector, text, opts = {}) {
2720
2791
  try {
2721
2792
  return await this.fillInFrameInner(urlIncludes, selector, text, opts);
2722
2793
  } catch (err) {
2794
+ if (isSameProcessError(err)) return this.fillInSameProcessFrame(urlIncludes, selector, text, opts);
2723
2795
  if (!isSessionDeadError(err)) throw err;
2724
2796
  this.cache.delete(urlIncludes);
2725
2797
  return this.fillInFrameInner(urlIncludes, selector, text, opts);
2726
2798
  }
2727
2799
  }
2800
+ /** 同进程跨源 iframe 填写:主 session 点击聚焦 + insertText + frame 内回读 */
2801
+ async fillInSameProcessFrame(urlIncludes, selector, text, opts = {}) {
2802
+ const { ox, oy } = await this.resolveFrameViewportOffset(urlIncludes);
2803
+ const rect = await this.pollFrameRect(urlIncludes, selector, opts.timeoutMs ?? 5e3);
2804
+ const x = ox + rect.x + rect.width / 2;
2805
+ const y = oy + rect.y + rect.height / 2;
2806
+ await this.deps.pageMouse.click(x, y, { elementWidth: rect.width, elementHeight: rect.height });
2807
+ await this.deps.evaluateInFrame(
2808
+ urlIncludes,
2809
+ `(function(){var el=document.querySelector(${JSON.stringify(selector)});if(el&&typeof el.select==='function')el.select();return true;})()`
2810
+ );
2811
+ await this.deps.pageKeyboard.type(text, { delay: 18 });
2812
+ const readback = await this.deps.evaluateInFrame(
2813
+ urlIncludes,
2814
+ `(function(){var el=document.querySelector(${JSON.stringify(selector)});return el?String(el.value??''):'';})()`
2815
+ );
2816
+ return { value: readback ?? "" };
2817
+ }
2728
2818
  async fillInFrameInner(urlIncludes, selector, text, opts = {}) {
2729
2819
  await this.clickInFrame(urlIncludes, selector, opts);
2730
2820
  const cached = this.cache.get(urlIncludes);
2731
- if (!cached) throw new Error(`frame session lost for "${urlIncludes}"`);
2732
2821
  await this.deps.evaluateInFrame(
2733
2822
  urlIncludes,
2734
2823
  `(() => { const el = document.querySelector(${JSON.stringify(selector)}); if (el && typeof el.select === 'function') el.select(); return true; })()`
2735
2824
  );
2736
- await this.deps.conn.send("Input.insertText", { text }, cached.sessionId);
2825
+ if (cached) {
2826
+ await this.deps.conn.send("Input.insertText", { text }, cached.sessionId);
2827
+ } else {
2828
+ await this.deps.pageKeyboard.type(text, { delay: 18 });
2829
+ }
2737
2830
  const readback = await this.deps.evaluateInFrame(
2738
2831
  urlIncludes,
2739
2832
  `(() => { const el = document.querySelector(${JSON.stringify(selector)}); return el ? String(el.value ?? '') : ''; })()`
@@ -2850,7 +2943,11 @@ var XBPageImpl = class _XBPageImpl {
2850
2943
  this.frameInputService = new FrameInputService({
2851
2944
  conn,
2852
2945
  resolveFrameSession: (urlIncludes) => this.runtimeEvaluationService.resolveFrameSession(urlIncludes),
2853
- evaluateInFrame: (urlIncludes, expression) => this.evaluateInFrame(urlIncludes, expression)
2946
+ evaluateInFrame: (urlIncludes, expression) => this.evaluateInFrame(urlIncludes, expression),
2947
+ evaluateMain: (expression) => this.evaluate(expression),
2948
+ pageMouse: this.mouse,
2949
+ pageKeyboard: this.keyboard,
2950
+ frameTreeNodes: () => this.runtimeEvaluationService.frameTreeNodes()
2854
2951
  });
2855
2952
  this.domQueryService = new DomQueryService({
2856
2953
  conn,
@@ -14,7 +14,7 @@ import {
14
14
  scrollIntoView,
15
15
  waitForActionable,
16
16
  waitForNetworkIdle
17
- } from "./chunk-OCECE22C.js";
17
+ } from "./chunk-NDBOLP5Z.js";
18
18
  import "./chunk-A7NEA4PA.js";
19
19
  import "./chunk-SMYSNO4M.js";
20
20
  import {
@@ -2,7 +2,7 @@
2
2
  import {
3
3
  Core
4
4
  } from "@dyyz1993/xcli-core";
5
- import { resolve as resolve2, basename, dirname } from "path";
5
+ import { resolve as resolve2, basename, dirname as dirname2 } from "path";
6
6
  import { existsSync as existsSync4, readdirSync } from "fs";
7
7
  import { homedir as homedir2 } from "os";
8
8
 
@@ -301,9 +301,9 @@ function patchLoginRequired() {
301
301
  }
302
302
 
303
303
  // src/lib/mistake-ledger.ts
304
- import { existsSync as existsSync3, mkdirSync as mkdirSync2, readFileSync as readFileSync2, writeFileSync as writeFileSync3 } from "fs";
304
+ import { existsSync as existsSync3, mkdirSync as mkdirSync2, readFileSync as readFileSync2, writeFileSync as writeFileSync3, appendFileSync } from "fs";
305
305
  import { homedir } from "os";
306
- import { join as join2 } from "path";
306
+ import { join as join2, dirname } from "path";
307
307
  var MISTAKE_THRESHOLD = 3;
308
308
  var MISTAKE_LEDGER_PATH = join2(
309
309
  homedir(),
@@ -335,6 +335,56 @@ function writeMistakeLedger(ledger, path = MISTAKE_LEDGER_PATH) {
335
335
  mkdirSync2(join2(path, ".."), { recursive: true });
336
336
  writeFileSync3(path, JSON.stringify(ledger, null, 2), "utf8");
337
337
  }
338
+ var MISTAKES_MD_HEADER = `# \u6301\u4E45\u5316\u9519\u9898\u672C\uFF08 graduated mistake ledger \uFF09
339
+
340
+ > \u8FD0\u884C\u65F6\u9519\u9898\u672C\uFF08~/.xbrowser/knowledge/mistakes.json\uFF0CJSON\uFF09\u53EA\u505A\u8BA1\u6570\uFF1B
341
+ > **\u540C\u4E00\u9519\u8BEF\u6307\u7EB9\u51FA\u73B0 \u2265${MISTAKE_THRESHOLD} \u6B21\u81EA\u52A8\u6C89\u6DC0\u5230\u672C\u6587\u4EF6**\uFF08\u6309\u65E5\u671F\u8BB0\u5F55\uFF09\u3002
342
+ > \u672C\u6587\u4EF6\u88AB AGENTS.md \u5F15\u7528\u2014\u2014\u6240\u6709 Agent \u5F00\u5DE5\u524D\u5FC5\u8BFB\uFF0C\u907F\u514D\u91CD\u8E48\u8986\u8F99\u3002
343
+ > \u4FEE\u590D\u540E\u5728\u5BF9\u5E94\u6761\u76EE\u8865\u300C\u6839\u56E0\u300D\u4E0E\u300C\u4FEE\u590D\u300D\u4E24\u884C\uFF1B\u65B0\u6761\u76EE\u7531 recordMistake \u81EA\u52A8\u8FFD\u52A0\uFF0C\u4EBA\u5DE5\u53EA\u8865\u4E0D\u5220\u3002
344
+
345
+ `;
346
+ function persistentMistakesPath() {
347
+ if (process.env.XBROWSER_MISTAKES_MD) return process.env.XBROWSER_MISTAKES_MD;
348
+ let dir = process.cwd();
349
+ for (let i = 0; i < 8; i++) {
350
+ const pkg = join2(dir, "package.json");
351
+ if (existsSync3(pkg)) {
352
+ try {
353
+ const name = JSON.parse(readFileSync2(pkg, "utf8")).name;
354
+ if (name === "@xbrowser/cli") return join2(dir, "docs", "mistakes.md");
355
+ } catch {
356
+ }
357
+ }
358
+ const parent = join2(dir, "..");
359
+ if (parent === dir) break;
360
+ dir = parent;
361
+ }
362
+ return join2(homedir(), ".xbrowser", "knowledge", "mistakes.md");
363
+ }
364
+ function graduateToMarkdown(entry) {
365
+ const mdPath = persistentMistakesPath();
366
+ try {
367
+ mkdirSync2(dirname(mdPath), { recursive: true });
368
+ if (!existsSync3(mdPath)) writeFileSync3(mdPath, MISTAKES_MD_HEADER);
369
+ const day = entry.lastSeen.slice(0, 10);
370
+ const first = entry.occurrences[0];
371
+ const anchor = `<!-- fingerprint:${entry.fingerprint} -->`;
372
+ const existing = existsSync3(mdPath) ? readFileSync2(mdPath, "utf8") : "";
373
+ if (existing.includes(anchor)) return;
374
+ const block = [
375
+ `## ${day} \xB7 ${first.command} \xB7 ${first.message.split("\n")[0].slice(0, 80)}`,
376
+ anchor,
377
+ `- **\u7C7B\u522B**: ${entry.category}`,
378
+ `- **\u9996\u6B21/\u6700\u8FD1**: ${entry.firstSeen.slice(0, 19)} \u2192 ${entry.lastSeen.slice(0, 19)}\uFF08\u7D2F\u8BA1 ${entry.occurrences.length} \u6B21\uFF09`,
379
+ `- **\u5178\u578B\u9519\u8BEF**: ${first.message.split("\n")[0].slice(0, 160)}`,
380
+ `- **\u6839\u56E0**: \uFF08\u5F85\u8865\u2014\u2014\u4FEE\u590D\u65F6\u586B\u5199\uFF09`,
381
+ `- **\u4FEE\u590D**: \uFF08\u5F85\u8865\u2014\u2014\u586B PR \u53F7\u6216\u4FEE\u590D\u8BF4\u660E\uFF09`,
382
+ ""
383
+ ].join("\n");
384
+ appendFileSync(mdPath, block);
385
+ } catch {
386
+ }
387
+ }
338
388
  function recordMistake(input, path = MISTAKE_LEDGER_PATH) {
339
389
  if (!input.category) throw new Error("mistake category is required");
340
390
  if (!input.command) throw new Error("mistake command is required");
@@ -363,10 +413,9 @@ function recordMistake(input, path = MISTAKE_LEDGER_PATH) {
363
413
  };
364
414
  ledger.entries[fingerprint] = entry;
365
415
  writeMistakeLedger(ledger, path);
366
- return {
367
- entry,
368
- newlyConstrained: Boolean(previous && !previous.constrained && entry.constrained)
369
- };
416
+ const newlyConstrained = Boolean(previous && !previous.constrained && entry.constrained);
417
+ if (newlyConstrained) graduateToMarkdown(entry);
418
+ return { entry, newlyConstrained };
370
419
  }
371
420
 
372
421
  // src/plugin/loader.ts
@@ -446,7 +495,7 @@ var XBrowserPluginLoader = class {
446
495
  async loadPlugin(pluginPath, id) {
447
496
  const before = new Set(this.core.loader.getSites().map((s) => s.name));
448
497
  const instance = await this.loader.loadPlugin(pluginPath, id);
449
- this.recordSiteAliases(basename(dirname(pluginPath)), before);
498
+ this.recordSiteAliases(basename(dirname2(pluginPath)), before);
450
499
  return instance;
451
500
  }
452
501
  async unloadPlugin(id) {
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  launch
3
- } from "./chunk-SPNWUUHA.js";
3
+ } from "./chunk-VYGK4X33.js";
4
4
  import {
5
5
  errMsg
6
6
  } from "./chunk-GDKLH7ZY.js";
@@ -20,7 +20,7 @@ import {
20
20
  resolveLaunchOpts,
21
21
  saveSessionDiskMeta,
22
22
  setActivePage
23
- } from "./chunk-OSIDUT4H.js";
23
+ } from "./chunk-NBW3GGZB.js";
24
24
  import {
25
25
  rand,
26
26
  sleep,
@@ -1368,11 +1368,11 @@ var preflightCommand = registerCommand({
1368
1368
  let pageUrl = null;
1369
1369
  try {
1370
1370
  const { fileURLToPath } = await import("url");
1371
- const { join: join10, dirname: dirname4 } = await import("path");
1371
+ const { join: join10, dirname: dirname5 } = await import("path");
1372
1372
  const { existsSync: existsSync8 } = await import("fs");
1373
1373
  const here = typeof __filename !== "undefined" ? __filename : fileURLToPath(import.meta.url);
1374
1374
  const candidates = [
1375
- join10(dirname4(here), "..", "assets", "preflight.html"),
1375
+ join10(dirname5(here), "..", "assets", "preflight.html"),
1376
1376
  join10(process.cwd(), "assets", "preflight.html")
1377
1377
  ];
1378
1378
  pageUrl = candidates.find((c) => existsSync8(c)) || null;
@@ -7971,9 +7971,9 @@ function classifyFailure(message) {
7971
7971
  }
7972
7972
 
7973
7973
  // src/lib/mistake-ledger.ts
7974
- import { existsSync as existsSync4, mkdirSync as mkdirSync4, readFileSync as readFileSync5, writeFileSync as writeFileSync4 } from "fs";
7974
+ import { existsSync as existsSync4, mkdirSync as mkdirSync4, readFileSync as readFileSync5, writeFileSync as writeFileSync4, appendFileSync } from "fs";
7975
7975
  import { homedir as homedir5 } from "os";
7976
- import { join as join7 } from "path";
7976
+ import { join as join7, dirname as dirname3 } from "path";
7977
7977
  var MISTAKE_THRESHOLD = 3;
7978
7978
  var MISTAKE_LEDGER_PATH = join7(
7979
7979
  homedir5(),
@@ -8005,6 +8005,56 @@ function writeMistakeLedger(ledger, path2 = MISTAKE_LEDGER_PATH) {
8005
8005
  mkdirSync4(join7(path2, ".."), { recursive: true });
8006
8006
  writeFileSync4(path2, JSON.stringify(ledger, null, 2), "utf8");
8007
8007
  }
8008
+ var MISTAKES_MD_HEADER = `# \u6301\u4E45\u5316\u9519\u9898\u672C\uFF08 graduated mistake ledger \uFF09
8009
+
8010
+ > \u8FD0\u884C\u65F6\u9519\u9898\u672C\uFF08~/.xbrowser/knowledge/mistakes.json\uFF0CJSON\uFF09\u53EA\u505A\u8BA1\u6570\uFF1B
8011
+ > **\u540C\u4E00\u9519\u8BEF\u6307\u7EB9\u51FA\u73B0 \u2265${MISTAKE_THRESHOLD} \u6B21\u81EA\u52A8\u6C89\u6DC0\u5230\u672C\u6587\u4EF6**\uFF08\u6309\u65E5\u671F\u8BB0\u5F55\uFF09\u3002
8012
+ > \u672C\u6587\u4EF6\u88AB AGENTS.md \u5F15\u7528\u2014\u2014\u6240\u6709 Agent \u5F00\u5DE5\u524D\u5FC5\u8BFB\uFF0C\u907F\u514D\u91CD\u8E48\u8986\u8F99\u3002
8013
+ > \u4FEE\u590D\u540E\u5728\u5BF9\u5E94\u6761\u76EE\u8865\u300C\u6839\u56E0\u300D\u4E0E\u300C\u4FEE\u590D\u300D\u4E24\u884C\uFF1B\u65B0\u6761\u76EE\u7531 recordMistake \u81EA\u52A8\u8FFD\u52A0\uFF0C\u4EBA\u5DE5\u53EA\u8865\u4E0D\u5220\u3002
8014
+
8015
+ `;
8016
+ function persistentMistakesPath() {
8017
+ if (process.env.XBROWSER_MISTAKES_MD) return process.env.XBROWSER_MISTAKES_MD;
8018
+ let dir = process.cwd();
8019
+ for (let i = 0; i < 8; i++) {
8020
+ const pkg = join7(dir, "package.json");
8021
+ if (existsSync4(pkg)) {
8022
+ try {
8023
+ const name = JSON.parse(readFileSync5(pkg, "utf8")).name;
8024
+ if (name === "@xbrowser/cli") return join7(dir, "docs", "mistakes.md");
8025
+ } catch {
8026
+ }
8027
+ }
8028
+ const parent = join7(dir, "..");
8029
+ if (parent === dir) break;
8030
+ dir = parent;
8031
+ }
8032
+ return join7(homedir5(), ".xbrowser", "knowledge", "mistakes.md");
8033
+ }
8034
+ function graduateToMarkdown(entry) {
8035
+ const mdPath = persistentMistakesPath();
8036
+ try {
8037
+ mkdirSync4(dirname3(mdPath), { recursive: true });
8038
+ if (!existsSync4(mdPath)) writeFileSync4(mdPath, MISTAKES_MD_HEADER);
8039
+ const day = entry.lastSeen.slice(0, 10);
8040
+ const first = entry.occurrences[0];
8041
+ const anchor = `<!-- fingerprint:${entry.fingerprint} -->`;
8042
+ const existing = existsSync4(mdPath) ? readFileSync5(mdPath, "utf8") : "";
8043
+ if (existing.includes(anchor)) return;
8044
+ const block = [
8045
+ `## ${day} \xB7 ${first.command} \xB7 ${first.message.split("\n")[0].slice(0, 80)}`,
8046
+ anchor,
8047
+ `- **\u7C7B\u522B**: ${entry.category}`,
8048
+ `- **\u9996\u6B21/\u6700\u8FD1**: ${entry.firstSeen.slice(0, 19)} \u2192 ${entry.lastSeen.slice(0, 19)}\uFF08\u7D2F\u8BA1 ${entry.occurrences.length} \u6B21\uFF09`,
8049
+ `- **\u5178\u578B\u9519\u8BEF**: ${first.message.split("\n")[0].slice(0, 160)}`,
8050
+ `- **\u6839\u56E0**: \uFF08\u5F85\u8865\u2014\u2014\u4FEE\u590D\u65F6\u586B\u5199\uFF09`,
8051
+ `- **\u4FEE\u590D**: \uFF08\u5F85\u8865\u2014\u2014\u586B PR \u53F7\u6216\u4FEE\u590D\u8BF4\u660E\uFF09`,
8052
+ ""
8053
+ ].join("\n");
8054
+ appendFileSync(mdPath, block);
8055
+ } catch {
8056
+ }
8057
+ }
8008
8058
  function recordMistake(input, path2 = MISTAKE_LEDGER_PATH) {
8009
8059
  if (!input.category) throw new Error("mistake category is required");
8010
8060
  if (!input.command) throw new Error("mistake command is required");
@@ -8033,10 +8083,9 @@ function recordMistake(input, path2 = MISTAKE_LEDGER_PATH) {
8033
8083
  };
8034
8084
  ledger.entries[fingerprint] = entry;
8035
8085
  writeMistakeLedger(ledger, path2);
8036
- return {
8037
- entry,
8038
- newlyConstrained: Boolean(previous && !previous.constrained && entry.constrained)
8039
- };
8086
+ const newlyConstrained = Boolean(previous && !previous.constrained && entry.constrained);
8087
+ if (newlyConstrained) graduateToMarkdown(entry);
8088
+ return { entry, newlyConstrained };
8040
8089
  }
8041
8090
 
8042
8091
  // src/executor/foundation.ts
@@ -8054,7 +8103,7 @@ import {
8054
8103
  import {
8055
8104
  Core
8056
8105
  } from "@dyyz1993/xcli-core";
8057
- import { resolve as resolve2, basename, dirname as dirname3 } from "path";
8106
+ import { resolve as resolve2, basename, dirname as dirname4 } from "path";
8058
8107
  import { existsSync as existsSync7, readdirSync } from "fs";
8059
8108
  import { homedir as homedir6 } from "os";
8060
8109
 
@@ -8429,7 +8478,7 @@ var XBrowserPluginLoader = class {
8429
8478
  async loadPlugin(pluginPath, id) {
8430
8479
  const before = new Set(this.core.loader.getSites().map((s) => s.name));
8431
8480
  const instance = await this.loader.loadPlugin(pluginPath, id);
8432
- this.recordSiteAliases(basename(dirname3(pluginPath)), before);
8481
+ this.recordSiteAliases(basename(dirname4(pluginPath)), before);
8433
8482
  return instance;
8434
8483
  }
8435
8484
  async unloadPlugin(id) {
@@ -9129,7 +9178,7 @@ async function executeCommand(commandName, params, sessionName = "default", extr
9129
9178
  }
9130
9179
  let targetPageOverride = null;
9131
9180
  if (_target && extraOpts?.cdpEndpoint) {
9132
- const { findTargetPage } = await import("./browser-I2NLJN2A.js");
9181
+ const { findTargetPage } = await import("./browser-CE4GTZRO.js");
9133
9182
  targetPageOverride = await findTargetPage(extraOpts.cdpEndpoint, _target);
9134
9183
  if (!targetPageOverride) {
9135
9184
  return errorResult(`Target "${_target}" not found. Use 'xbrowser targets --cdp ${extraOpts.cdpEndpoint}' to list available pages.`);
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  createRuleEngine,
3
3
  launch
4
- } from "./chunk-OCECE22C.js";
4
+ } from "./chunk-NDBOLP5Z.js";
5
5
  import {
6
6
  errMsg
7
7
  } from "./chunk-GDKLH7ZY.js";
@@ -2560,6 +2560,17 @@ var RuntimeEvaluationService = class {
2560
2560
  const mainId = tree.frameTree?.frame?.id;
2561
2561
  const target = all.find((f) => f.id !== mainId && f.url.includes(urlIncludes));
2562
2562
  if (target) {
2563
+ try {
2564
+ const world = await this.options.conn.send(
2565
+ "Page.createIsolatedWorld",
2566
+ { frameId: target.id, grantUniveralAccess: true, allowUniversalAccessFromFileURLs: true },
2567
+ this.options.sessionId
2568
+ );
2569
+ if (world?.executionContextId) {
2570
+ return await evalIn(this.options.sessionId, world.executionContextId);
2571
+ }
2572
+ } catch {
2573
+ }
2563
2574
  const contexts = [];
2564
2575
  const onCtx = (raw) => {
2565
2576
  const c = raw?.context;
@@ -2621,6 +2632,11 @@ var RuntimeEvaluationService = class {
2621
2632
  }
2622
2633
  throw new Error(`no OOPIF target for "${urlIncludes}"\uFF08\u7528 'xbrowser frames' \u67E5\u770B\u53EF\u89C1\u7684 iframe target\uFF1BOOPIF \u53EA\u5728 Target.getTargets \u91CC\u51FA\u73B0\uFF09`);
2623
2634
  }
2635
+ /** 完整 frame 树(递归含嵌套——同进程 iframe 祖先链构造用) */
2636
+ async frameTreeNodes() {
2637
+ const tree = await this.options.conn.send("Page.getFrameTree", void 0, this.options.sessionId);
2638
+ return tree.frameTree;
2639
+ }
2624
2640
  /** 浏览器级 iframe target 列表(OOPIF 可见性——Page.getFrameTree 看不到它们) */
2625
2641
  async listFrameTargets() {
2626
2642
  try {
@@ -2735,6 +2751,9 @@ var RuntimeEvaluationService = class {
2735
2751
  };
2736
2752
 
2737
2753
  // src/cdp-driver/page-frame-input.ts
2754
+ function isSameProcessError(err) {
2755
+ return /同进程/.test(String(err));
2756
+ }
2738
2757
  function isSessionDeadError(err) {
2739
2758
  return /session (with given id )?not found|no session with given id/i.test(errMsg(err));
2740
2759
  }
@@ -2782,31 +2801,105 @@ var FrameInputService = class {
2782
2801
  }
2783
2802
  /** 在 OOPIF 内点击选择器命中的元素(frame 局部坐标,stealth 落点偏移继承) */
2784
2803
  async clickInFrame(urlIncludes, selector, opts = {}) {
2804
+ try {
2805
+ const rect = await this.pollFrameRect(urlIncludes, selector, opts.timeoutMs ?? 5e3);
2806
+ const x = rect.x + rect.width / 2;
2807
+ const y = rect.y + rect.height / 2;
2808
+ await this.runFrameMouse(urlIncludes, (mouse) => mouse.click(x, y, { elementWidth: rect.width, elementHeight: rect.height }));
2809
+ return { x, y };
2810
+ } catch (err) {
2811
+ if (!isSameProcessError(err)) throw err;
2812
+ return this.clickInSameProcessFrame(urlIncludes, selector, opts);
2813
+ }
2814
+ }
2815
+ /**
2816
+ * 同进程跨源 iframe 点击(站点隔离按 eTLD+1 分组——qq.com 子域 iframe 同进程
2817
+ * 但跨源,DOM 进不去、也没有独立 target;输入派发到主 session,Input 事件按
2818
+ * hit-test 落进 iframe,isTrusted=true)。坐标 = 祖先 iframe 偏移累加 + 局部 rect。
2819
+ */
2820
+ async clickInSameProcessFrame(urlIncludes, selector, opts = {}) {
2821
+ this.cache.delete(urlIncludes);
2822
+ const { ox, oy } = await this.resolveFrameViewportOffset(urlIncludes);
2785
2823
  const rect = await this.pollFrameRect(urlIncludes, selector, opts.timeoutMs ?? 5e3);
2786
- const x = rect.x + rect.width / 2;
2787
- const y = rect.y + rect.height / 2;
2788
- await this.runFrameMouse(urlIncludes, (mouse) => mouse.click(x, y, { elementWidth: rect.width, elementHeight: rect.height }));
2824
+ const x = ox + rect.x + rect.width / 2;
2825
+ const y = oy + rect.y + rect.height / 2;
2826
+ await this.deps.pageMouse.click(x, y, { elementWidth: rect.width, elementHeight: rect.height });
2789
2827
  return { x, y };
2790
2828
  }
2829
+ /**
2830
+ * 目标帧在主页面视口中的偏移:沿 getFrameTree 祖先链逐帧查询
2831
+ * 「src 匹配子帧 URL 的 iframe 元素」的视口 rect 并累加(嵌套帧每层一次求值)。
2832
+ */
2833
+ async resolveFrameViewportOffset(urlIncludes) {
2834
+ const nodes = await this.deps.frameTreeNodes();
2835
+ const norm = (u) => (u || "").replace(/\/+$/, "");
2836
+ const findPath = (node, path2) => {
2837
+ const next = [...path2, node];
2838
+ if (norm(node.frame.url).includes(norm(urlIncludes))) return next;
2839
+ for (const child of node.childFrames ?? []) {
2840
+ const hit = findPath(child, next);
2841
+ if (hit) return hit;
2842
+ }
2843
+ return null;
2844
+ };
2845
+ const path = findPath(nodes, []);
2846
+ if (!path || path.length < 2) throw new Error(`no ancestor chain for frame "${urlIncludes}"`);
2847
+ let ox = 0;
2848
+ let oy = 0;
2849
+ for (let i = 0; i < path.length - 1; i++) {
2850
+ const parent = path[i];
2851
+ const child = path[i + 1];
2852
+ const isRoot = i === 0;
2853
+ const expr = `(function(){var u=${JSON.stringify(child.frame.url)};var hit=null;var ou='';try{ou=new URL(u).origin;}catch(e){}[...document.querySelectorAll('iframe')].forEach(function(f){if(hit)return;var s=f.src||'';if(!s)return;var os='';try{os=new URL(s).origin;}catch(e){}if(s.indexOf(u)>=0||u.indexOf(s)>=0||(ou&&os&&ou===os))hit=f;});if(!hit)return null;var r=hit.getBoundingClientRect();return JSON.stringify({x:r.x,y:r.y});})()`;
2854
+ const raw = isRoot ? await this.deps.evaluateMain(expr) : await this.deps.evaluateInFrame(parent.frame.url, expr);
2855
+ if (!raw) throw new Error(`ancestor frame offset not found for "${child.frame.url.slice(0, 60)}"`);
2856
+ const off = JSON.parse(String(raw));
2857
+ ox += off.x;
2858
+ oy += off.y;
2859
+ }
2860
+ return { ox, oy };
2861
+ }
2791
2862
  /** 在 OOPIF 内填输入框:点击聚焦 → el.select() 全选 → insertText 整体替换 → 回读验证 */
2792
2863
  async fillInFrame(urlIncludes, selector, text, opts = {}) {
2793
2864
  try {
2794
2865
  return await this.fillInFrameInner(urlIncludes, selector, text, opts);
2795
2866
  } catch (err) {
2867
+ if (isSameProcessError(err)) return this.fillInSameProcessFrame(urlIncludes, selector, text, opts);
2796
2868
  if (!isSessionDeadError(err)) throw err;
2797
2869
  this.cache.delete(urlIncludes);
2798
2870
  return this.fillInFrameInner(urlIncludes, selector, text, opts);
2799
2871
  }
2800
2872
  }
2873
+ /** 同进程跨源 iframe 填写:主 session 点击聚焦 + insertText + frame 内回读 */
2874
+ async fillInSameProcessFrame(urlIncludes, selector, text, opts = {}) {
2875
+ const { ox, oy } = await this.resolveFrameViewportOffset(urlIncludes);
2876
+ const rect = await this.pollFrameRect(urlIncludes, selector, opts.timeoutMs ?? 5e3);
2877
+ const x = ox + rect.x + rect.width / 2;
2878
+ const y = oy + rect.y + rect.height / 2;
2879
+ await this.deps.pageMouse.click(x, y, { elementWidth: rect.width, elementHeight: rect.height });
2880
+ await this.deps.evaluateInFrame(
2881
+ urlIncludes,
2882
+ `(function(){var el=document.querySelector(${JSON.stringify(selector)});if(el&&typeof el.select==='function')el.select();return true;})()`
2883
+ );
2884
+ await this.deps.pageKeyboard.type(text, { delay: 18 });
2885
+ const readback = await this.deps.evaluateInFrame(
2886
+ urlIncludes,
2887
+ `(function(){var el=document.querySelector(${JSON.stringify(selector)});return el?String(el.value??''):'';})()`
2888
+ );
2889
+ return { value: readback ?? "" };
2890
+ }
2801
2891
  async fillInFrameInner(urlIncludes, selector, text, opts = {}) {
2802
2892
  await this.clickInFrame(urlIncludes, selector, opts);
2803
2893
  const cached = this.cache.get(urlIncludes);
2804
- if (!cached) throw new Error(`frame session lost for "${urlIncludes}"`);
2805
2894
  await this.deps.evaluateInFrame(
2806
2895
  urlIncludes,
2807
2896
  `(() => { const el = document.querySelector(${JSON.stringify(selector)}); if (el && typeof el.select === 'function') el.select(); return true; })()`
2808
2897
  );
2809
- await this.deps.conn.send("Input.insertText", { text }, cached.sessionId);
2898
+ if (cached) {
2899
+ await this.deps.conn.send("Input.insertText", { text }, cached.sessionId);
2900
+ } else {
2901
+ await this.deps.pageKeyboard.type(text, { delay: 18 });
2902
+ }
2810
2903
  const readback = await this.deps.evaluateInFrame(
2811
2904
  urlIncludes,
2812
2905
  `(() => { const el = document.querySelector(${JSON.stringify(selector)}); return el ? String(el.value ?? '') : ''; })()`
@@ -2923,7 +3016,11 @@ var XBPageImpl = class _XBPageImpl {
2923
3016
  this.frameInputService = new FrameInputService({
2924
3017
  conn,
2925
3018
  resolveFrameSession: (urlIncludes) => this.runtimeEvaluationService.resolveFrameSession(urlIncludes),
2926
- evaluateInFrame: (urlIncludes, expression) => this.evaluateInFrame(urlIncludes, expression)
3019
+ evaluateInFrame: (urlIncludes, expression) => this.evaluateInFrame(urlIncludes, expression),
3020
+ evaluateMain: (expression) => this.evaluate(expression),
3021
+ pageMouse: this.mouse,
3022
+ pageKeyboard: this.keyboard,
3023
+ frameTreeNodes: () => this.runtimeEvaluationService.frameTreeNodes()
2927
3024
  });
2928
3025
  this.domQueryService = new DomQueryService({
2929
3026
  conn,