#!/usr/bin/env python3 """为四所 templates 注入 instance_theme 脚本/样式与切换按钮。""" from __future__ import annotations from pathlib import Path ROOT = Path(__file__).resolve().parents[1] EXCHANGES = ("crypto_monitor_binance", "crypto_monitor_okx", "crypto_monitor_gate", "crypto_monitor_gate_bot") FILES = ("index.html", "login.html", "key_focus_v2.html", "order_focus_v2.html") SCRIPT_TAG = ' \n' CSS_LINK = ' \n' THEME_TOGGLE = """
""" INDEX_HEADER_OLD = """

加密货币|交易监控 + AI复盘一体化

{{ exchange_display }}
""" INDEX_HEADER_NEW = """

加密货币|交易监控 + AI复盘一体化

{{ exchange_display }}
""" + THEME_TOGGLE + """
""" def patch_file(path: Path) -> bool: text = path.read_text(encoding="utf-8") orig = text if 'data-theme="dark"' not in text: text = text.replace('', '', 1) if "/static/instance_theme.js" not in text: text = text.replace( "", "\n" + SCRIPT_TAG.strip() + "\n", 1, ) if "/static/instance_theme.css" not in text: text = text.replace("", "\n" + CSS_LINK, 1) if path.name == "index.html" and INDEX_HEADER_OLD in text and "instance-theme-toggle" not in text: text = text.replace(INDEX_HEADER_OLD, INDEX_HEADER_NEW) if path.name == "login.html" and "instance-theme-toggle" not in text: text = text.replace( "", '
\n' + THEME_TOGGLE + "
\n", 1, ) if path.name == "key_focus_v2.html" and "instance-theme-toggle" not in text: marker = '
' if marker in text: text = text.replace( marker, marker + "\n " + THEME_TOGGLE.replace("\n", "\n "), 1, ) if path.name == "order_focus_v2.html" and "instance-theme-toggle" not in text: marker = '
' if marker in text: text = text.replace( marker, marker + "\n " + THEME_TOGGLE.replace("\n", "\n "), 1, ) if text != orig: path.write_text(text, encoding="utf-8") return True return False def main() -> None: n = 0 for ex in EXCHANGES: for fn in FILES: p = ROOT / ex / "templates" / fn if p.is_file() and patch_file(p): print("patched", p.relative_to(ROOT)) n += 1 print("done", n, "files") if __name__ == "__main__": main()