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"))
# iframes
fr=p.eval("JSON.stringify(Array.from(document.querySelectorAll('iframe')).map(function(f){return f.src;}))")
print("IFRAMES:", fr)
# ALL elements with title/aria/class hinting export/print/pdf/excel - icon buttons often <i> or <span> or <a> with class
icons=p.eval("""(function(){
  function vis(e){var r=e.getBoundingClientRect();return r.width>0&&r.height>0;}
  var out=[];
  document.querySelectorAll('*').forEach(function(e){
    if(!vis(e))return;
    var t=(e.title||'')+'|'+(e.getAttribute&&(e.getAttribute('aria-label')||'')||'')+'|'+((e.className||'').toString());
    if(/export|csv|pdf|excel|print|download|xls/i.test(t)){
      out.push({tag:e.tagName, title:e.title, aria:(e.getAttribute&&e.getAttribute('aria-label'))||'', cls:(e.className||'').toString().slice(0,60)});
    }
  });
  // dedupe
  var seen={};var u=[];out.forEach(function(o){var k=JSON.stringify(o); if(!seen[k]){seen[k]=1;u.push(o);}});
  return JSON.stringify(u.slice(0,25));
})()""")
print("ICON CONTROLS:", icons)
# Also dump top toolbar region HTML
tb=p.eval("""(function(){
  var el=Array.from(document.querySelectorAll('*')).find(function(e){return (e.innerText||'').trim()==='Close';});
  if(!el) return 'NOCLOSE';
  var bar=el; for(var i=0;i<4;i++){if(bar.parentElement)bar=bar.parentElement;}
  return bar.outerHTML.slice(0,2000);
})()""")
print("TOOLBAR HTML:", tb)
print("DONE_TB")
p.close()
