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"))
info=p.eval("""(function(){
  var lbl=document.querySelector('label.export-text');
  var sel=document.querySelector('select.export-select');
  var cb=document.querySelector('input.export-checkbox');
  var opts = sel ? Array.from(sel.options).map(function(o){return {v:o.value, t:o.text.trim()};}) : [];
  // find run/generate/preview buttons
  function vis(e){var r=e.getBoundingClientRect();return r.width>0&&r.height>0;}
  var btns=[];
  document.querySelectorAll('button,a,input[type=button],input[type=submit]').forEach(function(b){if(!vis(b))return; var s=(b.innerText||b.value||'').trim(); if(s&&s.length<30)btns.push(s);});
  return JSON.stringify({
    export_label: lbl?lbl.innerText.trim():null,
    checkbox_checked: cb?cb.checked:null,
    select_options: opts,
    buttons: Array.from(new Set(btns)).slice(0,30)
  });
})()""")
print("EXPORT INFO:", info)
print("DONE_OPTS")
p.close()
