Spaces:
Running
Running
File size: 3,762 Bytes
8ddf70a b30d0d9 8ddf70a b30d0d9 8ddf70a b30d0d9 8ddf70a b30d0d9 8ddf70a b30d0d9 8ddf70a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 | """后台页面内联 JS 语法校验:把 admin_html 里的 <script> 交给 node --check。
后台是一大段 Python 字符串里的 HTML/JS,改界面时最怕引号、转义写坏:
`python -m py_compile` 只查 Python 那层,JS 即使语法全错也照样通过,
部署上去才发现整个后台白屏。这里把内联脚本抽出来单独过一遍语法检查,
顺带确认几个关键函数还在(大改之后最容易出现的"函数被误删")。
需要 node(仓库里公式渲染侧车本来就要求 node,不算新依赖)。
运行:python -X utf8 _admin_ui_js_check.py
"""
from __future__ import annotations
import os
import re
import shutil
import subprocess
import sys
import tempfile
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from app.admin_html import _ADMIN_UI_REV, _HTML_TEMPLATE as html # noqa: E402
FAILS: list[str] = []
def check(name: str, cond: bool, extra: str = "") -> None:
print((" [PASS] " if cond else " [FAIL] ") + name + ("" if cond else f" {extra}"))
if not cond:
FAILS.append(name)
def main() -> int:
print(f"[info] admin ui rev = {_ADMIN_UI_REV}, html = {len(html)} chars")
node = shutil.which("node")
scripts = re.findall(r"<script[^>]*>(.*?)</script>", html, re.S)
print(f"[info] script blocks = {len(scripts)}")
if not node:
print(" [SKIP] 没装 node,跳过 JS 语法检查")
else:
for i, js in enumerate(scripts):
if not js.strip():
continue
with tempfile.NamedTemporaryFile("w", suffix=f".chk{i}.js",
delete=False, encoding="utf-8") as f:
f.write(js)
path = f.name
r = subprocess.run([node, "--check", path], capture_output=True, text=True)
check(f"script#{i} 语法({len(js)} chars)", r.returncode == 0, r.stderr[:2000])
os.unlink(path)
# 关键函数必须在:改界面时被误删就会在这里报出来
for name in (
"function authModeIds", "function onAuthModeChange",
"function applyDualKeyPreset", "function mergeDualHeader",
"function collectExtraHeaders", "function authModeBodyCheck",
"function pendingHeaderNames", "function authHeaderLabel",
"function extraHeadersToText", "function addProvider",
"function saveProviderEdit", "function editProvider",
"function probeNewProvider", "function loadProviders",
"function splitModelList",
):
check(name, name in html)
# 新版控件与版本号占位符(第二个请求头输入框必须在折叠块外面,不能藏在 <details> 里)
for token in ("pAuthMode", "epAuthMode", "pAuthModeHint", "epAuthModeHint",
"pDualRow", "epDualRow", "pDualHeaderName", "pDualHeaderValue",
"epDualHeaderName", "epDualHeaderValue",
"applyDualKeyPreset('p')", "applyDualKeyPreset('ep')",
'id="uiRevBadge"',
'<meta name="xtc-admin-rev" content="__UI_REV__">'):
check(f"页面含 {token}", token in html)
# 结构校验:第二个请求头那一行要在 <details> 之前(否则又会被折叠起来看不见)
i_keys = html.find('id="pKeys"')
i_row = html.find('id="pDualRow"')
i_det = html.find('<details class="adv-box">', i_keys)
check("第二个请求头输入框在折叠块之前(可见)",
0 < i_keys < i_row < i_det, f"pKeys={i_keys} pDualRow={i_row} details={i_det}")
print("\n" + ("=" * 60))
if FAILS:
print(f"FAILED: {len(FAILS)} -> {FAILS}")
return 1
print("ALL PASS")
return 0
if __name__ == "__main__":
sys.exit(main())
|