function r(e){let t=document.createElement("script");return t.setAttribute("kaption-ai-script","true"),t.src=chrome.runtime.getURL(e),t.setAttribute("kaption-ai-baseurl",t.src.replace("chrome-extension://","").split("/")[0]),e.endsWith(".js")&&!e.includes("chunks/")&&(t.type="module"),(document.head||document.documentElement).appendChild(t),t}function o(e){let t=document.createElement("link");t.setAttribute("kaption-ai-style","true"),t.rel="stylesheet",t.type="text/css",t.href=chrome.runtime.getURL(e),(document.head||document.documentElement).appendChild(t)}async function c(e){let{type:t,id:n,...a}=e.data;if(t==="KAI_FROM_PAGE")try{if(chrome.runtime?.id){let s=await chrome.runtime.sendMessage({type:"KAI_FROM_CONTENT_SCRIPT",id:n,...a});window.postMessage({type:"KAI_TO_PAGE",id:n,instanceId:e.data.instanceId,...s})}else console.warn("chrome.runtime not available. Cannot send message from content script."),window.postMessage({type:"KAI_TO_PAGE",id:n,instanceId:e.data.instanceId,error:"Extension context invalidated",message:"Extension context invalidated"})}catch(s){console.error("Error processing message in content script:",s),s instanceof Error&&s.message.includes("Extension context invalidated")&&console.log("Refreshing web page due to extension update");const i={error:s instanceof Error?s.message:String(s),message:s instanceof Error?s.message:String(s)};window.postMessage({type:"KAI_TO_PAGE",id:n,instanceId:e.data.instanceId,...i})}}window.addEventListener("message",c,!1);try{r("./errors.js")}catch(e){console.error("Failed to load errors.js:",e)}try{r("./main.js")}catch(e){console.error("Failed to load main.js:",e)}try{o("./styles/main.css")}catch(e){console.error("Failed to load main.css:",e)}try{o("./styles/shadcn.css")}catch(e){console.error("Failed to load shadcn.css:",e)}
