const g="https://blog.lvonce.com/api/jamstack/messages",u="extension-feedback-visitor-id-v1";function E(){try{const e=localStorage.getItem(u);if(e)return e;const a=crypto.randomUUID();return localStorage.setItem(u,a),a}catch{return""}}async function k(e,a){const r=e.trim();if(!r)throw new Error("请先填写反馈内容");const i=await fetch(g,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({site_slug:"www",content:r,contact:a?.trim()||null,nickname:"Extension 用户",path:`/extension${window.location.pathname}`,visitor_id:E()||null})});if(i.ok)return;let c=`反馈发送失败（HTTP ${i.status}）`;try{const l=await i.json();c=l?.error?.message??l?.message??c}catch{}throw new Error(c)}const y=document.getElementById("feedback-widget"),s=document.getElementById("feedback-panel"),f=document.getElementById("feedback-trigger"),w=document.getElementById("feedback-close"),n=document.getElementById("feedback-input"),m=document.getElementById("feedback-contact"),d=document.getElementById("feedback-submit"),t=document.getElementById("feedback-status");function o(e){s.hidden=!e,f.setAttribute("aria-expanded",String(e)),e&&window.setTimeout(()=>n.focus(),0)}f.addEventListener("click",()=>o(s.hidden));w.addEventListener("click",()=>o(!1));document.addEventListener("pointerdown",e=>{!s.hidden&&!y.contains(e.target)&&o(!1)});document.addEventListener("keydown",e=>{e.key==="Escape"&&!s.hidden&&o(!1)});d.addEventListener("click",async()=>{if(!n.value.trim()){t.textContent="请先填写反馈内容",t.dataset.kind="error",n.focus();return}d.disabled=!0,t.textContent="发送中…",t.dataset.kind="info";try{await k(n.value,m.value),n.value="",m.value="",t.textContent="已收到，谢谢",window.setTimeout(()=>{o(!1),t.textContent=""},1200)}catch(e){t.textContent=e instanceof Error?e.message:"发送失败，请稍后重试",t.dataset.kind="error"}finally{d.disabled=!1}});
