#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Playwright render nantong list page, capture XHR requests for getMessage.do"""
import asyncio, json, re, sys
from playwright.async_api import async_playwright

async def main():
    targets = [
        "https://sthjj.nantong.gov.cn/",
        "https://sthjj.nantong.gov.cn/ntshbj/tzgg/",
    ]
    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",
            viewport={"width": 1400, "height": 900},
        )
        page = await ctx.new_page()
        # capture XHR requests
        captured = []
        async def on_request(req):
            if "getMessage" in req.url or "messageController" in req.url:
                captured.append({"url": req.url, "method": req.method, "post": req.post_data})
        page.on("request", on_request)

        for t in targets:
            print(f"=== visiting {t} ===")
            try:
                resp = await page.goto(t, wait_until="domcontentloaded", timeout=45000)
                print(f"  status={resp.status if resp else '?'}")
                await page.wait_for_timeout(8000)  # wait for JS + possible WAF
                html = await page.content()
                print(f"  html len={len(html)}")
                # find tzgg links
                links = await page.eval_on_selector_all("a", "els => els.map(e => e.href).filter(h => h.includes('tzgg'))")
                print("  tzgg links:", list(dict.fromkeys(links))[:15])
                # check for WAF markers
                if "473" in html[:500] or "waf" in html[:1000].lower():
                    print("  WAF page marker detected")
            except Exception as e:
                print(f"  ERR: {e}")
            await page.wait_for_timeout(2000)

        print("\n=== captured getMessage requests ===")
        for c in captured:
            print(json.dumps(c, ensure_ascii=False))
        await browser.close()

asyncio.run(main())
