#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Save nantong homepage HTML and extract list-page links + js refs"""
import asyncio, re
from playwright.async_api import async_playwright

async def main():
    async with async_playwright() as p:
        browser = await p.chromium.launch(headless=True, args=["--disable-blink-features=AutomationControlled", "--no-sandbox"])
        ctx = await browser.new_context(user_agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36")
        page = await ctx.new_page()
        await page.goto("https://sthjj.nantong.gov.cn/", wait_until="domcontentloaded", timeout=45000)
        await page.wait_for_timeout(6000)
        html = await page.content()
        with open("/root/gov_crawler/nt_home.html", "w", encoding="utf-8") as f:
            f.write(html)
        print("saved, len:", len(html))
        # all links containing tzgg or list
        links = await page.eval_on_selector_all("a", "els => els.map(e => ({t: e.innerText.trim().slice(0,30), h: e.href}))")
        seen = set()
        for l in links:
            if "tzgg" in l["h"] or "list" in l["h"].lower() or "column" in l["h"].lower():
                key = l["h"]
                if key in seen: continue
                seen.add(key)
                print(f"  [{l['t']}] {l['h']}")
        # find scripts referencing getMessage / jpage
        scripts = await page.eval_on_selector_all("script", "els => els.map(e => e.src || e.innerText.slice(0,200))")
        for s in scripts:
            if s and ("getMessage" in s or "jpage" in s.lower() or "truecms" in s.lower()):
                print("SCRIPT:", s[:300])
        await browser.close()

asyncio.run(main())
