var background=(function(){"use strict";var h,b;function w(t){return t==null||typeof t=="function"?{main:t}:t}const c=(b=(h=globalThis.browser)==null?void 0:h.runtime)!=null&&b.id?globalThis.browser:globalThis.chrome,p={nextSdk:c.runtime.getURL("/vendor/next-sdk.js"),mcpServer:c.runtime.getURL("/vendor/mcp-server.js"),mcpInjectTools:c.runtime.getURL("/vendor/mcp-inject-tools.js")},S=t=>`/mcp-servers/${t}/index.js`,g=async({hostname:t,extraScripts:o=[],tabId:e})=>{const i=S(t),r=c.runtime.getURL(i);if(!r)return!1;let n=await fetch(r).then(s=>s.text());o.length&&(n+=o.join(""));try{const s=await c.userScripts.getScripts({ids:[r]}),a=s.length?"update":"register",d=s.length===0;if(console.log("existingScripts.length",s),console.log("【Common】 injectType",a,r,t),await c.userScripts[a]([{id:r,matches:[`https://${t}/*`],js:[{code:n}],world:"MAIN"}]),console.log("existingScripts.length",s),console.log("【Common】 injectType success",a,r,t),d)try{if(typeof e=="number")await c.tabs.reload(e),console.log("【Common】 首次注册后刷新当前标签页",{hostname:t,tabId:e});else{const l=await c.tabs.query({url:[`https://${t}/*`,`http://${t}/*`]});l.length?(await c.tabs.reload(l[0].id),console.log("【Common】 首次注册后刷新匹配标签页",{hostname:t,tabId:l[0].id})):console.warn("【Common】 首次注册后未找到可刷新的标签页",{hostname:t})}}catch(l){console.error("【Common】 首次注册后刷新标签页失败",l,{hostname:t,tabId:e})}return!0}catch(s){return console.error("User Scripts API 调用失败:",s),await c.notifications.create(`userScripts-error-${Date.now()}`,{type:"basic",iconUrl:c.runtime.getURL("/icons/128.png"),title:"User Scripts 权限未开启",message:"请在扩展管理页面开启 User Scripts 开关",priority:2}),!1}},y=async(t,o,e=!0)=>{const i=[];if(e){const r=await fetch(p.nextSdk).then(s=>s.text()),n=await fetch(p.mcpServer).then(s=>s.text());i.push(r.replace("define.amd","define.amdx"),n)}return g({hostname:t,extraScripts:i,tabId:o})},j=async(t,o)=>{const e=await fetch(p.mcpInjectTools).then(i=>i.text());return g({hostname:t,extraScripts:[e],tabId:o})},m=(t,o,e,i)=>{const r=(n,s,a)=>{if(n.type===t&&n.direction===e){const{data:d}=n;o(d,s,a),a(s)}};return c.runtime.onMessage.addListener(r),()=>c.runtime.onMessage.removeListener(r)},x=w(()=>{c.runtime.onMessage.addListener((t,o,e)=>{if(t.type==="inject-mcp-scripts"){const{hostname:i,tabId:r}=t;try{y(i,r).then(n=>{e({success:n,hostname:i,tabId:r})})}catch(n){console.error("脚本注入失败:",n),e({success:!1,hostname:i,tabId:r,error:n})}return!0}if(t.type==="inject-tools-script"){const{hostname:i,tabId:r}=t;try{j(i,r).then(n=>{e({success:n,hostname:i,tabId:r})})}catch(n){console.error("脚本注入失败:",n),e({success:!1,hostname:i,tabId:r,error:n})}return!0}}),m("focus-current-tab",async(t,o)=>{var e;return await c.tabs.update((e=o.tab)==null?void 0:e.id,{active:!0})},"content->bg"),m("who-am-i",()=>{},"content->bg")});function U(){}function u(t,...o){if(typeof o[0]=="string"){const e=o.shift();t(`[wxt] ${e}`,...o)}else t("[wxt]",...o)}const T={debug:(...t)=>u(console.debug,...t),log:(...t)=>u(console.log,...t),warn:(...t)=>u(console.warn,...t),error:(...t)=>u(console.error,...t)};let f;try{f=x.main(),f instanceof Promise&&console.warn("The background's main() function return a promise, but it must be synchronous")}catch(t){throw T.error("The background crashed on startup!"),t}return f})();
