import sys, time, json, urllib.request
sys.path.insert(0, "/tmp/amdwork")
import cdp
def targets():
    return [t for t in json.loads(urllib.request.urlopen("http://localhost:9223/json",timeout=10).read()) if t.get("type")=="page"]
rc=None
for t in targets():
    if "/reportcenter/" in (t.get("url") or ""): rc=t; break
p=cdp.attach(rc); p.enable()
print("url:", p.eval("location.href"))
print("title:", p.eval("document.title"))
body=(p.eval("document.body.innerText") or "")
print("body[:1500]:", body[:1500])
# all visible buttons/links/icons with export-ish meaning
ctrls=p.eval("""(function(){
  function vis(e){var r=e.getBoundingClientRect();return r.width>0&&r.height>0;}
  var out=[];
  document.querySelectorAll('button,a,[role=button],input[type=button],input[type=submit],i,[class*=icon],[title],[aria-label]').forEach(function(b){
    if(!vis(b))return;
    var s=((b.innerText||b.value||'')+' | '+(b.title||'')+' | '+(b.getAttribute('aria-label')||'')+' | '+(b.className||'')).trim();
    if(/export|csv|download|excel|run|generate|view/i.test(s)) out.push(s.slice(0,90));
  });
  return JSON.stringify(Array.from(new Set(out)).slice(0,30));
})()""")
print("EXPORT-ISH CONTROLS:", ctrls)
# also: all buttons regardless
allb=p.eval("""(function(){
  function vis(e){var r=e.getBoundingClientRect();return r.width>0&&r.height>0;}
  var out=[];
  document.querySelectorAll('button,a,[role=button]').forEach(function(b){if(!vis(b))return; var s=(b.innerText||b.title||'').trim(); if(s&&s.length<40)out.push(s);});
  return JSON.stringify(Array.from(new Set(out)).slice(0,40));
})()""")
print("ALL BUTTONS:", allb)
p.screenshot("/tmp/amdwork/rc-current-report.png")
print("DONE_INSPECT")
p.close()
