import subprocess, time, requests, json

chrome_exe = r"C:\Program Files\Google\Chrome\Application\chrome.exe"
user_data = r"C:\Users\Admin\AppData\Local\Google\Chrome\User Data"

cmd = [
    chrome_exe,
    f"--user-data-dir={user_data}",
    "--profile-directory=Profile 5",
    "--remote-debugging-port=9222",
    "--no-first-run",
    "--no-default-browser-check",
    "https://muse.ai"
]

print("[1] Spawning Chrome with CDP on port 9222...", flush=True)
proc = subprocess.Popen(cmd)
print(f"[2] Process PID: {proc.pid}", flush=True)

time.sleep(5)

try:
    res = requests.get("http://127.0.0.1:9222/json", timeout=5)
    print(f"[3] CDP /json response (status {res.status_code}):", flush=True)
    pages = res.json()
    for p in pages:
        print(f"    - Type: {p.get('type')} | Title: {p.get('title')} | URL: {p.get('url')}", flush=True)
except Exception as e:
    print(f"[!] Failed to connect to CDP: {e}", flush=True)
