import{c as O}from"./logger-CvfM-6aa.js";import{h as T}from"./jszip.min-DpCewD43.js";import{b as v,r as R,c as k}from"./content-processor-IWRcpt0Y.js";import"./_commonjsHelpers-BosuxZz1.js";const _=O("WeixinEditor");(()=>{let i=null,d=null;function C(){const t=window.location.href;return t.includes("mp.weixin.qq.com/cgi-bin/appmsg")&&(t.includes("action=edit")||t.includes("appmsg_edit"))}function A(){if(document.querySelector("#wechatsync-editor-fab"))return;const t=document.createElement("button");t.id="wechatsync-editor-fab",t.title="同步助手",t.innerHTML=`
    <svg viewBox="0 0 24 24" width="24" height="24" fill="white">
      <path d="M12 4V1L8 5l4 4V6c3.31 0 6 2.69 6 6 0 1.01-.25 1.97-.7 2.8l1.46 1.46C19.54 15.03 20 13.57 20 12c0-4.42-3.58-8-8-8zm0 14c-3.31 0-6-2.69-6-6 0-1.01.25-1.97.7-2.8L5.24 7.74C4.46 8.97 4 10.43 4 12c0 4.42 3.58 8 8 8v3l4-4-4-4v3z"/>
    </svg>
  `,t.style.cssText=`
    position: fixed;
    right: 20px;
    bottom: 80px;
    z-index: 2147483647;
    width: 48px;
    height: 48px;
    border-radius: 50%;
    background: linear-gradient(135deg, #07c160 0%, #06ad56 100%);
    border: none;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 4px 12px rgba(7, 193, 96, 0.4);
    transition: all 0.2s;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  `,t.addEventListener("mouseenter",()=>{t.style.transform="scale(1.05)",t.style.boxShadow="0 6px 16px rgba(7, 193, 96, 0.5)"}),t.addEventListener("mouseleave",()=>{t.style.transform="scale(1)",t.style.boxShadow="0 4px 12px rgba(7, 193, 96, 0.4)"}),t.addEventListener("click",()=>L()),document.body.appendChild(t)}async function L(){var l;if(d)return;d=document.createElement("div"),d.id="wechatsync-dialog-overlay",d.style.cssText=`
    position: fixed; inset: 0; z-index: 2147483647;
    display: flex; align-items: center; justify-content: center;
    background: rgba(0,0,0,0.3);
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  `,d.addEventListener("click",u=>{u.target===d&&h()});const t=document.createElement("div");t.style.cssText=`
    background: white; padding: 20px 32px; border-radius: 12px;
    box-shadow: 0 20px 60px rgba(0,0,0,0.2);
    display: flex; align-items: center; gap: 12px;
  `,t.innerHTML=`
    <div style="width:20px;height:20px;border:3px solid #e5e5e5;border-top-color:#07c160;border-radius:50%;animation:wcs-spin 0.8s linear infinite;"></div>
    <span style="font-size:14px;color:#333;">正在提取文章...</span>
    <style>@keyframes wcs-spin { to { transform: rotate(360deg); } }</style>
  `,d.appendChild(t),document.body.appendChild(d);const[n,o]=await Promise.all([E(),chrome.runtime.sendMessage({type:"CHECK_ALL_AUTH"}).catch(()=>({platforms:[]}))]);if(!n){chrome.runtime.sendMessage({type:"TRACK_ARTICLE_EXTRACT",payload:{source:"weixin-editor",success:!1}}).catch(()=>{}),h();return}chrome.runtime.sendMessage({type:"TRACK_ARTICLE_EXTRACT",payload:{source:"weixin-editor",success:!0,hasTitle:!!n.title,hasContent:!!n.content,hasCover:!!n.cover,contentLength:((l=n.content)==null?void 0:l.length)||0}}).catch(()=>{});const r=o.platforms||[];if(!d)return;t.remove(),i=document.createElement("iframe"),i.src=chrome.runtime.getURL("src/sync-dialog/index.html"),i.style.cssText=`
    width: 400px; height: 520px; border: none;
    border-radius: 12px; overflow: hidden;
    box-shadow: 0 20px 60px rgba(0,0,0,0.2);
  `,d.appendChild(i),document.body.appendChild(d);const s=u=>{var y;try{(typeof u.data=="string"?JSON.parse(u.data):u.data).type==="SYNC_DIALOG_READY"&&(window.removeEventListener("message",s),(y=i==null?void 0:i.contentWindow)==null||y.postMessage(JSON.stringify({type:"INIT_DATA",article:n,platforms:r}),"*"))}catch{}};window.addEventListener("message",s)}function h(){d&&(d.remove(),d=null,i=null)}window.addEventListener("message",t=>{try{const n=typeof t.data=="string"?JSON.parse(t.data):t.data;if(n.type==="CLOSE_SYNC_DIALOG")h();else if(n.type==="START_SYNC"){const o=n.syncId;chrome.runtime.sendMessage({type:"SYNC_ARTICLE",payload:{article:n.article,platforms:n.platforms,source:"weixin-editor",syncId:o}}).then(r=>{var s;(s=i==null?void 0:i.contentWindow)==null||s.postMessage(JSON.stringify({type:"SYNC_COMPLETE",results:r.results,rateLimitWarning:r.rateLimitWarning,syncId:o}),"*")}).catch(r=>{var s;(s=i==null?void 0:i.contentWindow)==null||s.postMessage(JSON.stringify({type:"SYNC_ERROR",error:r.message,syncId:o}),"*")})}}catch{}}),chrome.runtime.onMessage.addListener(t=>{var n,o,r;i&&(t.type==="SYNC_PROGRESS"&&((o=i.contentWindow)==null||o.postMessage(JSON.stringify({type:"SYNC_PROGRESS",result:(n=t.payload)==null?void 0:n.result,syncId:t.syncId}),"*")),t.type==="SYNC_DETAIL_PROGRESS"&&((r=i.contentWindow)==null||r.postMessage(JSON.stringify({type:"SYNC_DETAIL_PROGRESS",progress:t.payload,syncId:t.syncId}),"*")))}),chrome.runtime.onMessage.addListener((t,n,o)=>{if(t.type==="EXTRACT_ARTICLE"&&C())return E().then(r=>{o({article:r})}).catch(()=>{o({article:null})}),!0;if(t.type==="EXPAND_SYNC_PANEL")return L(),o({success:!0}),!0});async function E(){var t,n,o;try{_.debug("Extracting article...");const r=["#js_title_place","#title",'input[name="title"]',".weui-desktop-form__input",".title_input input",".js_title",'[data-id="title"]',".appmsg_title input",".appmsg-edit-title input"];let s="";for(const c of r){const e=document.querySelector(c),a=((t=e==null?void 0:e.value)==null?void 0:t.trim())||((n=e==null?void 0:e.textContent)==null?void 0:n.trim());if(a){s=a;break}}let l="";const u=["#ueditor_0",'iframe[id^="ueditor"]',".edui-editor iframe","iframe.edui-body-container"];for(const c of u)try{const e=document.querySelector(c);if((o=e==null?void 0:e.contentDocument)!=null&&o.body){const a=e.contentDocument.body,p=a.innerHTML;if(p&&p.trim()&&p.trim()!=="<p><br></p>"&&p.length>10){const b=v(a);l=a.innerHTML,R(b);break}}}catch{}if(!l){const c=[".edui-body-container",".rich_media_content","#js_content",".appmsg-edit-content"];for(const e of c){const a=document.querySelector(e);if(a!=null&&a.innerHTML&&a.innerHTML.trim().length>10){const p=v(a);l=a.innerHTML,R(p);break}}}const y=new URLSearchParams(window.location.search).get("appmsgid");if(y&&(!l||!s)){const c=await M(y);if(c)return c}if(!s||!l)return null;const f=[".appmsg_thumb img",".js_cover img",".cover-img img",".appmsg_thumb_wrap img"];let x="";for(const c of f){const e=document.querySelector(c);if(e!=null&&e.src&&!e.src.includes("data:")){x=e.src;break}}const w=['[name="digest"]',"#digest","textarea.digest",".appmsg_desc textarea"];let S="";for(const c of w){const e=document.querySelector(c);if(e!=null&&e.value){S=e.value;break}}const m=k(l),g=T(m);return{title:s,html:m,content:m,markdown:g,summary:S,cover:x,source:{url:window.location.href,platform:"weixin-editor"}}}catch(r){return _.error("Extract failed:",r),null}}async function M(t){var n,o,r,s;try{const l=window.location.search.match(/token=(\d+)/);if(!l)return null;const u=l[1],f=await(await fetch(`https://mp.weixin.qq.com/cgi-bin/appmsg?action=get_temp_url&appmsgid=${t}&itemidx=1&token=${u}&lang=zh_CN&f=json&ajax=1`,{credentials:"include"})).json();if(!f.temp_url)return null;const w=await(await fetch(f.temp_url)).text(),m=new DOMParser().parseFromString(w,"text/html"),g=(o=(n=m.querySelector("#activity-name"))==null?void 0:n.textContent)==null?void 0:o.trim(),c=m.querySelector("#js_content"),e=(r=m.querySelector('meta[property="og:image"]'))==null?void 0:r.getAttribute("content"),a=(s=m.querySelector('meta[property="og:description"]'))==null?void 0:s.getAttribute("content");if(!g||!c)return null;const p=k(c.innerHTML),b=T(p);return{title:g,html:p,content:p,markdown:b,summary:a,cover:e,source:{url:f.temp_url,platform:"weixin"}}}catch(l){return _.error("API fetch failed:",l),null}}function N(){if(!C())return;const t=()=>setTimeout(A,1500);document.readyState==="loading"?document.addEventListener("DOMContentLoaded",t):t()}N()})();
