(function(){"use strict";var P=Object.defineProperty;var U=(f,l,a)=>l in f?P(f,l,{enumerable:!0,configurable:!0,writable:!0,value:a}):f[l]=a;var d=(f,l,a)=>U(f,typeof l!="symbol"?l+"":l,a);var x,I,h,C,c;function f(r){return r}const a=(I=(x=globalThis.browser)==null?void 0:x.runtime)!=null&&I.id?globalThis.browser:globalThis.chrome;async function A(r,t){var m;const n=a.runtime.getURL(r),e=document.createElement("script"),i=a.runtime.getManifest().manifest_version===2;i?e.text=await fetch(n).then(w=>w.text()):e.src=n;const s=i?void 0:T(e);return await((m=t==null?void 0:t.modifyScript)==null?void 0:m.call(t,e)),(document.head??document.documentElement).append(e),await s,{script:e}}function T(r){return new Promise((t,n)=>{const e=()=>{t(),s()},i=()=>{n(new Error(`Failed to load script: ${r.src}`)),s()},s=()=>{r.removeEventListener("load",e),r.removeEventListener("error",i)};r.addEventListener("load",e),r.addEventListener("error",i)})}const F="zaowu-x-article:to-main",S="zaowu-x-article:from-main",L={allFrames:!1,matches:["https://x.com/*","https://twitter.com/*"],runAt:"document_idle",async main(r){const{script:t}=await A("/x-article-main-world.js",{keepInDom:!0,modifyScript(u){u.id=`zaowu-x-article-main-${crypto.randomUUID()}`}}),n=new D;let e=null,i=!1;const s=async u=>{const o=await a.runtime.sendMessage(u);if(!o)throw new Error("\u9020\u7269\u52A9\u624B\u540E\u53F0\u6682\u65F6\u6CA1\u6709\u54CD\u5E94\u3002");if(!o.ok)throw Object.assign(new Error(o.error.message),{code:o.error.code});return o.data},m=u=>t.dispatchEvent(new CustomEvent(F,{detail:u})),w=async()=>{if(!i){i=!0;try{if(e=await s({type:"x-article.claim"}),!(e!=null&&e.plan))return;n.show("\u6B63\u5728\u8FDE\u63A5 X Articles\u2026","working"),m({jobId:e.id,plan:e.plan,type:"run"})}catch(u){i=!1,n.show(u instanceof Error?u.message:"\u65E0\u6CD5\u8FDE\u63A5\u540C\u6B65\u4EFB\u52A1\u3002","error")}}},y=u=>{const o=u.detail;if(o!=null&&o.type){if(o.type==="ready"){w();return}if(o.type==="asset-request"){s({assetId:o.assetId,jobId:o.jobId,type:"x-article.asset.get"}).then(p=>m({asset:p,ok:!0,requestId:o.requestId,type:"asset-response"}),p=>m({error:p instanceof Error?p.message:"\u4E34\u65F6\u56FE\u7247\u8BFB\u53D6\u5931\u8D25\u3002",ok:!1,requestId:o.requestId,type:"asset-response"}));return}if(o.type==="progress"&&e){n.show(String(o.label||"\u6B63\u5728\u540C\u6B65\u5230 X \u8349\u7A3F\u2026"),"working"),s({jobId:e.id,stage:o.stage,targetUrl:location.href,type:"x-article.progress"}).catch(()=>{});return}if(o.type==="complete"&&e){s({jobId:e.id,result:o.result,targetUrl:o.targetUrl||location.href,type:"x-article.complete"}).then(()=>n.show("\u5DF2\u4FDD\u5B58\u4E3A X \u8349\u7A3F\u3002\u8BF7\u68C0\u67E5\u5C01\u9762\u4E0E\u6392\u7248\u540E\u518D\u51B3\u5B9A\u662F\u5426\u53D1\u5E03\u3002","success"),p=>n.show(p instanceof Error?p.message:"\u8349\u7A3F\u6821\u9A8C\u5931\u8D25\u3002","error"));return}o.type==="error"&&e&&(n.show(String(o.message||"\u540C\u6B65\u5230 X \u8349\u7A3F\u5931\u8D25\u3002"),"error"),s({error:{code:String(o.code||"X_ARTICLE_SYNC_FAILED"),message:String(o.message||"\u540C\u6B65\u5931\u8D25\u3002"),retryable:o.retryable===!0},jobId:e.id,type:"x-article.fail"}).catch(()=>{}))}};t.addEventListener(S,y),m({type:"ping"}),r.onInvalidated(()=>{t.removeEventListener(S,y),n.remove(),t.remove()})}};class D{constructor(){d(this,"host");d(this,"text");this.host=document.createElement("div"),this.host.id="zaowu-x-article-status";const t=this.host.attachShadow({mode:"open"}),n=document.createElement("style");n.textContent=`
      :host { all: initial; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif; }
      .bar { align-items: center; background: rgba(15, 20, 25, .96); border: 1px solid #333639; border-radius: 999px; box-shadow: 0 10px 34px rgba(0,0,0,.28); color: #fff; display: flex; font-size: 13px; font-weight: 650; gap: 9px; left: 50%; max-width: min(680px, calc(100vw - 32px)); min-height: 42px; padding: 0 18px; position: fixed; top: 16px; transform: translateX(-50%); z-index: 2147483647; }
      .dot { background: #1d9bf0; border-radius: 50%; height: 9px; flex: 0 0 auto; width: 9px; }
      .success .dot { background: #00ba7c; }
      .error .dot { background: #f4212e; }
    `;const e=document.createElement("div");e.className="bar";const i=document.createElement("span");i.className="dot",this.text=document.createElement("span"),e.append(i,this.text),t.append(n,e)}show(t,n){const e=this.host.shadowRoot.querySelector(".bar");e.className=`bar ${n}`,this.text.textContent=t,this.host.isConnected||document.documentElement.append(this.host)}remove(){this.host.remove()}}function g(r,...t){}const N={debug:(...r)=>g(console.debug,...r),log:(...r)=>g(console.log,...r),warn:(...r)=>g(console.warn,...r),error:(...r)=>g(console.error,...r)};var b=(h=class extends Event{constructor(t,n){super(h.EVENT_NAME,{}),this.newUrl=t,this.oldUrl=n}},d(h,"EVENT_NAME",v("wxt:locationchange")),h);function v(r){var t;return`${(t=a==null?void 0:a.runtime)==null?void 0:t.id}:x-article:${r}`}const M=typeof((C=globalThis.navigation)==null?void 0:C.addEventListener)=="function";function _(r){let t,n=!1;return{run(){n||(n=!0,t=new URL(location.href),M?globalThis.navigation.addEventListener("navigate",e=>{const i=new URL(e.destination.url);i.href!==t.href&&(window.dispatchEvent(new b(i,t)),t=i)},{signal:r.signal}):r.setInterval(()=>{const e=new URL(location.href);e.href!==t.href&&(window.dispatchEvent(new b(e,t)),t=e)},1e3))}}}var R=(c=class{constructor(t,n){d(this,"id");d(this,"abortController");d(this,"locationWatcher",_(this));this.contentScriptName=t,this.options=n,this.id=Math.random().toString(36).slice(2),this.abortController=new AbortController,this.stopOldScripts(),this.listenForNewerScripts()}get signal(){return this.abortController.signal}abort(t){return this.abortController.abort(t)}get isInvalid(){var t;return((t=a.runtime)==null?void 0:t.id)==null&&this.notifyInvalidated(),this.signal.aborted}get isValid(){return!this.isInvalid}onInvalidated(t){return this.signal.addEventListener("abort",t),()=>this.signal.removeEventListener("abort",t)}block(){return new Promise(()=>{})}setInterval(t,n){const e=setInterval(()=>{this.isValid&&t()},n);return this.onInvalidated(()=>clearInterval(e)),e}setTimeout(t,n){const e=setTimeout(()=>{this.isValid&&t()},n);return this.onInvalidated(()=>clearTimeout(e)),e}requestAnimationFrame(t){const n=requestAnimationFrame((...e)=>{this.isValid&&t(...e)});return this.onInvalidated(()=>cancelAnimationFrame(n)),n}requestIdleCallback(t,n){const e=requestIdleCallback((...i)=>{this.signal.aborted||t(...i)},n);return this.onInvalidated(()=>cancelIdleCallback(e)),e}addEventListener(t,n,e,i){var s;n==="wxt:locationchange"&&this.isValid&&this.locationWatcher.run(),(s=t.addEventListener)==null||s.call(t,n.startsWith("wxt:")?v(n):n,e,{...i,signal:this.signal})}notifyInvalidated(){this.abort("Content script context invalidated"),N.debug(`Content script "${this.contentScriptName}" context invalidated`)}stopOldScripts(){var t;document.dispatchEvent(new CustomEvent(c.SCRIPT_STARTED_MESSAGE_TYPE,{detail:{contentScriptName:this.contentScriptName,messageId:this.id}})),(t=this.options)!=null&&t.noScriptStartedPostMessage||window.postMessage({type:c.SCRIPT_STARTED_MESSAGE_TYPE,contentScriptName:this.contentScriptName,messageId:this.id},"*")}verifyScriptStartedEvent(t){var i,s;const n=((i=t.detail)==null?void 0:i.contentScriptName)===this.contentScriptName,e=((s=t.detail)==null?void 0:s.messageId)===this.id;return n&&!e}listenForNewerScripts(){const t=n=>{!(n instanceof CustomEvent)||!this.verifyScriptStartedEvent(n)||this.notifyInvalidated()};document.addEventListener(c.SCRIPT_STARTED_MESSAGE_TYPE,t),this.onInvalidated(()=>document.removeEventListener(c.SCRIPT_STARTED_MESSAGE_TYPE,t))}},d(c,"SCRIPT_STARTED_MESSAGE_TYPE",v("wxt:content-script-started")),c);function B(){}function E(r,...t){}const k={debug:(...r)=>E(console.debug,...r),log:(...r)=>E(console.log,...r),warn:(...r)=>E(console.warn,...r),error:(...r)=>E(console.error,...r)};return(async()=>{try{const{main:r,...t}=L;return await r(new R("x-article",t))}catch(r){throw k.error('The content script "x-article" crashed on startup!',r),r}})()})();
