@aiyiran/myclaw 1.1.152 → 1.1.154

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aiyiran/myclaw",
3
- "version": "1.1.152",
3
+ "version": "1.1.154",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "bin": {
@@ -423,12 +423,31 @@ def _get_probe():
423
423
  return _probe_cache
424
424
 
425
425
 
426
+ def _find_probe_insert_pos(html):
427
+ """决定 probe 注入位置:越早越好,确保 probe 在所有作品脚本之前运行,
428
+ 从而 window error listener 能捕获 head/body 任何同步阶段的报错。
429
+ 逐级降级:<head> 开始标签后 → <html> 开始标签后 → <body> 开始标签后 → 文档开头。
430
+ 返回插入下标(在该下标处插入 tag)。"""
431
+ lower = html.lower()
432
+ for open_tag in ("<head", "<html", "<body"):
433
+ idx = lower.find(open_tag)
434
+ if idx != -1:
435
+ tag_end = html.find(">", idx)
436
+ if tag_end != -1:
437
+ return tag_end + 1
438
+ return 0 # 三个标签都没有:插到最前面
439
+
440
+
426
441
  def inject_probe_into_html(html_bytes):
427
442
  """对 HTML 字节注入/更新 probe,返回处理后的 bytes。
428
443
  - 已含当前版本 → 原样返回(②)
429
444
  - 含旧版本 → 移除旧 probe 再注入新版(③)
430
- - 不含 → 注入到 </body> 前;无 </body> 则追加末尾(③)
431
- probe 不可用、或内容非 UTF-8 文本时,原样返回。"""
445
+ - 不含 → 注入到尽可能靠前的位置(见 _find_probe_insert_pos)(③)
446
+ probe 不可用、或内容非 UTF-8 文本时,原样返回。
447
+
448
+ ⚠️ probe 必须在作品脚本之前加载,否则 init() 等同步阶段的报错会在
449
+ window error listener 安装之前发生,无法被捕获。所以注入到 <head>/<html>
450
+ 开始标签之后,而非 </body> 之前。"""
432
451
  version, src = _get_probe()
433
452
  if not version or not src:
434
453
  return html_bytes
@@ -449,17 +468,8 @@ def inject_probe_into_html(html_bytes):
449
468
  )
450
469
 
451
470
  tag = f'<script {PROBE_MARKER_ATTR}="{version}">\n{src}\n</script>'
452
- # 注入到 <body> 开头而非 </body> 前,确保 probe 在游戏脚本之前加载,
453
- # 能捕获 init() 等同步执行阶段的报错。
454
- body_open = html.lower().find("<body")
455
- if body_open != -1:
456
- body_tag_end = html.find(">", body_open)
457
- if body_tag_end != -1:
458
- html = html[:body_tag_end + 1] + "\n" + tag + html[body_tag_end + 1:]
459
- else:
460
- html = html + "\n" + tag
461
- else:
462
- html = html + "\n" + tag
471
+ pos = _find_probe_insert_pos(html)
472
+ html = html[:pos] + "\n" + tag + html[pos:]
463
473
  return html.encode("utf-8")
464
474
 
465
475