|
@@ -3,15 +3,34 @@ const state = globalThis.__cfbrowserState ||= {
|
|
|
document: `${Date.now()}-${Math.random()}`, snapshot: "", refs: new Map()
|
|
document: `${Date.now()}-${Math.random()}`, snapshot: "", refs: new Map()
|
|
|
};
|
|
};
|
|
|
const clip = (s, n) => String(s || "").slice(0, n);
|
|
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) => {
|
|
const text = (e, skipControls=false) => {
|
|
|
if (!e) return "";
|
|
if (!e) return "";
|
|
|
- const walker=document.createTreeWalker(e,NodeFilter.SHOW_TEXT);
|
|
|
|
|
- let node, result="", count=0;
|
|
|
|
|
- while ((node=walker.nextNode()) && count++<200 && result.length<2048) {
|
|
|
|
|
- const parent=node.parentElement;
|
|
|
|
|
|
|
+ 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 (!parent || parent.closest("script,style,noscript,template")) continue;
|
|
|
if (skipControls && parent.closest("input,select,textarea,button")) continue;
|
|
if (skipControls && parent.closest("input,select,textarea,button")) continue;
|
|
|
result+=clip(node.data,2048-result.length)+" ";
|
|
result+=clip(node.data,2048-result.length)+" ";
|
|
|
|
|
+ if(result.length>=2048) break;
|
|
|
}
|
|
}
|
|
|
return result.trim();
|
|
return result.trim();
|
|
|
};
|
|
};
|
|
@@ -23,7 +42,7 @@ const name = e => {
|
|
|
const labels=[];
|
|
const labels=[];
|
|
|
if (e.labels) for (let i=0;i<Math.min(e.labels.length,20);i++) labels.push(text(e.labels[i],true));
|
|
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)
|
|
const labelled = clip(e.getAttribute("aria-labelledby"),4096).split(/\s+/).slice(0,16)
|
|
|
- .filter(Boolean).map(id => text(document.getElementById(id))).join(" ");
|
|
|
|
|
|
|
+ .filter(Boolean).map(id => text(e.getRootNode().getElementById(id))).join(" ");
|
|
|
return clip(labelled || e.getAttribute("aria-label") ||
|
|
return clip(labelled || e.getAttribute("aria-label") ||
|
|
|
labels.join(" ") ||
|
|
labels.join(" ") ||
|
|
|
e.getAttribute("alt") || e.getAttribute("title") ||
|
|
e.getAttribute("alt") || e.getAttribute("title") ||
|
|
@@ -43,10 +62,9 @@ function resolve(a) {
|
|
|
function run(op, a) {
|
|
function run(op, a) {
|
|
|
if (op === "snapshot") {
|
|
if (op === "snapshot") {
|
|
|
state.refs.clear(); state.snapshot = a.id;
|
|
state.refs.clear(); state.snapshot = a.id;
|
|
|
- const elements = [], chunks = [], walker = document.createTreeWalker(document.body || document.documentElement, NodeFilter.SHOW_ELEMENT);
|
|
|
|
|
- let e, scanned = 0, count = 0, chars = 0, truncated = false;
|
|
|
|
|
- while ((e = walker.nextNode())) {
|
|
|
|
|
- if (++scanned > 4000) { truncated = true; break; }
|
|
|
|
|
|
|
+ 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 (["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 (e.matches("a[href],button,input:not([type=hidden]),select,textarea,[role],[tabindex],table")) {
|
|
|
if (elements.length >= a.maxElements) { truncated = true; continue; }
|
|
if (elements.length >= a.maxElements) { truncated = true; continue; }
|
|
@@ -62,17 +80,18 @@ function run(op, a) {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
return {snapshotId:a.id,documentId:state.document,url:clip(location.href,8192),title:clip(document.title,512),
|
|
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,scanned};
|
|
|
|
|
|
|
+ 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 === "state") return {url:clip(location.href,8192),ready:document.readyState,documentId:state.document};
|
|
|
if (op === "wait") {
|
|
if (op === "wait") {
|
|
|
if (a.kind === "url") return location.href.includes(a.value);
|
|
if (a.kind === "url") return location.href.includes(a.value);
|
|
|
if (a.kind === "ready") return document.readyState !== "loading";
|
|
if (a.kind === "ready") return document.readyState !== "loading";
|
|
|
if (a.kind === "text" || a.kind === "textGone") {
|
|
if (a.kind === "text" || a.kind === "textGone") {
|
|
|
- const walker = document.createTreeWalker(document.body || document.documentElement, NodeFilter.SHOW_TEXT);
|
|
|
|
|
- let n, s="", count=0;
|
|
|
|
|
- while ((n=walker.nextNode()) && count++<4000 && s.length<100000) {
|
|
|
|
|
- if (n.parentElement && !n.parentElement.closest("script,style,noscript,input[type=password]")) s += clip(n.textContent, 1000);
|
|
|
|
|
|
|
+ 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 a.kind === "text" ? s.includes(a.value) : !s.includes(a.value);
|
|
|
}
|
|
}
|
|
@@ -83,7 +102,9 @@ function run(op, a) {
|
|
|
if (op === "read") {
|
|
if (op === "read") {
|
|
|
if (e.type === "password" || e.type === "file") return {url:location.href,text:"",rows:[],truncated:false};
|
|
if (e.type === "password" || e.type === "file") return {url:location.href,text:"",rows:[],truncated:false};
|
|
|
if (a.mode === "table") {
|
|
if (a.mode === "table") {
|
|
|
- const table = e.tagName === "TABLE" ? e : e.querySelector("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;
|
|
const rows=[]; let total=0, truncated=false;
|
|
|
if (table) for (const row of table.rows) {
|
|
if (table) for (const row of table.rows) {
|
|
|
if (rows.length >= 100 || total >= a.limit) { truncated=true;break; }
|
|
if (rows.length >= 100 || total >= a.limit) { truncated=true;break; }
|
|
@@ -94,22 +115,31 @@ function run(op, a) {
|
|
|
}
|
|
}
|
|
|
rows.push(cells);
|
|
rows.push(cells);
|
|
|
}
|
|
}
|
|
|
- return {url:clip(location.href,8192),rows,truncated};
|
|
|
|
|
|
|
+ return {url:clip(location.href,8192),rows,truncated:truncated || tables.truncated};
|
|
|
}
|
|
}
|
|
|
- const walker=document.createTreeWalker(e,NodeFilter.SHOW_TEXT);let n,s="",count=0,truncated=false;
|
|
|
|
|
- while ((n=walker.nextNode())) {
|
|
|
|
|
- if (++count > 4000 || s.length >= a.limit) { truncated=true;break; }
|
|
|
|
|
- if (n.parentElement && !n.parentElement.closest("script,style,noscript,input[type=password]")) s+=clip(n.textContent,Math.min(1000,a.limit-s.length))+" ";
|
|
|
|
|
|
|
+ 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};
|
|
|
|
|
|
|
+ return {url:clip(location.href,8192),text:s.trim(),truncated:truncated || walker.truncated};
|
|
|
}
|
|
}
|
|
|
if (op === "prepare") {
|
|
if (op === "prepare") {
|
|
|
if (!visible(e) || e.disabled) return {error:"BROWSER_TARGET_NOT_ACTIONABLE"};
|
|
if (!visible(e) || e.disabled) return {error:"BROWSER_TARGET_NOT_ACTIONABLE"};
|
|
|
e.scrollIntoView({block:"center",inline:"center"});
|
|
e.scrollIntoView({block:"center",inline:"center"});
|
|
|
const r=e.getBoundingClientRect(), x=r.left+r.width/2,y=r.top+r.height/2;
|
|
const r=e.getBoundingClientRect(), x=r.left+r.width/2,y=r.top+r.height/2;
|
|
|
- const hit=document.elementFromPoint(x,y);
|
|
|
|
|
- if (!hit || (hit!==e && !e.contains(hit))) return {error:"BROWSER_TARGET_NOT_ACTIONABLE"};
|
|
|
|
|
- e.focus(); return {x,y,value:e.value || "",checked:!!e.checked,tag:e.tagName,type:e.type || ""};
|
|
|
|
|
|
|
+ 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 (op === "mutation") {
|
|
|
if (!visible(e) || e.disabled) return {error:"BROWSER_TARGET_NOT_ACTIONABLE"};
|
|
if (!visible(e) || e.disabled) return {error:"BROWSER_TARGET_NOT_ACTIONABLE"};
|