(()=>{const t=document.createElement("mini-popup");function e(){t.listOfActivePopups.forEach((e=>{t.shadowRoot.getElementById(`${e}grabbable`).addEventListener("mousedown",i,!1)}))}let o,n;function i(e){if(e.target.id.includes("temperature"))return;e.preventDefault();const i=this.id;o=void 0,n=void 0;const s=e=>{!function(e,i){const s=t.shadowRoot.getElementById(i.replace("grabbable",""));o||n||(o=e.clientX-s.offsetLeft,n=e.clientY-s.offsetTop);const r=e.clientY-n,u=e.clientX-o;s.style.top=`${r}px`,s.style.left=`${u}px`}(e,i)};window.addEventListener("mousemove",s,!0),window.addEventListener("mouseup",(()=>{window.removeEventListener("mousemove",s,!0)}),!1)}function s(e,o){t.setAttribute(e,JSON.stringify(o))}document.body.appendChild(t),window.addEventListener("scroll",(function(){const e=window.scrollX,o=window.scrollY;t.listOfUnpinnedPopups.forEach((n=>{const i=t.shadowRoot.getElementById(`pin${n}`);i.hasAttribute("hidden")&&i.removeAttribute("hidden");const s=t.shadowRoot.getElementById(parseInt(n)),r=s.offsetTop-(o-this.window.lastY),u=s.offsetLeft-(e-this.window.lastX);s.style.top=`${r}px`,s.style.left=`${u}px`})),this.window.lastY=o,this.window.lastX=e})),document.addEventListener("contextmenu",(()=>{window.getSelection().toString().length>0&&s("mousePosition_primary",function(){const t=window.getSelection().getRangeAt(0).getBoundingClientRect();return{left:t.right+5,top:t.top}}())})),document.addEventListener("contextmenu",(function(t){var e=function(t){const{clientX:e,clientY:o}=t||window.event;return{x:e,y:o}}(t);s("mousePosition_support",{left:e.x,top:e.y})}));const r={};function u(e,o,n){try{if("[DONE]"===e)return void t.updatepopup(n,o,!1);e=e.replace(/\n/g,"");const i=JSON.parse(e);t.updatepopup(i,o,!0)}catch(t){console.error("Failed to parse JSON object:",t),console.log(e)}}chrome.runtime.onMessage.addListener(((o,n,i)=>{if("shouldReenableContextMenu"===o.greeting)return void i({farewell:"yes"});if(o.getSelection)return void i({selection:window.getSelection().toString()});const s=void 0===(d=o.id_popup)||-1===d?t.ids:parseInt(d);var d;const p=()=>{t.ids+=1,t.listOfActivePopups.push(t.ids),t.listOfUnpinnedPopups.push(t.ids),t.hasAttribute("mousePosition_primary")?(t.setAttribute("mousePosition",t.getAttribute("mousePosition_primary")),t.removeAttribute("mousePosition_primary")):t.hasAttribute("mousePosition_support")?(t.setAttribute("mousePosition",t.getAttribute("mousePosition_support")),t.removeAttribute("mousePosition_support")):t.setAttribute("mousePosition",JSON.stringify({left:0,top:0}))};switch(o.message){case"showPopUp":p(),t.defaultpopup(),e();break;case"showPopUpOnTheFly":p(),t.ontheflypopup(o.text,o.bodyData,o.cursorPosition),e();break;case"showPopUpChatGPT":p(),t.chatGPTpopup(o.text,o.bodyData,o.cursorPosition),e();break;case"GPTprompt":t.updatePopupHeader(o,s);break;case"GPTStream_completion":try{t.shadowRoot.getElementById(s)&&t.stop_stream!==s||t.listOfUndesiredStreams.includes(o.uuid)||(console.log("Stop stream with uuid",o.uuid),t.listOfUndesiredStreams.push(o.uuid),delete r[o.uuid],t.stop_stream=!1,t.clearnewlines=!0),t.listOfUndesiredStreams.includes(o.uuid)||function(t,e,o,n){r[t]=r[t]||"";var i=0;for(const o of e.split("\n"))parsed_line=o.replace(/^data: /,""),0!=parsed_line.length&&(r[t]+=parsed_line+"\n",i+=1);for(let e=0;e<i;e++){const e=r[t].indexOf("}\n{");-1!==e&&(u(r[t].substring(0,e+1),n,o),r[t]=r[t].substring(e+2))}r[t].includes("[DONE]")&&u("[DONE]",n,o)}(o.uuid,o.text,o,s)}catch(t){console.error(t)}break;default:alert(o)}})),console.log("GPT-prompter content script is running")})();