import os, re
os.chdir(r"D:\work buddy\2026-11-05-14-15-52")

html = open("index_main.html", encoding="utf-8").read()

# 1. 修复 jhu-msf 后缺逗号
# 找 jhu-msf 条目结尾，在 } 后加逗号
# 从输出看，第1120行是 {id:"jhu-msf",... 结尾没有逗号
# 直接定位
old = '{id:"jhu-msf",s:"jhu",n:"金融学硕士（MS in Finance · 9/12/15 个月 · STEM · CFA-aligned · Carey）",en:"Master of Science in Finance (MS in Finance, Carey Business School) - 9/12/15-month full-time formats",deg:"MSF",lvl:"m",stem:1,dur:"3 个版本：9-month (30 credits, $78,500) / 12-month (36 credits, $94,500) / 15-month (36 credits, $99,750, DC only)",ddl:"Fall 入学；具体轮次待 Carey 系站确认",lang:"TOEFL iBT / IELTS Academic",test:{cls:"GRE/GMAT required (holistic review)"},tui:"$78,500 / $94,500 / $99,750",url:"details/jhu-msf.html"}'
# 找这个条目在 html 里的位置
idx = html.find('url:"details/jhu-msf.html"}')
if idx > 0:
    # 检查后面是否紧跟换行或逗号
    after = html[idx:idx+30]
    print(f"jhu-msf 结尾附近: {repr(after)}")
    # 如果后面不是逗号，加逗号
    if after[len('url:"details/jhu-msf.html"}')] != ',':
        html = html[:idx + len('url:"details/jhu-msf.html"}')] + ',' + html[idx + len('url:"details/jhu-msf.html"}'):]
        print("  -> 已加逗号")
    else:
        print("  -> 已有逗号")

# 2. 撤销错误的 VD 追加（把插到 localStorage.getItem 里的 url 删掉）
# 从调试输出看，错误插入的内容是：
# let favs=JSON.parse(localStorage.getItem("nxf_favs",\n"details/umich-ross-macc.html",\n"details/umich-ross-mscm.html",\n"details/duke-mqm-ba.html",\n"details/usc-msmkt.html"\n)||"[]");
# 应该恢复为：
# let favs=JSON.parse(localStorage.getItem("nxf_favs"))||"[]");

bad = 'let favs=JSON.parse(localStorage.getItem("nxf_favs",\n"details/umich-ross-macc.html",\n"details/umich-ross-mscm.html",\n"details/duke-mqm-ba.html",\n"details/usc-msmkt.html"\n)||"[]");'
good = 'let favs=JSON.parse(localStorage.getItem("nxf_favs"))||"[]");'

if bad in html:
    html = html.replace(bad, good)
    print("VD 错误插入已撤销")
else:
    print("VD 错误插入未找到，尝试其他模式...")
    # 尝试找模式
    pattern = r'let favs=JSON\.parse\(localStorage\.getItem\("nxf_favs",[\s\S]*?\)\|\|"[]"\);'
    m = re.search(pattern, html)
    if m:
        print(f"  找到模式: {repr(m.group()[:200])}")
        html = html[:m.start()] + good + html[m.end():]
        print("  -> 已修复")

# 3. 用正确逻辑追加 VD（初始 depth=1，因为 Set( 的 ( 还没闭合）
vd_start = html.index("const VERIFIED_DETAILS=new Set(")
# 从 ( 开始，初始 depth=1
i = vd_start + len("const VERIFIED_DETAILS=new Set")  # 指向 (
depth = 1  # 因为 ( 已开
j = i + 1  # 从 ( 后面开始
while j < len(html) and depth > 0:
    if html[j] == '(':
        depth += 1
    elif html[j] == ')':
        depth -= 1
        if depth == 0:
            vd_end = j
            break
    j += 1

print(f"VD Set 正确结束位置: {vd_end}")
print(f"  VD 结尾附近: {repr(html[vd_end-100:vd_end+20])}")

# 在 VD Set 内部最后一个 " 后追加
vd_inner = html[i+1:vd_end]
last_quote = vd_inner.rstrip().rfind('"')
new_vd_block = ",\n" + ",\n".join(f'"{u}"' for u in ["details/umich-ross-macc.html", "details/umich-ross-mscm.html", "details/duke-mqm-ba.html", "details/usc-msmkt.html"])
insert_pos = i + 1 + last_quote + 1
html = html[:insert_pos] + new_vd_block + "\n" + html[insert_pos:]
print("VD 已正确追加")

with open("index_main.html", "w", encoding="utf-8") as f:
    f.write(html)

print(f"\n文件已写回，大小: {os.path.getsize('index_main.html')} bytes")
