@aiyiran/myclaw 1.1.150 → 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 +1 -1
- package/server/sync_workspace.py +23 -7
package/package.json
CHANGED
package/server/sync_workspace.py
CHANGED
|
@@ -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
|
-
- 不含 →
|
|
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,11 +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
|
-
|
|
453
|
-
|
|
454
|
-
html = html[:idx] + tag + "\n" + html[idx:]
|
|
455
|
-
else:
|
|
456
|
-
html = html + "\n" + tag
|
|
471
|
+
pos = _find_probe_insert_pos(html)
|
|
472
|
+
html = html[:pos] + "\n" + tag + html[pos:]
|
|
457
473
|
return html.encode("utf-8")
|
|
458
474
|
|
|
459
475
|
|