| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167 |
- // 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<Math.min(e.labels.length,20);i++) labels.push(text(e.labels[i],true));
- const labelled = clip(e.getAttribute("aria-labelledby"),4096).split(/\s+/).slice(0,16)
- .filter(Boolean).map(id => 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<Math.min(row.cells.length,30);i++) {
- const cell=row.cells[i];
- const v=clip(text(cell),Math.min(512,a.limit-total)); total+=v.length;cells.push(v);
- }
- rows.push(cells);
- }
- return {url:clip(location.href,8192),rows,truncated:truncated || tables.truncated};
- }
- const walker=walk(e,NodeFilter.SHOW_TEXT,4000);let s="",truncated=false;
- for (const n of walker) {
- if (s.length >= 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;i<Math.min(e.options.length,1000);i++) if(e.options[i].value===a.value){found=true;break;}
- if (!found) return {error:"BROWSER_INVALID_ARGUMENT"};
- e.value=a.value;e.dispatchEvent(new Event("input",{bubbles:true}));e.dispatchEvent(new Event("change",{bubbles:true}));
- } else if (a.action === "check" || a.action === "uncheck") {
- if (!e.matches("input[type=checkbox],input[type=radio]")) return {error:"BROWSER_INVALID_ARGUMENT"};
- const target=a.action === "check"; if (e.checked!==target) e.click();
- } else if (a.action === "scroll") {
- const delta=a.value === "down" ? 500 : -500;
- if (e.scrollHeight>e.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"};
- }
|