(function(){"use strict";const C={debug:0,info:1,warn:2,error:3,none:4};let k=3;function $(e){if(!e)return"";const t=[];for(const[r,n]of Object.entries(e)){let o;if(n===null)o="null";else if(n===void 0)o="undefined";else if(typeof n=="string")o=n;else if(typeof n=="number"||typeof n=="boolean")o=String(n);else try{o=JSON.stringify(n)}catch{o="[Circular]"}t.push(`${r}=${o}`)}return t.length>0?` ${t.join(" ")}`:""}function g(e,t,r){if(e<k)return;const n=$(r),o=`[extension-js][content-script] ${t}${n}`;e>=3?console.error(o):e===2?console.warn(o):console.log(o)}const b={debug:(e,t)=>{g(0,e,t)},info:(e,t)=>{g(1,e,t)},warn:(e,t)=>{g(2,e,t)},error:(e,t)=>{g(3,e,t)}};if(window.__jsNotebookSetLogLevel=e=>{k=C[e]??3},window.__jsNotebookContentScriptInjected)throw new Error("Content script already injected");window.__jsNotebookContentScriptInjected=!0;function f(e){return document.querySelector(`[data-ref-id='${CSS.escape(String(e))}']`)}function y(e){var n,o,i;const t=e.toLowerCase().trim();if(!t)return null;const r=Array.from(document.querySelectorAll('input, textarea, select, button, a, [role="button"], [role="link"]'));for(const l of r){const u=l.getAttribute("aria-label");if(u&&u.toLowerCase().trim()===t)return l;const c=l.placeholder;if(c&&c.toLowerCase().trim()===t)return l;const d=l.id;if(d){const w=document.querySelector(`label[for='${CSS.escape(d)}']`);if(w&&((n=w.textContent)==null?void 0:n.trim().toLowerCase())===t)return l}const a=l.closest("label");if(a&&((o=a.textContent)==null?void 0:o.trim().toLowerCase())===t||(((i=l.textContent)==null?void 0:i.trim().toLowerCase())||"")===t)return l}return null}function p(e){var o;const t=e.toLowerCase().trim(),r=new Set,n=Array.from(document.querySelectorAll('input, textarea, select, button, a, [role="button"], [role="link"]'));for(const i of n){const l=i.getAttribute("aria-label");l&&r.add(l.trim());const u=i.placeholder;u&&r.add(u.trim());const c=((o=i.textContent)==null?void 0:o.trim())||"";c&&r.add(c)}return Array.from(r).filter(i=>i.toLowerCase().includes(t)).slice(0,5)}function h(e){return typeof e=="object"&&e!==null&&!Array.isArray(e)?e:{}}function s(e,t){const r=h(e)[t];return typeof r=="string"?r:""}function I(e,t,r){const n=h(e)[t];return typeof n=="number"?n:r}function v(e){const t=e.tagName.toLowerCase(),r=e.getAttribute("role");if(r)return r;if(t==="button"||t==="input"&&e.type==="submit")return"button";if(t==="a")return"link";if(t==="input"){const n=e.type;if(n==="text"||n==="email"||n==="password"||n==="search")return"textbox";if(n==="checkbox")return"checkbox";if(n==="radio")return"radio";if(n==="submit"||n==="button")return"button"}return t==="textarea"?"textbox":t==="select"?"combobox":t==="img"?"img":t==="h1"||t==="h2"||t==="h3"||t==="h4"||t==="h5"||t==="h6"?"heading":t==="li"?"listitem":t==="ul"||t==="ol"?"list":t==="table"?"table":t==="tr"?"row":t==="td"||t==="th"?"cell":t==="nav"?"navigation":t==="main"?"main":t==="article"?"article":t==="section"?"region":t==="aside"?"complementary":t==="form"?"form":t==="dialog"||t==="modal"?"dialog":t==="figure"?"figure":t==="figcaption"?"caption":e.getAttribute("onclick")||e.onclick?"button":"generic"}function S(e){var l,u;const t=e.getAttribute("aria-label");if(t)return t;const r=e.getAttribute("aria-labelledby");if(r){const c=document.getElementById(r);if(c)return((l=c.textContent)==null?void 0:l.slice(0,60))||""}if(e.tagName.toLowerCase()==="img"){const c=e.getAttribute("alt");if(c)return c}const o=e.title;if(o)return o;const i=v(e);return i!=="generic"&&i!=="list"&&i!=="table"&&i!=="row"&&i!=="region"&&i!=="navigation"&&i!=="main"&&((u=e.textContent)==null?void 0:u.trim().slice(0,60))||""}function _(e){const t=v(e);if(t==="generic"||t==="presentation"||t==="none"||e.hidden)return!1;const r=window.getComputedStyle(e);return!(r.display==="none"||r.visibility==="hidden")}function A(e){let t=1;const r=[],n=[];function o(l,u){if(r.length>=e)return;const c=l.tagName.toLowerCase();if(c==="script"||c==="style"||c==="noscript"||c==="template")return;const d=_(l);let a=u;if(d){const m=t++;l.setAttribute("data-ref-id",String(m));const w=v(l),E=S(l),L={refId:m,role:w,tag:c};E&&(L.name=E),r.push(L);const x=[`${"  ".repeat(u)}- ${w}`];E&&x.push(`"${E.replace(/"/g,'\\"')}"`),x.push(`[ref=${m}]`),n.push(x.join(" ")),a=u+1}for(const m of l.children)o(m,a)}return document.body&&o(document.body,0),{text:[`URL: ${window.location.href}`,`Title: ${document.title}`,""].concat(n).join(`
`),nodes:r,url:window.location.href,title:document.title,viewport:{width:window.innerWidth,height:window.innerHeight}}}const T={click:e=>{const t=s(e,"refId"),r=s(e,"label");let n=t?f(t):null;if(!n&&r&&(n=y(r)),!n){const o=r||t,i=o?p(o):[];throw new Error(`Element not found${o?` by label: "${o}"`:""}. Candidates: ${i.join(", ")||"none"}`)}return n.click(),null},fill:e=>{const t=s(e,"refId"),r=s(e,"label"),n=s(e,"value");let o=t?f(t):null;if(!o&&r&&(o=y(r)),!o){const i=r||t,l=i?p(i):[];throw new Error(`Element not found${i?` by label: "${i}"`:""}. Candidates: ${l.join(", ")||"none"}`)}if(o instanceof HTMLInputElement||o instanceof HTMLTextAreaElement){o.value=n;const i=new InputEvent("input",{bubbles:!0});return o.dispatchEvent(i),null}throw new Error("Element is not an input")},type:e=>{const t=s(e,"refId"),r=s(e,"label"),n=s(e,"text");let o=t?f(t):null;if(!o&&r&&(o=y(r)),!o){const i=r||t,l=i?p(i):[];throw new Error(`Element not found${i?` by label: "${i}"`:""}. Candidates: ${l.join(", ")||"none"}`)}if(o instanceof HTMLInputElement||o instanceof HTMLTextAreaElement){o.value=n;const i=new InputEvent("input",{bubbles:!0});return o.dispatchEvent(i),null}throw new Error("Element is not an input")},append:e=>{const t=s(e,"refId"),r=s(e,"label"),n=s(e,"text");let o=t?f(t):null;if(!o&&r&&(o=y(r)),!o){const i=r||t,l=i?p(i):[];throw new Error(`Element not found${i?` by label: "${i}"`:""}. Candidates: ${l.join(", ")||"none"}`)}if(o instanceof HTMLInputElement||o instanceof HTMLTextAreaElement){o.value+=n;const i=new InputEvent("input",{bubbles:!0});return o.dispatchEvent(i),null}throw new Error("Element is not an input")},press:e=>{const t=s(e,"key"),r=new KeyboardEvent("keydown",{key:t,bubbles:!0});document.dispatchEvent(r);const n=new KeyboardEvent("keyup",{key:t,bubbles:!0});return document.dispatchEvent(n),null},select:e=>{const t=s(e,"refId"),r=s(e,"value"),n=t?f(t):null;if(!n)throw new Error(`Element ${t} not found`);if(n instanceof HTMLSelectElement)return n.value=r,null;throw new Error("Element is not a select")},check:e=>{const t=s(e,"refId"),r=(()=>{const o=h(e);return typeof o.checked=="boolean"?o.checked:!0})(),n=t?f(t):null;if(!n)throw new Error(`Element ${t} not found`);if(n instanceof HTMLInputElement&&n.type==="checkbox")return n.checked=r,null;throw new Error("Element is not a checkbox")},hover:e=>{const t=s(e,"refId"),r=t?f(t):null;if(!r)throw new Error(`Element ${t} not found`);const n=new MouseEvent("mouseenter",{bubbles:!0});return r.dispatchEvent(n),null},unhover:()=>{const e=new MouseEvent("mouseleave",{bubbles:!0});return document.body.dispatchEvent(e),null},scroll:e=>{const t=h(e),r=t.direction??"down",n=typeof t.amount=="number"?t.amount:300;return window.scrollBy({top:r==="down"?n:-n,behavior:"smooth"}),!0},dblclick:e=>{const t=s(e,"refId"),r=t?f(t):null;if(!r)throw new Error(`Element ${t} not found`);const n=new MouseEvent("dblclick",{bubbles:!0});return r.dispatchEvent(n),null},forward:()=>(window.history.forward(),!0),reload:()=>(window.location.reload(),!0),scrollTo:e=>{const t=s(e,"refId"),r=I(e,"x",0),n=I(e,"y",0);if(t){const o=f(t);if(o)return o.scrollIntoView({behavior:"smooth"}),!0;throw new Error(`Element ${t} not found`)}return window.scrollTo({top:n,left:r,behavior:"smooth"}),!0},evaluate:e=>{const t=s(e,"code");if(typeof t!="string")throw new Error("evaluate requires a string argument");return new Function(t)()},back:()=>(window.history.back(),!0),ping:()=>({ok:!0}),snapshot:async e=>{const t=h(e),r=typeof t.max_nodes=="number"?t.max_nodes:500;b.debug("snapshot",{maxNodes:r,hasBody:!!document.body});const n=A(r);return b.debug("snapshot_result",{nodeCount:n.nodes.length}),n},fetch:async e=>{const t=h(e),r=t.url,n=(t.method||"GET").toString().toUpperCase(),o=t.headers||{},i=t.body??null,l=typeof t.timeout=="number"?t.timeout:3e4,u=new AbortController,c=setTimeout(()=>u.abort(),l||3e4);try{const d={method:n||"GET",headers:typeof o=="object"&&o!==null?o:{},signal:u.signal};i!=null&&(d.body=typeof i=="string"?i:String(i));const a=await fetch(r,d);clearTimeout(c);const m=await a.text();return{status:a.status,ok:a.ok,headers:Object.fromEntries(a.headers.entries()),body:m}}catch(d){throw clearTimeout(c),d}}};chrome.runtime.onMessage.addListener((e,t,r)=>{const n=e==null?void 0:e.action;b.debug("received",{action:n,hasParams:!!(e!=null&&e.params)});const o=T[n];if(!o)return b.debug("no_handler",{action:n}),r({ok:!1,error:`Unknown content script action: ${n}`}),!1;try{const i=o(e==null?void 0:e.params);return i instanceof Promise?(i.then(l=>{b.debug("async_response",{action:n,resultType:typeof l}),r(l)}).catch(l=>{const u=l instanceof Error?l.message:String(l);b.debug("async_error",{action:n,error:u}),r({ok:!1,error:u||String(l)})}),!0):(b.debug("sync_response",{action:n,resultType:typeof i}),r(i),!1)}catch(i){const l=i instanceof Error?i.message:String(i);return b.debug("sync_error",{action:n,error:l}),r({ok:!1,error:l||String(i)}),!1}})})();
