document.addEventListener("DOMContentLoaded",()=>{const e=window.ShaarliProI18n||{t:function(e){return e}},t=["note","shaarli-note","todo","shaarli-todo","shaarli-pin","note-color-*","notebg-*","notefilter-*","font-*","readitlater","brain-dump","shaarli-archive"];function a(e){if(!e)return!1;const a=e.toLowerCase().trim();return function(){try{const e=localStorage.getItem("shaarli_hidden_tags");if(e)return JSON.parse(e)}catch(e){console.error("Error loading hidden tags:",e)}return[...t]}().some(e=>{const t=e.toLowerCase().trim();return t.includes("*")?function(e,t){return new RegExp("^"+e.replace(/\*/g,".*")+"$","i").test(t)}(t,a):t===a})}function s(){document.querySelectorAll(".link-tag[data-tag]").forEach(e=>{a(e.getAttribute("data-tag"))&&(e.style.display="none")}),document.querySelectorAll("#tagListContainer .list-group-item[data-tag-name]").forEach(e=>{a(e.getAttribute("data-tag-name"))&&(e.style.display="none")}),document.querySelectorAll(".tag-item[data-tag]").forEach(e=>{a(e.getAttribute("data-tag"))&&(e.style.display="none")});const e=document.getElementById("visibleTagCount");if(e){const t=document.querySelectorAll('#tagListContainer .list-group-item:not([style*="display: none"])');e.textContent=t.length}}s();new MutationObserver(e=>{let t=!1;e.forEach(e=>{"childList"===e.type&&e.addedNodes.forEach(e=>{e.nodeType===Node.ELEMENT_NODE&&(e.matches?.(".link-tag[data-tag], .tag-item[data-tag], [data-tag-name], .list-group-item[data-tag-name]")||e.querySelector?.(".link-tag[data-tag], .tag-item[data-tag], [data-tag-name], .list-group-item[data-tag-name]"))&&(t=!0)})}),t&&s()}).observe(document.body,{childList:!0,subtree:!0}),window.ShaarliProUtils={escapeHtml:function(e){if(null==e)return"";const t=document.createElement("div");return t.textContent=String(e),t.innerHTML},isTagHidden:a,filterHiddenTags:function(e){return Array.isArray(e)?e.filter(e=>!a(e)):[]}};const n=document.getElementById("links-list"),r=document.getElementById("view-grid-btn"),o=document.getElementById("view-list-btn"),i=document.getElementById("view-compact-btn");function l(e){n&&(n.classList.remove("view-grid","view-list","view-compact"),r?.classList.remove("active"),o?.classList.remove("active"),i?.classList.remove("active"),"list"===e?(n.classList.add("view-list"),o?.classList.add("active")):"compact"===e?(n.classList.add("view-compact"),i?.classList.add("active")):(n.classList.add("view-grid"),r?.classList.add("active")),localStorage.setItem("linksView",e))}l(localStorage.getItem("linksView")||"grid"),r?.addEventListener("click",()=>l("grid")),o?.addEventListener("click",()=>l("list")),i?.addEventListener("click",()=>l("compact"));if(document.querySelector(".page-thumbnails")){const t=document.querySelector(".thumbnail-placeholder"),a=document.querySelector(".thumbnail-link-title"),s=document.querySelector(".progress-current"),n=document.querySelector(".progress-actual"),r=document.querySelector('input[name="ids"]');if(r&&r.value){const o=r.value.split(","),i=o.length;let l=0;const u=function(r){fetch(shaarli.basePath+"/admin/shaare/"+r+"/update-thumbnail",{method:"PATCH",headers:{Accept:"application/json"}}).then(e=>e.json()).then(r=>{if(l++,a&&(a.textContent=r.title||e.t("Processing...")),t&&(t.innerHTML="",r.thumbnail)){const a=document.createElement("img");a.title=e.t("Current thumbnail"),a.src=r.thumbnail,a.style.maxWidth="100%",a.style.height="auto",t.appendChild(a)}s&&(s.textContent=l),n&&(n.style.width=100*l/i+"%"),l{console.error("Failed to update thumbnail:",t),a&&(a.textContent=e.t("Error updating thumbnail #{index}",{index:l})),l++,l{e.target===u&&h()}),document.addEventListener("keydown",e=>{"Escape"===e.key&&u?.classList.contains("show")&&h()}),document.addEventListener("click",t=>{const a=t.target.closest(".view-desc-btn");if(a){t.preventDefault();const s=a.dataset.id,n=document.getElementById(s);if(n){const t=n.querySelector(".link-description");if(t)p(t.cloneNode(!0));else{const t=document.createElement("p");t.className="text-muted",t.textContent=e.t("No description available."),p(t)}}}});const m=document.getElementById("qrcode-modal"),g=document.getElementById("qrcode-modal-body"),y=document.getElementById("qrcode-modal-close");function v(){m&&(m.classList.remove("show"),document.body.style.overflow="")}y?.addEventListener("click",v),m?.addEventListener("click",e=>{e.target===m&&v()}),document.addEventListener("keydown",e=>{"Escape"===e.key&&m?.classList.contains("show")&&v()}),document.querySelectorAll(".link-plugin .linkqrcode, .link-plugin img.qrcode").forEach(t=>{const a="IMG"===t.tagName?t:t.querySelector("img.qrcode");if(!a)return;const s=a.dataset.permalink||"",n=a.closest("a")||a.parentElement,r=document.createElement("i");if(r.className="mdi mdi-qrcode","A"===n.tagName||n.classList.contains("linkqrcode")){const a=document.createElement("a");a.href="#",a.className="qrcode-trigger",a.title=e.t("QR Code"),a.dataset.permalink=s,a.appendChild(r);(t.classList.contains("linkqrcode")?t:t.closest(".linkqrcode")||t).replaceWith(a)}}),document.addEventListener("click",t=>{const a=t.target.closest(".qrcode-trigger, .link-plugin img.qrcode");if(!a)return;t.preventDefault(),t.stopPropagation();const s=a.dataset?.permalink||a.querySelector("img")?.dataset?.permalink||"",n=a.closest(".link-outer"),r=n?.querySelector(".link-title")?.textContent?.trim()||s;s&&function(t,a){function s(e){g.innerHTML="";const t=document.createElement("div");t.style.padding="2rem",t.style.color="var(--text-muted)",t.textContent=e,g.appendChild(t)}m&&g&&(s(e.t("Generating QR Code...")),m.classList.add("show"),document.body.style.overflow="hidden",function n(){if("undefined"!=typeof qr){const n=qr.image({size:8,value:t});if(n){if(g.innerHTML="",n.style.maxWidth="100%",n.style.borderRadius="0.5rem",n.style.background="white",n.style.padding="0.75rem",g.appendChild(n),a){const e=document.createElement("div");e.className="qrcode-modal-title",e.textContent=a,g.appendChild(e)}}else s(e.t("Failed to generate QR Code"))}else{const e=document.querySelector('input[name="js_base_path"]')?.value||"",t=document.createElement("script");t.src=e+"/plugins/qrcode/qr-1.1.3.min.js",document.body.appendChild(t),setTimeout(()=>n(),300)}}())}(s,r)});const f="readitlater",b=["readitlater","readlater","toread"],w=e=>(e||"").trim().toLowerCase(),k=e=>b.includes(w(e));async function E(t,a){const s=await fetch(t,{method:"GET",credentials:"same-origin"});if(!s.ok)throw new Error(e.t("Unable to load bookmark edit form."));const n=await s.text(),r=(new DOMParser).parseFromString(n,"text/html").querySelector('form[name="linkform"]');if(!r)throw new Error(e.t("Bookmark edit form not found."));const o=function(e){const t=new URLSearchParams;return e.querySelectorAll("input, textarea, select").forEach(e=>{e.name&&!e.disabled&&("checkbox"!==e.type?("radio"!==e.type||e.checked)&&t.append(e.name,e.value||""):e.checked&&t.append(e.name,e.value||"on"))}),t}(r),i=(o.get("lf_tags")||"").split(/[\s,]+/).map(e=>e.trim()).filter(Boolean).filter(e=>!k(e));a&&i.push(f),o.set("lf_tags",i.join(" ")),o.set("save_edit","1");if(!(await fetch(r.action,{method:"POST",headers:{"Content-Type":"application/x-www-form-urlencoded"},body:o.toString(),credentials:"same-origin"})).ok)throw new Error(e.t("Unable to save bookmark tags."))}function S(t,a){if(!t)return;t.classList.toggle("readitlater-active",a);let s=t.querySelector(".link-readlater-badge");a&&!s?(s=document.createElement("div"),s.className="link-readlater-badge",s.textContent=e.t("Read Later"),t.appendChild(s)):!a&&s&&s.remove();const n=t.querySelector(".readitlater-toggle-btn");if(n){const t=a?e.t("Remove from Read It Later"):e.t("Add to Read It Later");n.classList.toggle("active",a),n.dataset.active=a?"1":"0",n.setAttribute("title",t),n.setAttribute("aria-label",t);const s=n.querySelector("i");s&&(s.className="mdi "+(a?"mdi-bookmark-clock":"mdi-bookmark-clock-outline"))}!function(t,a){const s=t.querySelector(".link-tag-list");if(!s)return;const n=Array.from(s.querySelectorAll(".link-tag")).find(e=>{const t=e.dataset.tag||e.querySelector(".link-tag-link")?.textContent||e.textContent||"";return w(t)===f});if(!a)return void n?.remove();if(n)return;const r=document.createElement("span");r.className="link-tag",r.dataset.tag=f;const o=document.createElement("a");if(o.className="link-tag-link",o.href=`${shaarli.basePath}/add-tag/${encodeURIComponent(f)}`,o.textContent=f,r.appendChild(o),shaarli.isAuth){const t=document.createElement("button");t.type="button",t.className="tag-remove-btn",t.dataset.tag=f,t.setAttribute("aria-label",e.t("Delete tag {tag}",{tag:f})),t.title=e.t("Delete"),t.textContent="×",r.appendChild(t)}s.appendChild(r)}(t,a)}document.querySelectorAll(".link-outer").forEach(e=>{const t=Array.from(e.querySelectorAll(".link-tag")).map(e=>{const t=e.dataset.tag||e.querySelector(".link-tag-link")?.textContent||e.textContent||"";return w(t)}).some(k)||e.classList.contains("readitlater-active")||e.classList.contains("readitlater-unread");S(e,t)}),document.addEventListener("click",async t=>{const a=t.target.closest(".readitlater-toggle-btn");if(!a)return;t.preventDefault(),t.stopPropagation();const s=a.closest(".link-outer");if(!s)return;const n=function(e){if(!e)return"";const t=e.dataset.id;if(t)return`${shaarli.basePath}/admin/shaare/${encodeURIComponent(t)}`;const a=e.querySelector('.link-actions a[href*="/admin/shaare/"]:not([href*="/pin"]):not([href*="/delete"])');return a?a.href:""}(s);if(!n)return;const r=!(s.classList.contains("readitlater-active")||"1"===a.dataset.active);if(!a.disabled){a.disabled=!0,a.classList.add("is-loading");try{await E(n,r),S(s,r)}catch(t){console.error("Failed to toggle readitlater tag:",t),alert(e.t("Failed to toggle readitlater tag."))}finally{a.disabled=!1,a.classList.remove("is-loading")}}}),function(){const e=document.querySelectorAll('.bookmark-editor-form[data-batch-mode="0"]');if(!e.length)return;const t=e=>e.trim().replace(/\s+/g,"-");e.forEach(e=>{e.classList.add("is-enhanced");const a=e.querySelector(".bookmark-editor-source"),s=e.querySelector(".bookmark-markdown-editor"),n=e.querySelector(".bookmark-tags-hidden-input"),r=e.querySelector(".bookmark-tags-input"),o=e.querySelector(".bookmark-tags-list"),i=e.querySelector(".bookmark-tags-text-input"),l=e.querySelector(".bookmark-toggle-readlater"),u=e.querySelector(".bookmark-toggle-note"),d=e.querySelector(".bookmark-toggle-todo"),c=e.querySelector('input[name="lf_title"]'),p="dark"===document.documentElement.getAttribute("data-theme");if(a&&s&&window.toastui&&window.toastui.Editor){const t=window.innerWidth<992?"tab":"vertical";let n=null;const r=new window.toastui.Editor({el:s,height:"320px",initialEditType:"wysiwyg",previewStyle:t,initialValue:a.value||"",placeholder:a.getAttribute("placeholder")||"",theme:p?"dark":void 0,usageStatistics:!1});n=window.setInterval(()=>{if(!(a.value||"").trim())return;(r.getMarkdown()||"").trim()||r.setMarkdown(a.value||"",!1),window.clearInterval(n),n=null},250),e.addEventListener("submit",()=>{n&&(window.clearInterval(n),n=null),a.value=r.getMarkdown()})}else s&&(s.style.display="none",a&&(a.style.display="block"));if(!n||!o||!i)return;const h=(n.getAttribute("data-tag-options")||"").split(",").map(e=>t(e)).filter(Boolean),m=()=>h.filter(e=>!v.some(t=>t.toLowerCase()===e.toLowerCase()));let g=null;const y=()=>{g&&(g.list=m())};r&&r.addEventListener("click",()=>i.focus());let v=n.value.split(/[\s,]+/).map(e=>t(e)).filter(Boolean);v=v.filter((e,t,a)=>t===a.findIndex(t=>t.toLowerCase()===e.toLowerCase()));const f=e.querySelector('input[name="returnurl"]');f&&e.addEventListener("submit",()=>{(u?.checked||v.some(e=>/^note$/i.test(e)))&&(f.value=`${shaarli.basePath}/?searchtags=note`)});const b=()=>{n.value=v.join(" ")},w=()=>{l&&(l.checked=v.some(e=>/^(readitlater|readlater|toread)$/i.test(e)))},k=()=>{u&&(u.checked=v.some(e=>/^(note|#note|shaarli-note)$/i.test(e)))},E=()=>{d&&(d.checked=v.some(e=>/^(todo|#todo|shaarli-todo)$/i.test(e)))},S="📝",L="✅",I=e=>e&&e.startsWith(S),x=e=>e&&e.startsWith(L),C=e=>e?I(e)?e:"📝 "+e:"📝 ",D=e=>e?x(e)?e:"✅ "+e:"✅ ",B=e=>{if(!c)return;const t=c.value||"";var a;e?I(t)||(c.value=C(t)):I(t)&&(c.value=(a=t)?a.startsWith("📝 ")?a.substring(3):a.startsWith(S)?a.substring(2):a:"")},T=e=>{if(!c)return;const t=c.value||"";var a;e?x(t)||(c.value=D(t)):x(t)&&(c.value=(a=t)?a.startsWith("✅ ")?a.substring(2):a.startsWith(L)?a.substring(1):a:"")},q=e=>{const a=t(e);if(!a)return;v.some(e=>e.toLowerCase()===a.toLowerCase())||(v.push(a),b(),w(),k(),E(),N(),y())},P=e=>{v=v.filter(t=>t.toLowerCase()!==e.toLowerCase()),b(),w(),k(),E(),N(),y()},A=()=>{const e=i.value;e.trim()&&(e.split(/[\s,]+/).map(e=>e.trim()).filter(Boolean).forEach(q),i.value="",y())};function N(){o.innerHTML="",v.forEach(e=>{const t=document.createElement("span");t.className="bookmark-tag-pill",t.setAttribute("data-tag",e);const a=document.createElement("span");a.className="bookmark-tag-label",a.textContent=e;const s=document.createElement("button");s.type="button",s.className="bookmark-tag-remove",s.setAttribute("aria-label",`Remove tag ${e}`),s.innerHTML='',s.addEventListener("click",()=>P(e)),t.appendChild(a),t.appendChild(s),o.appendChild(t)})}if(i.addEventListener("keydown",e=>{const t=Boolean(g&&g.ul&&!g.ul.hasAttribute("hidden"));if(" "===e.key||","===e.key||"Enter"===e.key&&!t)return e.preventDefault(),void A();"Backspace"===e.key&&!i.value&&v.length&&P(v[v.length-1])}),i.addEventListener("blur",A),i.addEventListener("paste",e=>{const t=e.clipboardData?e.clipboardData.getData("text"):"";t&&(e.preventDefault(),t.split(/[\s,]+/).map(e=>e.trim()).filter(Boolean).forEach(q),i.value="")}),l&&l.addEventListener("change",()=>{v=v.filter(e=>!/^(readitlater|readlater|toread)$/i.test(e)),l.checked&&v.push("readitlater"),b(),k(),N(),y()}),u&&u.addEventListener("change",()=>{v=v.filter(e=>!/^note$/i.test(e)),u.checked?(v.push("note"),B(!0)):B(!1),b(),w(),E(),N(),y()}),d&&d.addEventListener("change",()=>{v=v.filter(e=>!/^shaarli-todo$/i.test(e)),d.checked?(v.push("shaarli-todo"),T(!0)):T(!1),b(),w(),k(),N(),y()}),window.Awesomplete&&h.length&&(g=new window.Awesomplete(i,{list:m(),minChars:1,maxItems:8,autoFirst:!0,sort:!1,filter:window.Awesomplete.FILTER_CONTAINS}),i.addEventListener("input",()=>{y(),g.evaluate()}),i.addEventListener("awesomplete-selectcomplete",A)),e.addEventListener("submit",A),e.addEventListener("submit",()=>{try{if(!window.ShaarItRules)return;const t=e.querySelector('input[name="lf_url"]'),a=t?(t.value||"").trim():"";if(!v.some(e=>/^(todo|shaarli-todo)$/i.test(e)))return;if(v.some(e=>/^shaarli-todo$/i.test(e))&&(v=v.filter(e=>!/^shaarli-todo$/i.test(e)),v.some(e=>/^todo$/i.test(e))||v.push("todo"),b()),!a||/^https?:\/\/shaarli-todo/.test(a)||"http://shaarli-todo"===a){const e=window.ShaarItRules.generateTodoUrl();t.value=e}}catch(e){console.warn("[shaarit] todo migration failed:",e)}}),e.addEventListener("submit",()=>{try{if(!window.ShaarItRules)return;const t=e.querySelector('input[name="lf_url"]'),a=t?(t.value||"").trim():"";if(!a)return;const s={url:a,tags:v};if(window.ShaarItRules.isNote(s)||window.ShaarItRules.isTodo(s))return;const n=window.ShaarItRules.detectContentType(a);if(!n||!n.tags||0===n.tags.length)return;v=window.ShaarItRules.mergeAutoTags(v,n.tags),b()}catch(e){console.warn("[shaarit] content-type auto-tagging failed:",e)}}),b(),w(),k(),E(),N(),y(),d&&d.checked&&c){const e=c.value||"";x(e)||e.trim()?x(e)||(c.value=D(e)):c.value="✅ "}else if(u&&u.checked&&c){const e=c.value||"";"Note:"===e||"Note"===e?c.value="📝 ":I(e)||e.trim()?I(e)||(c.value=C(e)):c.value="📝 "}})}(),function(){const e=document.querySelector(".button-batch-addform"),t=document.querySelector("form.batch-addform");e&&t&&e.addEventListener("click",()=>{const a=t.classList.contains("hidden");if(t.classList.toggle("hidden"),e.setAttribute("aria-expanded",a?"true":"false"),a){const e=t.querySelector("#urls");e?.focus()}});const a=document.querySelector(".page-edit-link-batch");if(!a)return;const s=document.getElementById("progress-overlay"),n=a.querySelector(".progress-current"),r=a.querySelector(".progress-total"),o=a.querySelector(".progress-actual"),i=a.querySelectorAll('button[name="save_edit_batch"]'),l=()=>Array.from(a.querySelectorAll('.bookmark-editor-form[data-batch-mode="1"]')),u=(e,t)=>{if(n&&(n.textContent=String(e)),r&&(r.textContent=String(t)),o){const a=t>0?100*e/t:0;o.style.width=`${a}%`}},d=()=>{const e=l().length;u(0,e)};d(),a.addEventListener("click",e=>{const t=e.target.closest('button[name="cancel-batch-link"]');if(!t)return;e.preventDefault();const a=t.closest(".editlinkform");a?.remove(),d()}),i.forEach(e=>{e.addEventListener("click",async e=>{e.preventDefault();const t=l();t.length&&(i.forEach(e=>{e.disabled=!0}),await async function(e){const t=e.length;let a=0;u(0,t),s?.classList.remove("hidden");for(const s of e){const e=new FormData(s);try{const t=await fetch(s.action||`${shaarli.basePath}/admin/shaare`,{method:"POST",body:e,credentials:"same-origin"});t.ok||console.error("Batch save failed with status",t.status)}catch(e){console.error("Batch save request failed",e)}a+=1,u(a,t)}window.location.href=`${shaarli.basePath}/`}(t))})})}();const L=[".mp3",".mp4",".ogg",".webm",".m3u8",".flac",".wav",".aac",".m4a",".opus",".wma",".oga",".m3u",".pls"];function I(e){if(!e)return!1;for(var t=e.toLowerCase().split("?")[0].split("#")[0],a=0;a"),a.push(''),a.push(""),a.push(''),a.push(''),a.push("♪ Shaarli Player"),a.push(''),a.push(""),a.push(""),a.push(""),a.push(""),a.push('
'),a.push('
'),a.push(' '),a.push(' '+e.t("LIVE")+""),a.push("
"),a.push('
'+e.t("Loading...")+"
"),a.push('
'),a.push(' '),a.push('
'),a.push(' 0:00'),a.push(' 0:00'),a.push("
"),a.push("
"),a.push('
'),a.push(' "),a.push(' "),a.push(' "),a.push("
"),a.push('
'),a.push(' '),a.push(' '),a.push(' '),a.push("
"),a.push("
"),a.push(""),a.push('"),a.push(""),a.push(''),a.push(""),a.push("