import urllib.request, json
from urllib.parse import urlsplit

def get(path):
    req = urllib.request.Request("http://127.0.0.1:9222" + path)
    return urllib.request.urlopen(req, timeout=5).read().decode()

def redact_ws(url):
    if not url:
        return None
    p = urlsplit(url)
    return p.netloc  # host:port only, drops /devtools/.../<guid> path

def redact_http(url):
    if not url:
        return None
    p = urlsplit(url)
    seg = [s for s in p.path.split("/") if s]
    first = ("/" + seg[0]) if seg else ""
    return f"{p.scheme}://{p.netloc}{first}/..."

# 1. /json/version
ver = json.loads(get("/json/version"))
print("=== /json/version ===")
print("Browser:", ver.get("Browser"))
print("wsHost:", redact_ws(ver.get("webSocketDebuggerUrl")))

# 2. /json/list (fallback /json)
print("=== /json/list ===")
try:
    targets = json.loads(get("/json/list"))
except Exception:
    targets = json.loads(get("/json"))

for t in targets:
    typ = t.get("type")
    rhost = redact_http(t.get("url"))
    print(f"({typ}, {rhost})")
