#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Capture the real getMessage.do request params jpage.js sends when paginating"""
import asyncio, json
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",
            viewport={"width": 1400, "height": 900},
        )
        page = await ctx.new_page()
        captured = []
        async def on_request(req):
            if "getMessage" in req.url:
                captured.append({"url": req.url, "method": req.method, "post": req.post_data,
                                 "headers": {k: v for k, v in req.headers.items() if k.lower() in ("content-type", "referer", "x-requested-with")}})
        async def on_response(resp):
            if "getMessage" in resp.url:
                try:
                    body = await resp.text()
                    captured.append({"RESP": resp.url, "status": resp.status, "body_head": body[:400]})
                except Exception as e:
                    captured.append({"RESP_ERR": str(e)})
        page.on("request", on_request)
        page.on("response", on_response)

        await page.goto("https://sthjj.nantong.gov.cn/ntshbj/tzgg/tzgg.html", wait_until="domcontentloaded", timeout=60000)
        await page.wait_for_timeout(8000)
        # screenshot pagination area
        try:
            # find pagination links
            pages = await page.eval_on_selector_all(".page a, #page a, .jpage a, a[onclick]", "els => els.map(e => ({t: e.innerText.trim(), o: e.getAttribute('onclick'), h: e.href}))")
            print("pagination els:", json.dumps(pages[:20], ensure_ascii=False))
            # click the "2" page link
            clicked = False
            for el in await page.query_selector_all("a"):
                txt = (await el.inner_text()).strip()
                if txt == "2" or txt == "下一页" or txt == ">":
                    try:
                        await el.click()
                        clicked = True
                        print("clicked:", txt)
                        break
                    except Exception as e:
                        print("click fail", txt, e)
            if clicked:
                await page.wait_for_timeout(6000)
        except Exception as e:
            print("pagination err:", e)

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

asyncio.run(main())
