(function(){let m=()=>{try{return!!(chrome.runtime&&chrome.runtime.id)}catch{return!1}};if(!m()||window.top!==window||window.__tabbyOverlay)return;window.__tabbyOverlay=!0;let t=null,d=null,u=!1,b=!1,f=null,a="";try{a=chrome.runtime.getURL("").replace(/\/$/,"")}catch{}function L(e){if(b&&t)try{t.contentWindow?.postMessage(e,a)}catch{}else f=e}let s=!1,v=0,x=0,M=0,E=0;function y(){if(!u){u=!0;try{d&&chrome.storage?.onChanged&&chrome.storage.onChanged.removeListener(d)}catch{}try{t&&t.remove()}catch{}}}function C(){t||u||(t=document.createElement("iframe"),t.id="tabby-frame",t.src=chrome.runtime.getURL("overlay-frame.html"),t.style.cssText="position:fixed;z-index:2147483647;right:6px;bottom:6px;width:150px;height:180px;border:0;margin:0;padding:0;background:transparent;",t.addEventListener("load",()=>{if(b=!0,f){try{t.contentWindow?.postMessage(f,a)}catch{}f=null}requestAnimationFrame(g)}),(document.documentElement||document.body).appendChild(t))}function A(e,o){if(!t)return;let n=t.getBoundingClientRect();v=e,x=o,M=n.left+e,E=n.top+o,t.style.right="",t.style.bottom="",t.style.left=n.left+"px",t.style.top=n.top+"px",s=!0,t.style.pointerEvents="none",document.body.style.cursor="grabbing",document.addEventListener("mousemove",T),document.addEventListener("mouseup",_)}function T(e){if(!s||!t)return;let o=window.innerWidth,n=window.innerHeight,i=t.offsetWidth,r=t.offsetHeight,c=Math.max(0,Math.min(e.clientX-v,o-i)),w=Math.max(0,Math.min(e.clientY-x,n-r));t.style.left=c+"px",t.style.top=w+"px"}function _(e){if(!s)return;if(s=!1,document.removeEventListener("mousemove",T),document.removeEventListener("mouseup",_),t.style.pointerEvents="",document.body.style.cursor="",Math.hypot(e.clientX-M,e.clientY-E)>5){let n=parseInt(t.style.left),i=parseInt(t.style.top);try{chrome.runtime.sendMessage({type:"SET_POS",pos:{left:n,top:i}}).catch(()=>{})}catch{}}else try{t.contentWindow?.postMessage({type:"TABBY_CLICKED"},a)}catch{}}window.addEventListener("message",e=>{!t||e.source!==t.contentWindow||e.data?.type==="TABBY_DRAG_START"&&A(e.data.mouseX,e.data.mouseY)});async function p(){if(!m()){y();return}C();let e=!1;try{let o=await chrome.storage.local.get("tabby");e=!!(o.tabby&&o.tabby.overlayHidden);let n=o.tabby?.overlayPos;n&&t&&!s&&(t.style.right="",t.style.bottom="",t.style.left=n.left+"px",t.style.top=n.top+"px")}catch{y();return}t&&(t.style.display=e?"none":""),requestAnimationFrame(g)}let l=null;d=(e,o)=>{if(!m()){y();return}o==="local"&&e.tabby&&(clearTimeout(l),l=setTimeout(()=>{l=null,p();try{t&&t.contentWindow?.postMessage({type:"TABBY_SYNC"},a)}catch{}},300))};try{chrome.storage.onChanged.addListener(d)}catch{}document.addEventListener("visibilitychange",()=>{if(!document.hidden){clearTimeout(l),l=null,p();try{t&&t.contentWindow?.postMessage({type:"TABBY_SYNC"},a)}catch{}}}),chrome.runtime.onMessage.addListener(e=>{e?.type==="SITE_REACTION"&&L({type:"SITE_REACTION",line:e.line,anim:e.anim||null})});let h=null;function g(){if(!t||t.style.display==="none"||s)return;let e=window.innerWidth,o=window.innerHeight,n=t.offsetWidth,i=t.offsetHeight;if(!(n===0||i===0))if(t.style.left){let r=Math.max(0,Math.min(parseInt(t.style.left),e-n)),c=Math.max(0,Math.min(parseInt(t.style.top),o-i));t.style.left=r+"px",t.style.top=c+"px"}else{let r=t.getBoundingClientRect();if(r.right>e||r.bottom>o||r.left<0||r.top<0){let c=Math.max(0,Math.min(r.left,e-n)),w=Math.max(0,Math.min(r.top,o-i));t.style.right="",t.style.bottom="",t.style.left=c+"px",t.style.top=w+"px"}}}window.addEventListener("resize",()=>{h||(h=setTimeout(()=>{h=null,g()},200))}),p()})();
