@usecrow/client 0.1.24 → 0.1.26

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,9 @@
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});async function waitFor(e){await new Promise(n=>setTimeout(n,e*1e3))}async function movePointerToElement(e){const n=e.getBoundingClientRect(),s=n.left+n.width/2,i=n.top+n.height/2;window.dispatchEvent(new CustomEvent("PageAgent::MovePointerTo",{detail:{x:s,y:i}})),await waitFor(.3)}function getElementByIndex(e,n){const s=e.get(n);if(!s)throw new Error(`No interactive element found at index ${n}`);const i=s.ref;if(!i)throw new Error(`Element at index ${n} does not have a reference`);if(!(i instanceof HTMLElement))throw new Error(`Element at index ${n} is not an HTMLElement`);return i}let lastClickedElement=null;function blurLastClickedElement(){lastClickedElement&&(lastClickedElement.blur(),lastClickedElement.dispatchEvent(new MouseEvent("mouseout",{bubbles:!0,cancelable:!0})),lastClickedElement=null)}async function clickElement(e){blurLastClickedElement(),lastClickedElement=e,await scrollIntoViewIfNeeded(e),await movePointerToElement(e),window.dispatchEvent(new CustomEvent("PageAgent::ClickPointer")),await waitFor(.1),e.dispatchEvent(new MouseEvent("mouseenter",{bubbles:!0,cancelable:!0})),e.dispatchEvent(new MouseEvent("mouseover",{bubbles:!0,cancelable:!0})),e.dispatchEvent(new MouseEvent("mousedown",{bubbles:!0,cancelable:!0})),e.focus(),e.dispatchEvent(new MouseEvent("mouseup",{bubbles:!0,cancelable:!0})),e.dispatchEvent(new MouseEvent("click",{bubbles:!0,cancelable:!0})),await waitFor(.1)}const nativeInputValueSetter=Object.getOwnPropertyDescriptor(window.HTMLInputElement.prototype,"value").set,nativeTextAreaValueSetter=Object.getOwnPropertyDescriptor(window.HTMLTextAreaElement.prototype,"value").set;async function inputTextElement(e,n){if(!(e instanceof HTMLInputElement||e instanceof HTMLTextAreaElement))throw new Error("Element is not an input or textarea");await clickElement(e),e instanceof HTMLTextAreaElement?nativeTextAreaValueSetter.call(e,n):nativeInputValueSetter.call(e,n);const s=new Event("input",{bubbles:!0});e.dispatchEvent(s),await waitFor(.1),blurLastClickedElement()}async function selectOptionElement(e,n){if(!(e instanceof HTMLSelectElement))throw new Error("Element is not a select element");const i=Array.from(e.options).find(b=>{var d;return((d=b.textContent)==null?void 0:d.trim())===n.trim()});if(!i)throw new Error(`Option with text "${n}" not found in select element`);e.value=i.value,e.dispatchEvent(new Event("change",{bubbles:!0})),await waitFor(.1)}async function scrollIntoViewIfNeeded(e){const n=e;n.scrollIntoViewIfNeeded?n.scrollIntoViewIfNeeded():n.scrollIntoView({behavior:"auto",block:"center",inline:"nearest"})}async function scrollVertically(e,n,s){if(s){const l=s;console.log("[SCROLL DEBUG] Starting direct container scroll for element:",l.tagName);let h=l,v=!1,w=null,u=0,f=0;const T=n;for(;h&&f<10;){const F=window.getComputedStyle(h),P=/(auto|scroll|overlay)/.test(F.overflowY),O=h.scrollHeight>h.clientHeight;if(console.log("[SCROLL DEBUG] Checking element:",h.tagName,"hasScrollableY:",P,"canScrollVertically:",O,"scrollHeight:",h.scrollHeight,"clientHeight:",h.clientHeight),P&&O){const R=h.scrollTop,z=h.scrollHeight-h.clientHeight;let H=T/3;H>0?H=Math.min(H,z-R):H=Math.max(H,-R),h.scrollTop=R+H;const D=h.scrollTop,_=D-R;if(console.log("[SCROLL DEBUG] Scroll attempt:",h.tagName,"before:",R,"after:",D,"delta:",_),Math.abs(_)>.5){v=!0,w=h,u=_,console.log("[SCROLL DEBUG] Successfully scrolled container:",h.tagName,"delta:",_);break}}if(h===document.body||h===document.documentElement)break;h=h.parentElement,f++}return v?`Scrolled container (${w==null?void 0:w.tagName}) by ${u}px`:`No scrollable container found for element (${l.tagName})`}const i=n,b=l=>l.clientHeight>=window.innerHeight*.5,d=l=>l&&/(auto|scroll|overlay)/.test(getComputedStyle(l).overflowY)&&l.scrollHeight>l.clientHeight&&b(l);let r=document.activeElement;for(;r&&!d(r)&&r!==document.body;)r=r.parentElement;if(r=d(r)?r:Array.from(document.querySelectorAll("*")).find(d)||document.scrollingElement||document.documentElement,r===document.scrollingElement||r===document.documentElement||r===document.body){const l=window.scrollY,h=document.documentElement.scrollHeight-window.innerHeight;window.scrollBy(0,i);const v=window.scrollY,w=v-l;if(Math.abs(w)<1)return i>0?"⚠️ Already at the bottom of the page, cannot scroll down further.":"⚠️ Already at the top of the page, cannot scroll up further.";const u=i>0&&v>=h-1,f=i<0&&v<=1;return u?`✅ Scrolled page by ${w}px. Reached the bottom of the page.`:f?`✅ Scrolled page by ${w}px. Reached the top of the page.`:`✅ Scrolled page by ${w}px.`}else{const l=r.scrollTop,h=r.scrollHeight-r.clientHeight;r.scrollBy({top:i,behavior:"smooth"}),await waitFor(.1);const v=r.scrollTop,w=v-l;if(Math.abs(w)<1)return i>0?`⚠️ Already at the bottom of container (${r.tagName}), cannot scroll down further.`:`⚠️ Already at the top of container (${r.tagName}), cannot scroll up further.`;const u=i>0&&v>=h-1,f=i<0&&v<=1;return u?`✅ Scrolled container (${r.tagName}) by ${w}px. Reached the bottom.`:f?`✅ Scrolled container (${r.tagName}) by ${w}px. Reached the top.`:`✅ Scrolled container (${r.tagName}) by ${w}px.`}}async function scrollHorizontally(e,n,s){if(s){const l=s;console.log("[SCROLL DEBUG] Starting direct container scroll for element:",l.tagName);let h=l,v=!1,w=null,u=0,f=0;const T=e?n:-n;for(;h&&f<10;){const F=window.getComputedStyle(h),P=/(auto|scroll|overlay)/.test(F.overflowX),O=h.scrollWidth>h.clientWidth;if(console.log("[SCROLL DEBUG] Checking element:",h.tagName,"hasScrollableX:",P,"canScrollHorizontally:",O,"scrollWidth:",h.scrollWidth,"clientWidth:",h.clientWidth),P&&O){const R=h.scrollLeft,z=h.scrollWidth-h.clientWidth;let H=T/3;H>0?H=Math.min(H,z-R):H=Math.max(H,-R),h.scrollLeft=R+H;const D=h.scrollLeft,_=D-R;if(console.log("[SCROLL DEBUG] Scroll attempt:",h.tagName,"before:",R,"after:",D,"delta:",_),Math.abs(_)>.5){v=!0,w=h,u=_,console.log("[SCROLL DEBUG] Successfully scrolled container:",h.tagName,"delta:",_);break}}if(h===document.body||h===document.documentElement)break;h=h.parentElement,f++}return v?`Scrolled container (${w==null?void 0:w.tagName}) horizontally by ${u}px`:`No horizontally scrollable container found for element (${l.tagName})`}const i=e?n:-n,b=l=>l.clientWidth>=window.innerWidth*.5,d=l=>l&&/(auto|scroll|overlay)/.test(getComputedStyle(l).overflowX)&&l.scrollWidth>l.clientWidth&&b(l);let r=document.activeElement;for(;r&&!d(r)&&r!==document.body;)r=r.parentElement;if(r=d(r)?r:Array.from(document.querySelectorAll("*")).find(d)||document.scrollingElement||document.documentElement,r===document.scrollingElement||r===document.documentElement||r===document.body){const l=window.scrollX,h=document.documentElement.scrollWidth-window.innerWidth;window.scrollBy(i,0);const v=window.scrollX,w=v-l;if(Math.abs(w)<1)return i>0?"⚠️ Already at the right edge of the page, cannot scroll right further.":"⚠️ Already at the left edge of the page, cannot scroll left further.";const u=i>0&&v>=h-1,f=i<0&&v<=1;return u?`✅ Scrolled page by ${w}px. Reached the right edge of the page.`:f?`✅ Scrolled page by ${w}px. Reached the left edge of the page.`:`✅ Scrolled page horizontally by ${w}px.`}else{const l=r.scrollLeft,h=r.scrollWidth-r.clientWidth;r.scrollBy({left:i,behavior:"smooth"}),await waitFor(.1);const v=r.scrollLeft,w=v-l;if(Math.abs(w)<1)return i>0?`⚠️ Already at the right edge of container (${r.tagName}), cannot scroll right further.`:`⚠️ Already at the left edge of container (${r.tagName}), cannot scroll left further.`;const u=i>0&&v>=h-1,f=i<0&&v<=1;return u?`✅ Scrolled container (${r.tagName}) by ${w}px. Reached the right edge.`:f?`✅ Scrolled container (${r.tagName}) by ${w}px. Reached the left edge.`:`✅ Scrolled container (${r.tagName}) horizontally by ${w}px.`}}const VIEWPORT_EXPANSION=-1,domTree=(e={doHighlightElements:!0,focusHighlightIndex:-1,viewportExpansion:0,debugMode:!1,interactiveBlacklist:[],interactiveWhitelist:[],highlightOpacity:.1,highlightLabelOpacity:.5})=>{const{interactiveBlacklist:n,interactiveWhitelist:s,highlightOpacity:i,highlightLabelOpacity:b}=e,{doHighlightElements:d,focusHighlightIndex:r,viewportExpansion:l,debugMode:h}=e;let v=0;const w=new WeakMap;function u(t,o){!t||t.nodeType!==Node.ELEMENT_NODE||w.set(t,{...w.get(t),...o})}const f={boundingRects:new WeakMap,clientRects:new WeakMap,computedStyles:new WeakMap,clearCache:()=>{f.boundingRects=new WeakMap,f.clientRects=new WeakMap,f.computedStyles=new WeakMap}};function T(t){if(!t)return null;if(f.boundingRects.has(t))return f.boundingRects.get(t);const o=t.getBoundingClientRect();return o&&f.boundingRects.set(t,o),o}function F(t){if(!t)return null;if(f.computedStyles.has(t))return f.computedStyles.get(t);const o=window.getComputedStyle(t);return o&&f.computedStyles.set(t,o),o}function P(t){if(!t)return null;if(f.clientRects.has(t))return f.clientRects.get(t);const o=t.getClientRects();return o&&f.clientRects.set(t,o),o}const O={},R={current:0},z="playwright-highlight-container";function H(t,o,p=null){if(!t)return o;const a=[];let c=null,x=20,E=16,M=null;try{let m=document.getElementById(z);m||(m=document.createElement("div"),m.id=z,m.style.position="fixed",m.style.pointerEvents="none",m.style.top="0",m.style.left="0",m.style.width="100%",m.style.height="100%",m.style.zIndex="2147483640",m.style.backgroundColor="transparent",document.body.appendChild(m));const L=t.getClientRects();if(!L||L.length===0)return o;const A=["#FF0000","#00FF00","#0000FF","#FFA500","#800080","#008080","#FF69B4","#4B0082","#FF4500","#2E8B57","#DC143C","#4682B4"],g=o%A.length;let y=A[g];const k=y+Math.floor(i*255).toString(16).padStart(2,"0");y=y+Math.floor(b*255).toString(16).padStart(2,"0");let S={x:0,y:0};if(p){const I=p.getBoundingClientRect();S.x=I.left,S.y=I.top}const B=document.createDocumentFragment();for(const I of L){if(I.width===0||I.height===0)continue;const $=document.createElement("div");$.style.position="fixed",$.style.border=`2px solid ${y}`,$.style.backgroundColor=k,$.style.pointerEvents="none",$.style.boxSizing="border-box";const N=I.top+S.y,q=I.left+S.x;$.style.top=`${N}px`,$.style.left=`${q}px`,$.style.width=`${I.width}px`,$.style.height=`${I.height}px`,B.appendChild($),a.push({element:$,initialRect:I})}const G=L[0];c=document.createElement("div"),c.className="playwright-highlight-label",c.style.position="fixed",c.style.background=y,c.style.color="white",c.style.padding="1px 4px",c.style.borderRadius="4px",c.style.fontSize=`${Math.min(12,Math.max(8,G.height/2))}px`,c.textContent=o.toString(),x=c.offsetWidth>0?c.offsetWidth:x,E=c.offsetHeight>0?c.offsetHeight:E;const Y=G.top+S.y,Z=G.left+S.x;let j=Y+2,J=Z+G.width-x-2;(G.width<x+4||G.height<E+4)&&(j=Y-E-2,J=Z+G.width-x,J<S.x&&(J=Z)),j=Math.max(0,Math.min(j,window.innerHeight-E)),J=Math.max(0,Math.min(J,window.innerWidth-x)),c.style.top=`${j}px`,c.style.left=`${J}px`,B.appendChild(c);const it=((I,$)=>{let N=0;return(...q)=>{const V=performance.now();if(!(V-N<$))return N=V,I(...q)}})(()=>{const I=t.getClientRects();let $={x:0,y:0};if(p){const N=p.getBoundingClientRect();$.x=N.left,$.y=N.top}if(a.forEach((N,q)=>{if(q<I.length){const V=I[q],et=V.top+$.y,K=V.left+$.x;N.element.style.top=`${et}px`,N.element.style.left=`${K}px`,N.element.style.width=`${V.width}px`,N.element.style.height=`${V.height}px`,N.element.style.display=V.width===0||V.height===0?"none":"block"}else N.element.style.display="none"}),I.length<a.length)for(let N=I.length;N<a.length;N++)a[N].element.style.display="none";if(c&&I.length>0){const N=I[0],q=N.top+$.y,V=N.left+$.x;let et=q+2,K=V+N.width-x-2;(N.width<x+4||N.height<E+4)&&(et=q-E-2,K=V+N.width-x,K<$.x&&(K=V)),et=Math.max(0,Math.min(et,window.innerHeight-E)),K=Math.max(0,Math.min(K,window.innerWidth-x)),c.style.top=`${et}px`,c.style.left=`${K}px`,c.style.display="block"}else c&&(c.style.display="none")},16);return window.addEventListener("scroll",it,!0),window.addEventListener("resize",it),M=()=>{window.removeEventListener("scroll",it,!0),window.removeEventListener("resize",it),a.forEach(I=>I.element.remove()),c&&c.remove()},m.appendChild(B),o+1}finally{M&&(window._highlightCleanupFunctions=window._highlightCleanupFunctions||[]).push(M)}}function D(t){if(!t||t.nodeType!==Node.ELEMENT_NODE)return null;const o=F(t);if(!o)return null;const p=o.display;if(p==="inline"||p==="inline-block")return null;const a=o.overflowX,c=o.overflowY,x=a==="auto"||a==="scroll",E=c==="auto"||c==="scroll";if(!x&&!E)return null;const M=t.scrollWidth-t.clientWidth,m=t.scrollHeight-t.clientHeight,L=4;if(M<L&&m<L||!E&&M<L||!x&&m<L)return null;const A=t.scrollTop,g=t.scrollLeft,y=t.scrollWidth-t.clientWidth-t.scrollLeft,k=t.scrollHeight-t.clientHeight-t.scrollTop,S={top:A,right:y,bottom:k,left:g};return u(t,{scrollable:!0,scrollData:S}),S}function _(t){try{if(l===-1){const E=t.parentElement;if(!E)return!1;try{return E.checkVisibility({checkOpacity:!0,checkVisibilityCSS:!0})}catch{const m=window.getComputedStyle(E);return m.display!=="none"&&m.visibility!=="hidden"&&m.opacity!=="0"}}const o=document.createRange();o.selectNodeContents(t);const p=o.getClientRects();if(!p||p.length===0)return!1;let a=!1,c=!1;for(const E of p)if(E.width>0&&E.height>0&&(a=!0,!(E.bottom<-l||E.top>window.innerHeight+l||E.right<-l||E.left>window.innerWidth+l))){c=!0;break}if(!a||!c)return!1;const x=t.parentElement;if(!x)return!1;try{return x.checkVisibility({checkOpacity:!0,checkVisibilityCSS:!0})}catch{const M=window.getComputedStyle(x);return M.display!=="none"&&M.visibility!=="hidden"&&M.opacity!=="0"}}catch(o){return console.warn("Error checking text node visibility:",o),!1}}function rt(t){if(!t||!t.tagName)return!1;const o=new Set(["body","div","main","article","section","nav","header","footer"]),p=t.tagName.toLowerCase();return o.has(p)?!0:!new Set(["svg","script","style","link","meta","noscript","template"]).has(p)}function U(t){const o=F(t);return t.offsetWidth>0&&t.offsetHeight>0&&(o==null?void 0:o.visibility)!=="hidden"&&(o==null?void 0:o.display)!=="none"}function C(t){var k,S;if(!t||t.nodeType!==Node.ELEMENT_NODE||n.includes(t))return!1;if(s.includes(t))return!0;const o=t.tagName.toLowerCase(),p=F(t),a=new Set(["pointer","move","text","grab","grabbing","cell","copy","alias","all-scroll","col-resize","context-menu","crosshair","e-resize","ew-resize","help","n-resize","ne-resize","nesw-resize","ns-resize","nw-resize","nwse-resize","row-resize","s-resize","se-resize","sw-resize","vertical-text","w-resize","zoom-in","zoom-out"]),c=new Set(["not-allowed","no-drop","wait","progress","initial","inherit"]);function x(B){return B.tagName.toLowerCase()==="html"?!1:!!(p!=null&&p.cursor&&a.has(p.cursor))}if(x(t))return!0;const M=new Set(["a","button","input","select","textarea","details","summary","label","option","optgroup","fieldset","legend"]),m=new Set(["disabled","readonly"]);if(M.has(o)){if(p!=null&&p.cursor&&c.has(p.cursor))return!1;for(const B of m)if(t.hasAttribute(B)||t.getAttribute(B)==="true"||t.getAttribute(B)==="")return!1;return!(t.disabled||t.readOnly||t.inert)}const L=t.getAttribute("role"),A=t.getAttribute("aria-role");if(t.getAttribute("contenteditable")==="true"||t.isContentEditable||t.classList&&(t.classList.contains("button")||t.classList.contains("dropdown-toggle")||t.getAttribute("data-index")||t.getAttribute("data-toggle")==="dropdown"||t.getAttribute("aria-haspopup")==="true"))return!0;const g=new Set(["button","menu","menubar","menuitem","menuitemradio","menuitemcheckbox","radio","checkbox","tab","switch","slider","spinbutton","combobox","searchbox","textbox","listbox","option","scrollbar"]);if(M.has(o)||L&&g.has(L)||A&&g.has(A))return!0;try{if(typeof getEventListeners=="function"){const Y=getEventListeners(t),Z=["click","mousedown","mouseup","dblclick"];for(const j of Z)if(Y[j]&&Y[j].length>0)return!0}const B=((S=(k=t==null?void 0:t.ownerDocument)==null?void 0:k.defaultView)==null?void 0:S.getEventListenersForNode)||window.getEventListenersForNode;if(typeof B=="function"){const Y=B(t),Z=["click","mousedown","mouseup","keydown","keyup","submit","change","input","focus","blur"];for(const j of Z)for(const J of Y)if(J.type===j)return!0}const G=["onclick","onmousedown","onmouseup","ondblclick"];for(const Y of G)if(t.hasAttribute(Y)||typeof t[Y]=="function")return!0}catch{}return!!D(t)}function ot(t){if(l===-1)return!0;const o=P(t);if(!o||o.length===0)return!1;let p=!1;for(const m of o)if(m.width>0&&m.height>0&&!(m.bottom<-l||m.top>window.innerHeight+l||m.right<-l||m.left>window.innerWidth+l)){p=!0;break}if(!p)return!1;if(t.ownerDocument!==window.document)return!0;let c=Array.from(o).find(m=>m.width>0&&m.height>0);if(!c)return!1;const x=t.getRootNode();if(x instanceof ShadowRoot){const m=c.left+c.width/2,L=c.top+c.height/2;try{const A=x.elementFromPoint(m,L);if(!A)return!1;let g=A;for(;g&&g!==x;){if(g===t)return!0;g=g.parentElement}return!1}catch{return!0}}const E=5;return[{x:c.left+c.width/2,y:c.top+c.height/2},{x:c.left+E,y:c.top+E},{x:c.right-E,y:c.bottom-E}].some(({x:m,y:L})=>{try{const A=document.elementFromPoint(m,L);if(!A)return!1;let g=A;for(;g&&g!==document.documentElement;){if(g===t)return!0;g=g.parentElement}return!1}catch{return!0}})}function st(t,o){if(o===-1)return!0;const p=t.getClientRects();if(!p||p.length===0){const a=T(t);return!a||a.width===0||a.height===0?!1:!(a.bottom<-o||a.top>window.innerHeight+o||a.right<-o||a.left>window.innerWidth+o)}for(const a of p)if(!(a.width===0||a.height===0)&&!(a.bottom<-o||a.top>window.innerHeight+o||a.right<-o||a.left>window.innerWidth+o))return!0;return!1}function W(t){if(!t||t.nodeType!==Node.ELEMENT_NODE)return!1;const o=t.tagName.toLowerCase();return new Set(["a","button","input","select","textarea","details","summary","label"]).has(o)?!0:t.hasAttribute("onclick")||t.hasAttribute("role")||t.hasAttribute("tabindex")||t.hasAttribute("aria-")||t.hasAttribute("data-action")||t.getAttribute("contenteditable")==="true"}const X=new Set(["a","button","input","select","textarea","summary","details","label","option"]),Q=new Set(["button","link","menuitem","menuitemradio","menuitemcheckbox","radio","checkbox","tab","switch","slider","spinbutton","combobox","searchbox","textbox","listbox","option","scrollbar"]);function nt(t){if(!t||t.nodeType!==Node.ELEMENT_NODE||!U(t))return!1;const o=t.hasAttribute("role")||t.hasAttribute("tabindex")||t.hasAttribute("onclick")||typeof t.onclick=="function",p=/\b(btn|clickable|menu|item|entry|link)\b/i.test(t.className||""),a=!!t.closest('button,a,[role="button"],.menu,.dropdown,.list,.toolbar'),c=[...t.children].some(U),x=t.parentElement&&t.parentElement.isSameNode(document.body);return(C(t)||o||p)&&c&&a&&!x}function lt(t){var a,c;if(!t||t.nodeType!==Node.ELEMENT_NODE)return!1;const o=t.tagName.toLowerCase(),p=t.getAttribute("role");if(o==="iframe"||X.has(o)||p&&Q.has(p)||t.isContentEditable||t.getAttribute("contenteditable")==="true"||t.hasAttribute("data-testid")||t.hasAttribute("data-cy")||t.hasAttribute("data-test")||t.hasAttribute("onclick")||typeof t.onclick=="function")return!0;try{const x=((c=(a=t==null?void 0:t.ownerDocument)==null?void 0:a.defaultView)==null?void 0:c.getEventListenersForNode)||window.getEventListenersForNode;if(typeof x=="function"){const M=x(t),m=["click","mousedown","mouseup","keydown","keyup","submit","change","input","focus","blur"];for(const L of m)for(const A of M)if(A.type===L)return!0}if(["onmousedown","onmouseup","onkeydown","onkeyup","onsubmit","onchange","oninput","onfocus","onblur"].some(M=>t.hasAttribute(M)))return!0}catch{}return!!nt(t)}function ct(t,o,p,a){if(!t.isInteractive)return!1;let c=!1;return a?lt(o)?c=!0:c=!1:c=!0,c&&(t.isInViewport=st(o,l),(t.isInViewport||l===-1)&&(t.highlightIndex=v++,d))?(r>=0?r===t.highlightIndex&&H(o,t.highlightIndex,p):H(o,t.highlightIndex,p),!0):!1}function tt(t,o=null,p=!1){var E,M,m,L,A;if(!t||t.id===z||t.nodeType!==Node.ELEMENT_NODE&&t.nodeType!==Node.TEXT_NODE||!t||t.id===z||((E=t.dataset)==null?void 0:E.browserUseIgnore)==="true"||t.getAttribute&&t.getAttribute("aria-hidden")==="true")return null;if(t===document.body){const g={tagName:"body",attributes:{},xpath:"/body",children:[]};for(const k of t.childNodes){const S=tt(k,o,!1);S&&g.children.push(S)}const y=`${R.current++}`;return O[y]=g,y}if(t.nodeType!==Node.ELEMENT_NODE&&t.nodeType!==Node.TEXT_NODE)return null;if(t.nodeType===Node.TEXT_NODE){const g=(M=t.textContent)==null?void 0:M.trim();if(!g)return null;const y=t.parentElement;if(!y||y.tagName.toLowerCase()==="script")return null;const k=`${R.current++}`;return O[k]={type:"TEXT_NODE",text:g,isVisible:_(t)},k}if(t.nodeType===Node.ELEMENT_NODE&&!rt(t))return null;if(l!==-1&&!t.shadowRoot){const g=T(t),y=F(t),k=y&&(y.position==="fixed"||y.position==="sticky"),S=t.offsetWidth>0||t.offsetHeight>0;if(!g||!k&&!S&&(g.bottom<-l||g.top>window.innerHeight+l||g.right<-l||g.left>window.innerWidth+l))return null}const a={tagName:t.tagName.toLowerCase(),attributes:{},children:[]};if(W(t)||t.tagName.toLowerCase()==="iframe"||t.tagName.toLowerCase()==="body"){const g=((m=t.getAttributeNames)==null?void 0:m.call(t))||[];for(const y of g){const k=t.getAttribute(y);a.attributes[y]=k}t.tagName.toLowerCase()==="input"&&(t.type==="checkbox"||t.type==="radio")&&(a.attributes.checked=t.checked?"true":"false")}let c=!1;if(t.nodeType===Node.ELEMENT_NODE&&(a.isVisible=U(t),a.isVisible)){a.isTopElement=ot(t);const g=t.getAttribute("role"),y=g==="menu"||g==="menubar"||g==="listbox";(a.isTopElement||y)&&(a.isInteractive=C(t),c=ct(a,t,o,p),a.ref=t)}if(t.tagName){const g=t.tagName.toLowerCase();if(g==="iframe")try{const y=t.contentDocument||((L=t.contentWindow)==null?void 0:L.document);if(y)for(const k of y.childNodes){const S=tt(k,t,!1);S&&a.children.push(S)}}catch(y){console.warn("Unable to access iframe:",y)}else if(t.isContentEditable||t.getAttribute("contenteditable")==="true"||t.id==="tinymce"||t.classList.contains("mce-content-body")||g==="body"&&((A=t.getAttribute("data-id"))!=null&&A.startsWith("mce_")))for(const y of t.childNodes){const k=tt(y,o,c);k&&a.children.push(k)}else{if(t.shadowRoot){a.shadowRoot=!0;for(const y of t.shadowRoot.childNodes){const k=tt(y,o,c);k&&a.children.push(k)}}for(const y of t.childNodes){const S=tt(y,o,c||p);S&&a.children.push(S)}}}if(a.tagName==="a"&&a.children.length===0&&!a.attributes.href){const g=T(t);if(!(g&&g.width>0&&g.height>0||t.offsetWidth>0||t.offsetHeight>0))return null}a.extra=w.get(t)||null;const x=`${R.current++}`;return O[x]=a,x}const at=tt(document.body);return f.clearCache(),{rootId:at,map:O}},newElementsCache=new WeakMap;function getFlatTree(e){const n=[];for(const d of e.interactiveBlacklist||[])typeof d=="function"?n.push(d()):n.push(d);const s=[];for(const d of e.interactiveWhitelist||[])typeof d=="function"?s.push(d()):s.push(d);const i=domTree({doHighlightElements:!0,debugMode:!0,focusHighlightIndex:-1,viewportExpansion:VIEWPORT_EXPANSION,interactiveBlacklist:n,interactiveWhitelist:s,highlightOpacity:e.highlightOpacity??0,highlightLabelOpacity:e.highlightLabelOpacity??.1}),b=window.location.href;for(const d in i.map){const r=i.map[d];if(r.isInteractive&&r.ref){const l=r.ref;newElementsCache.has(l)||(newElementsCache.set(l,b),r.isNew=!0)}}return i}function flatTreeToString(e,n){const s=["title","type","checked","name","role","value","placeholder","data-date-format","alt","aria-label","aria-expanded","data-state","aria-checked","id","for","target","aria-haspopup","aria-controls","aria-owns"],i=[...n||[],...s],b=(u,f)=>u.length>f?u.substring(0,f)+"...":u,d=u=>{const f=e.map[u];if(!f)return null;if(f.type==="TEXT_NODE"){const T=f;return{type:"text",text:T.text,isVisible:T.isVisible,parent:null,children:[]}}else{const T=f,F=[];if(T.children)for(const P of T.children){const O=d(P);O&&(O.parent=null,F.push(O))}return{type:"element",tagName:T.tagName,attributes:T.attributes??{},isVisible:T.isVisible??!1,isInteractive:T.isInteractive??!1,isTopElement:T.isTopElement??!1,isNew:T.isNew??!1,highlightIndex:T.highlightIndex,parent:null,children:F,extra:T.extra??{}}}},r=(u,f=null)=>{u.parent=f;for(const T of u.children)r(T,u)},l=d(e.rootId);if(!l)return"";r(l);const h=u=>{let f=u.parent;for(;f;){if(f.type==="element"&&f.highlightIndex!==void 0)return!0;f=f.parent}return!1},v=(u,f,T)=>{var O,R,z,H;let F=f;const P=" ".repeat(f);if(u.type==="element"){if(u.highlightIndex!==void 0){F+=1;const D=getAllTextTillNextClickableElement(u);let _="";if(i.length>0&&u.attributes){const C={};for(const W of i){const X=u.attributes[W];X&&X.trim()!==""&&(C[W]=X.trim())}const ot=i.filter(W=>W in C);if(ot.length>1){const W=new Set,X={};for(const Q of ot){const nt=C[Q];nt.length>5&&(nt in X?W.add(Q):X[nt]=Q)}for(const Q of W)delete C[Q]}C.role===u.tagName&&delete C.role;const st=["aria-label","placeholder","title"];for(const W of st)C[W]&&C[W].toLowerCase().trim()===D.toLowerCase().trim()&&delete C[W];Object.keys(C).length>0&&(_=Object.entries(C).map(([W,X])=>`${W}=${b(X,20)}`).join(" "))}const rt=u.isNew?`*[${u.highlightIndex}]`:`[${u.highlightIndex}]`;let U=`${P}${rt}<${u.tagName??""}`;if(_&&(U+=` ${_}`),u.extra&&u.extra.scrollable){let C="";(O=u.extra.scrollData)!=null&&O.left&&(C+=`left=${u.extra.scrollData.left}, `),(R=u.extra.scrollData)!=null&&R.top&&(C+=`top=${u.extra.scrollData.top}, `),(z=u.extra.scrollData)!=null&&z.right&&(C+=`right=${u.extra.scrollData.right}, `),(H=u.extra.scrollData)!=null&&H.bottom&&(C+=`bottom=${u.extra.scrollData.bottom}`),U+=` data-scrollable="${C}"`}if(D){const C=D.trim();_||(U+=" "),U+=`>${C}`}else _||(U+=" ");U+=" />",T.push(U)}for(const D of u.children)v(D,F,T)}else if(u.type==="text"){if(h(u))return;u.parent&&u.parent.type==="element"&&u.parent.isVisible&&u.parent.isTopElement&&T.push(`${P}${u.text??""}`)}},w=[];return v(l,0,w),w.join(`
2
+ `)}const getAllTextTillNextClickableElement=(e,n=-1)=>{const s=[],i=(b,d)=>{if(!(n!==-1&&d>n)&&!(b.type==="element"&&b!==e&&b.highlightIndex!==void 0)){if(b.type==="text"&&b.text)s.push(b.text);else if(b.type==="element")for(const r of b.children)i(r,d+1)}};return i(e,0),s.join(`
3
+ `).trim()};function getSelectorMap(e){const n=new Map,s=Object.keys(e.map);for(const i of s){const b=e.map[i];b.isInteractive&&typeof b.highlightIndex=="number"&&n.set(b.highlightIndex,b)}return n}function getElementTextMap(e){const n=e.split(`
4
+ `).map(i=>i.trim()).filter(i=>i.length>0),s=new Map;for(const i of n){const d=/^\[(\d+)\]<[^>]+>([^<]*)/.exec(i);if(d){const r=parseInt(d[1],10);s.set(r,i)}}return s}function cleanUpHighlights(){const e=window._highlightCleanupFunctions||[];for(const n of e)typeof n=="function"&&n();window._highlightCleanupFunctions=[]}window.addEventListener("popstate",()=>{cleanUpHighlights()});window.addEventListener("hashchange",()=>{cleanUpHighlights()});window.addEventListener("beforeunload",()=>{cleanUpHighlights()});const navigation=window.navigation;if(navigation&&typeof navigation.addEventListener=="function")navigation.addEventListener("navigate",()=>{cleanUpHighlights()});else{let e=window.location.href;setInterval(()=>{window.location.href!==e&&(e=window.location.href,cleanUpHighlights())},500)}function getPageInfo(){const e=window.innerWidth,n=window.innerHeight,s=Math.max(document.documentElement.scrollWidth,document.body.scrollWidth||0),i=Math.max(document.documentElement.scrollHeight,document.body.scrollHeight||0),b=window.scrollX||window.pageXOffset||document.documentElement.scrollLeft||0,d=window.scrollY||window.pageYOffset||document.documentElement.scrollTop||0,r=Math.max(0,i-(window.innerHeight+d)),l=Math.max(0,s-(window.innerWidth+b));return{viewport_width:e,viewport_height:n,page_width:s,page_height:i,scroll_x:b,scroll_y:d,pixels_above:d,pixels_below:r,pages_above:n>0?d/n:0,pages_below:n>0?r/n:0,total_pages:n>0?i/n:0,current_page_position:d/Math.max(1,i-n),pixels_left:b,pixels_right:l}}function patchReact(e){const n=document.querySelectorAll('[data-reactroot], [data-reactid], [data-react-checksum], #root, #app, [id^="root-"], [id^="app-"], #adex-wrapper, #adex-root');for(const s of n)s.setAttribute("data-page-agent-not-interactive","true")}class PageController extends EventTarget{constructor(e={}){super(),this.flatTree=null,this.selectorMap=new Map,this.elementTextMap=new Map,this.simplifiedHTML="<EMPTY>",this.lastTimeUpdate=0,this.isIndexed=!1,this.mask=null,this.maskReady=null,this.config=e,patchReact(),e.enableMask&&this.initMask()}initMask(){this.maskReady===null&&(this.maskReady=(async()=>{const{SimulatorMask:e}=await Promise.resolve().then(()=>require("./SimulatorMask-DoBLczoQ.cjs"));this.mask=new e})())}async getCurrentUrl(){return window.location.href}async getLastUpdateTime(){return this.lastTimeUpdate}async getBrowserState(){const e=window.location.href,n=document.title,s=getPageInfo(),i=this.config.viewportExpansion??VIEWPORT_EXPANSION;await this.updateTree();const b=this.simplifiedHTML,d=`Current Page: [${n}](${e})`,r=`Page info: ${s.viewport_width}x${s.viewport_height}px viewport, ${s.page_width}x${s.page_height}px total page size, ${s.pages_above.toFixed(1)} pages above, ${s.pages_below.toFixed(1)} pages below, ${s.total_pages.toFixed(1)} total pages, at ${(s.current_page_position*100).toFixed(0)}% of page`,l=i===-1?"Interactive elements from top layer of the current page (full page):":"Interactive elements from top layer of the current page inside the viewport:",v=s.pixels_above>4&&i!==-1?`... ${s.pixels_above} pixels above (${s.pages_above.toFixed(1)} pages) - scroll to see more ...`:"[Start of page]",w=`${d}
5
+ ${r}
6
+
7
+ ${l}
8
+
9
+ ${v}`,f=s.pixels_below>4&&i!==-1?`... ${s.pixels_below} pixels below (${s.pages_below.toFixed(1)} pages) - scroll to see more ...`:"[End of page]";return{url:e,title:n,header:w,content:b,footer:f}}async updateTree(){this.dispatchEvent(new Event("beforeUpdate")),this.lastTimeUpdate=Date.now(),this.mask&&(this.mask.wrapper.style.pointerEvents="none"),cleanUpHighlights();const e=[...this.config.interactiveBlacklist||[],...document.querySelectorAll("[data-page-agent-not-interactive]").values()];return this.flatTree=getFlatTree({...this.config,interactiveBlacklist:e}),this.simplifiedHTML=flatTreeToString(this.flatTree,this.config.include_attributes),this.selectorMap.clear(),this.selectorMap=getSelectorMap(this.flatTree),this.elementTextMap.clear(),this.elementTextMap=getElementTextMap(this.simplifiedHTML),this.isIndexed=!0,this.mask&&(this.mask.wrapper.style.pointerEvents="auto"),this.dispatchEvent(new Event("afterUpdate")),this.simplifiedHTML}async cleanUpHighlights(){cleanUpHighlights()}assertIndexed(){if(!this.isIndexed)throw new Error("DOM tree not indexed yet. Can not perform actions on elements.")}async clickElement(e){try{this.assertIndexed();const n=getElementByIndex(this.selectorMap,e),s=this.elementTextMap.get(e);return await clickElement(n),n instanceof HTMLAnchorElement&&n.target==="_blank"?{success:!0,message:`✅ Clicked element (${s??e}). ⚠️ Link opens in a new tab. You are not capable of reading new tabs.`}:{success:!0,message:`✅ Clicked element (${s??e}).`}}catch(n){return{success:!1,message:`❌ Failed to click element: ${n}`}}}async inputText(e,n){try{this.assertIndexed();const s=getElementByIndex(this.selectorMap,e),i=this.elementTextMap.get(e);return await inputTextElement(s,n),{success:!0,message:`✅ Input text (${n}) into element (${i??e}).`}}catch(s){return{success:!1,message:`❌ Failed to input text: ${s}`}}}async selectOption(e,n){try{this.assertIndexed();const s=getElementByIndex(this.selectorMap,e),i=this.elementTextMap.get(e);return await selectOptionElement(s,n),{success:!0,message:`✅ Selected option (${n}) in element (${i??e}).`}}catch(s){return{success:!1,message:`❌ Failed to select option: ${s}`}}}async scroll(e){try{const{down:n,numPages:s,pixels:i,index:b}=e;this.assertIndexed();const d=i??s*(n?1:-1)*window.innerHeight,r=b!==void 0?getElementByIndex(this.selectorMap,b):null;return{success:!0,message:await scrollVertically(n,d,r)}}catch(n){return{success:!1,message:`❌ Failed to scroll: ${n}`}}}async scrollHorizontally(e){try{const{right:n,pixels:s,index:i}=e;this.assertIndexed();const b=s*(n?1:-1),d=i!==void 0?getElementByIndex(this.selectorMap,i):null;return{success:!0,message:await scrollHorizontally(n,b,d)}}catch(n){return{success:!1,message:`❌ Failed to scroll horizontally: ${n}`}}}async executeJavascript(script){try{const asyncFunction=eval(`(async () => { ${script} })`),result=await asyncFunction();return{success:!0,message:`✅ Executed JavaScript. Result: ${result}`}}catch(e){return{success:!1,message:`❌ Error executing JavaScript: ${e}`}}}async showMask(){var e;await this.maskReady,(e=this.mask)==null||e.show()}async hideMask(){var e;await this.maskReady,(e=this.mask)==null||e.hide()}dispose(){var e;cleanUpHighlights(),this.flatTree=null,this.selectorMap.clear(),this.elementTextMap.clear(),this.simplifiedHTML="<EMPTY>",this.isIndexed=!1,(e=this.mask)==null||e.dispose(),this.mask=null}}exports.PageController=PageController;