"""Headless Chromium (Playwright), jsdom 29.1 and happy-dom 20.14 on the 29 homepages in sites.txt, the ones
that needed JavaScript in browser-test-2026. One run each, October 3, 2026, from Mumbai, on an Apple M3 Mac."""
import asyncio, json, subprocess
from playwright.async_api import async_playwright
sites=open("sites.txt").read().split()
def node(script, url):
    try:
        out=subprocess.run(["node",script,url],capture_output=True,text=True,timeout=45).stdout.strip().splitlines()
        return json.loads(out[-1]) if out else {"error":"no output"}
    except subprocess.TimeoutExpired: return {"error":"timeout"}
    except Exception as e: return {"error":type(e).__name__}
async def main():
    res=[]
    async with async_playwright() as p:
        b=await p.chromium.launch()
        for d in sites:
            url=f"https://{d}/"
            pg=await b.new_page(user_agent="MyScraperBot/1.0 (https://example.com/bot; you@example.com) chromium")
            try:
                await pg.goto(url,timeout=30000); await pg.wait_for_timeout(5000)
                cw=len((await pg.inner_text("body")).split())
            except Exception as e: cw=type(e).__name__
            await pg.close()
            j=node("jsdom_one.mjs",url); h=node("happy_one.mjs",url)
            r={"domain":d,"chromium":cw,"jsdom":j,"happydom":h}; res.append(r); print(json.dumps(r),flush=True)
        await b.close()
    json.dump(res,open("real.json","w"),indent=1)
asyncio.run(main())
