// This script runs in an isolated world without host bindings. const state = globalThis.__cfbrowserState ||= { document: `${Date.now()}-${Math.random()}`, snapshot: "", refs: new Map() }; const clip = (s, n) => String(s || "").slice(0, n); // A stack of walkers visits open roots without expanding large child arrays. // The shared node budget includes shadow hosts and non-element nodes. function walk(root, mask, limit) { const state={scanned:0,truncated:false}; state[Symbol.iterator]=function* () { if (!root) return; const stack=[document.createTreeWalker(root,NodeFilter.SHOW_ALL)]; while(stack.length) { const node=stack[stack.length-1].nextNode(); if (!node) { stack.pop();continue; } if (++state.scanned>limit) { state.truncated=true;return; } if(node.nodeType===Node.ELEMENT_NODE && node.shadowRoot) stack.push(document.createTreeWalker(node.shadowRoot,NodeFilter.SHOW_ALL)); if((mask & (1 << (node.nodeType-1)))!==0) yield node; } }; return state; } const textParent = n => n.parentElement || n.getRootNode().host || null; const text = (e, skipControls=false) => { if (!e) return ""; let result=""; for(const node of walk(e,NodeFilter.SHOW_TEXT,200)) { const parent=textParent(node); if (!parent || parent.closest("script,style,noscript,template")) continue; if (skipControls && parent.closest("input,select,textarea,button")) continue; result+=clip(node.data,2048-result.length)+" "; if(result.length>=2048) break; } return result.trim(); }; const visible = e => { const r = e.getBoundingClientRect(), s = getComputedStyle(e); return r.width > 0 && r.height > 0 && s.visibility !== "hidden" && s.display !== "none"; }; const name = e => { const labels=[]; if (e.labels) for (let i=0;i text(e.getRootNode().getElementById(id))).join(" "); return clip(labelled || e.getAttribute("aria-label") || labels.join(" ") || e.getAttribute("alt") || e.getAttribute("title") || (e.tagName === "INPUT" ? e.getAttribute("placeholder") : text(e)), 256).replace(/\s+/g," ").trim(); }; const role = e => e.getAttribute("role") || ({A:"link",BUTTON:"button",SELECT:"combobox",TEXTAREA:"textbox",TABLE:"table"}[e.tagName]) || (e.tagName === "INPUT" ? ({checkbox:"checkbox",radio:"radio",submit:"button",button:"button"}[e.type] || "textbox") : "element"); const fingerprint = e => [e.tagName, e.getAttribute("type"), role(e), name(e), e.getAttribute("href")].join("\u001f"); function resolve(a) { const r = state.refs.get(a.ref); if (a.snapshotId !== state.snapshot || !r || !r.element.isConnected || r.element.ownerDocument !== document || fingerprint(r.element) !== r.fingerprint) { return {error:"BROWSER_STALE_REF"}; } return r.element; } function run(op, a) { if (op === "snapshot") { state.refs.clear(); state.snapshot = a.id; const elements = [], chunks = [], walker = walk(document.body || document.documentElement, NodeFilter.SHOW_ELEMENT,4000); let count = 0, chars = 0, truncated = false; for (const e of walker) { if (["SCRIPT","STYLE","NOSCRIPT","TEMPLATE"].includes(e.tagName) || !visible(e)) continue; if (e.matches("a[href],button,input:not([type=hidden]),select,textarea,[role],[tabindex],table")) { if (elements.length >= a.maxElements) { truncated = true; continue; } const ref = `e${++count}`; state.refs.set(ref, {element:e, fingerprint:fingerprint(e)}); elements.push({ref,role:role(e),name:name(e), value:e.type === "password" || e.type === "file" ? "" : clip(e.value,256), disabled:!!e.disabled, checked:!!e.checked}); } if (a.mode !== "interactive" && e.matches("h1,h2,h3,p,li,pre,td,th") && chars < a.textLimit) { const t = clip(text(e), Math.min(1000, a.textLimit - chars)).trim(); if (t) { chunks.push(t); chars += t.length; } } } return {snapshotId:a.id,documentId:state.document,url:clip(location.href,8192),title:clip(document.title,512), kind:"dom-semantic",text:chunks.join("\n"),elements,truncated:truncated || walker.truncated,scanned:walker.scanned}; } if (op === "state") return {url:clip(location.href,8192),ready:document.readyState,documentId:state.document}; if (op === "wait") { if (a.kind === "url") return location.href.includes(a.value); if (a.kind === "ready") return document.readyState !== "loading"; if (a.kind === "text" || a.kind === "textGone") { const walker = walk(document.body || document.documentElement, NodeFilter.SHOW_TEXT,4000); let s=""; for (const n of walker) { if(s.length>=100000) break; if (textParent(n) && !textParent(n).closest("script,style,noscript,input[type=password]")) s += clip(n.textContent, 1000); } return a.kind === "text" ? s.includes(a.value) : !s.includes(a.value); } return false; } let e = a.ref ? resolve(a) : document.body; if (!e || e.error) return e || {error:"BROWSER_STALE_REF"}; if (op === "read") { if (e.type === "password" || e.type === "file") return {url:location.href,text:"",rows:[],truncated:false}; if (a.mode === "table") { let table = e.tagName === "TABLE" ? e : null; const tables=walk(e,NodeFilter.SHOW_ELEMENT,4000); if(!table) for(const candidate of tables) if(candidate.tagName==="TABLE") { table=candidate;break; } const rows=[]; let total=0, truncated=false; if (table) for (const row of table.rows) { if (rows.length >= 100 || total >= a.limit) { truncated=true;break; } const cells=[]; for (let i=0;i= a.limit) { truncated=true;break; } if (textParent(n) && !textParent(n).closest("script,style,noscript,input[type=password]")) s+=clip(n.textContent,Math.min(1000,a.limit-s.length))+" "; } return {url:clip(location.href,8192),text:s.trim(),truncated:truncated || walker.truncated}; } if (op === "prepare") { if (!visible(e) || e.disabled) return {error:"BROWSER_TARGET_NOT_ACTIONABLE"}; e.scrollIntoView({block:"center",inline:"center"}); const r=e.getBoundingClientRect(), x=r.left+r.width/2,y=r.top+r.height/2; let hit=document.elementFromPoint(x,y); for(let depth=0;hit && hit.shadowRoot && depth<64;depth++) { const inner=hit.shadowRoot.elementFromPoint(x,y); if(!inner || inner===hit) break; hit=inner; } let actionable=false; for(let node=hit,depth=0;node && depth<64;node=node.getRootNode().host,depth++) { if(node===e || e.contains(node)) { actionable=true;break; } } if (!actionable) return {error:"BROWSER_TARGET_NOT_ACTIONABLE"}; e.focus(); return {x,y,value:e.value || "",checked:!!e.checked,tag:e.tagName,type:e.type || "",readOnly:!!e.readOnly}; } if (op === "mutation") { if (!visible(e) || e.disabled) return {error:"BROWSER_TARGET_NOT_ACTIONABLE"}; if (a.action === "click") e.click(); else if (a.action === "fill") { if (!e.matches("input,textarea") || e.type === "file" || e.readOnly) return {error:"BROWSER_TARGET_NOT_ACTIONABLE"}; const proto=e.tagName === "TEXTAREA" ? HTMLTextAreaElement.prototype : HTMLInputElement.prototype; Object.getOwnPropertyDescriptor(proto,"value").set.call(e,a.value); e.dispatchEvent(new Event("input",{bubbles:true}));e.dispatchEvent(new Event("change",{bubbles:true})); } else if (a.action === "select") { let found=false; if (e.tagName === "SELECT") for (let i=0;ie.clientHeight) e.scrollBy(0,delta);else window.scrollBy(0,delta); } else return {error:"BROWSER_CAPABILITY_UNSUPPORTED"}; return {value: e.type === "password" ? "" : clip(e.value,256),checked:!!e.checked}; } return {error:"BROWSER_INVALID_ARGUMENT"}; }