import sys, json
sys.path.insert(0, "/tmp/curo")
import cdp_agent as C

pages = C.list_pages()
print("=== TARGETS (9223) ===")
for p in pages:
    print(p.get("type"), "|", (p.get("title") or "")[:50], "|", (p.get("url") or "")[:100])

t = C.find_target()
pg = C.attach(t)
pg.enable()
print("\nURL:", pg.eval("location.href"))
print("TITLE:", pg.eval("document.title"))

# iframes on the page
frames = pg.eval("""
JSON.stringify(Array.from(document.querySelectorAll('iframe,frame')).map(f=>({
  id:f.id,name:f.name,src:(f.src||'').slice(0,120)
})))
""")
print("\nIFRAMES:", frames)

# all inputs top frame
inp = pg.eval("""
JSON.stringify(Array.from(document.querySelectorAll('input')).map(el=>({
  type:el.type,id:el.id,name:el.name,ph:el.placeholder||'',vis:!!el.offsetParent
})))
""")
print("\nTOP-FRAME INPUTS:", inp)

# visible body text
print("\nBODY TEXT (1500):")
print(pg.eval("document.body.innerText.slice(0,1500)"))

pg.screenshot("/tmp/curo/probe2.png")
print("\nSCREENSHOT /tmp/curo/probe2.png")
pg.close()
