(function(){function p(t){try{if(!t)return;const e=globalThis.SiteConfig&&globalThis.SiteConfig.COLORS||{},o=(r,a)=>{a&&t.style.setProperty(r,a)};o("--brand-accent",e.brandAccent||"#ff9000"),o("--brand-accent-hover",e.brandAccentHover||"#ff7700"),o("--bg-dark",e.bgDark||"#1b1b1b"),o("--bg-darker",e.bgDarker||"#2a2a2a"),o("--border-dark",e.borderDark||"#333"),o("--input-border",e.inputBorder||"#555"),o("--text-primary",e.textPrimary||"#fff"),o("--text-muted",e.textMuted||"#999"),o("--text-subtle",e.textSubtle||"#ccc")}catch{}}function b(){try{const t="ph-player-button-css";if(!document.getElementById(t)){const e=document.createElement("link");e.id=t,e.rel="stylesheet",e.href=chrome.runtime.getURL("styles/player-button.css"),(document.head||document.documentElement).appendChild(e)}}catch{}}function u(){try{document.querySelectorAll(".ph-quality-popover").forEach(e=>e.remove());const t=document.querySelector(".ph-download-button");t&&t.parentElement&&(t.parentElement.removeAttribute("data-ph-dl-attached"),t.remove())}catch{}}function g(){return new Promise(t=>{try{chrome.storage.local.get(["isActivated"],e=>{t(!!(e&&e.isActivated))})}catch{t(!1)}})}function m(){const t=["#kt_player",".kt-player","#videoplayer-v3","#mediaplayer_wrapper","#player",".mainPlayerDiv",".video-holder",".video-container"];for(const o of t){const r=document.querySelector(o);if(r)return r}const e=document.querySelector("video");if(e){const o=e.closest("#kt_player, .kt-player, #videoplayer-v3, #mediaplayer_wrapper, #player, .mainPlayerDiv, .video-holder, .video-container");if(o)return o;if(e.parentElement)return e.parentElement}return null}function h(){const t=m();if(!t)return!1;if(t.getAttribute("data-ph-dl-attached")==="1")return!0;const e=document.createElement("button");e.className="ph-download-button",e.type="button",e.setAttribute("title","Download this video"),e.innerHTML='<span class="ph-dl-icon">&#x2B07;</span><span class="ph-dl-text">Download</span>',p(e);const o=window.getComputedStyle(t);return(o.position==="static"||!o.position)&&(t.style.position="relative"),t.appendChild(e),t.setAttribute("data-ph-dl-attached","1"),e.addEventListener("click",async r=>{r.preventDefault(),w(e)}),!0}async function v(){if(!await g()){u();try{chrome.storage.onChanged.addListener((e,o)=>{o==="local"&&e.isActivated&&(!!e.isActivated.newValue?setTimeout(y,0):u())})}catch{}return}y()}function y(){if(b(),h())return;const t=new MutationObserver(()=>{if(h())try{t.disconnect()}catch{}});document.body&&t.observe(document.body,{childList:!0,subtree:!0})}document.readyState==="loading"?document.addEventListener("DOMContentLoaded",v):v();function w(t){document.querySelectorAll(".ph-quality-popover").forEach(c=>c.remove());const e=m();if(!e)return;const o=document.createElement("div");o.className="ph-quality-popover",o.innerHTML=`
      <div class="ph-popover-header">Select quality</div>
      <div class="ph-popover-content"><div class="ph-popover-loading">Loading...</div></div>
    `,p(o),e.appendChild(o),E(o,t,e);const r=c=>{if(!o.contains(c.target)&&c.target!==t){try{document.removeEventListener("click",r,!0)}catch{}try{o.remove()}catch{}}};setTimeout(()=>{document.addEventListener("click",r,!0)},0);let a=null;try{a=typeof extractVideoInfo=="function"?extractVideoInfo():null}catch{}if(!a||!a.id){o.querySelector(".ph-popover-content").innerHTML='<div class="ph-popover-error">No video info found.</div>';return}chrome.runtime.sendMessage({action:"getVideoFormats",videoInfo:a},c=>{if(chrome.runtime.lastError){o.querySelector(".ph-popover-content").innerHTML=`<div class="ph-popover-error">${chrome.runtime.lastError.message}</div>`;return}if(!c||!c.success||!Array.isArray(c.formats)||c.formats.length===0){o.querySelector(".ph-popover-content").innerHTML='<div class="ph-popover-error">No formats available.</div>';return}const l=c.formats.filter(n=>n&&n.url).map(n=>({...n,q:parseInt(n.quality||n.height||0,10)||0,type:n.ext==="m3u8"||n.format_type==="hls"||(n.url||"").includes(".m3u8")?"HLS":"MP4"})).sort((n,s)=>s.q-n.q||(n.type==="MP4")-(s.type==="MP4")),d=document.createElement("div");d.className="ph-popover-list",l.forEach(n=>{const s=n.q?`${n.q}p`:n.format_id||"Unknown",i=document.createElement("button");i.className="ph-popover-item",i.type="button",i.innerHTML=`<span class="ph-popover-q">${s}</span><span class="ph-popover-type">${n.type}</span>`,i.addEventListener("click",()=>{try{typeof showDownloadManager=="function"&&showDownloadManager()}catch{}const S={...a,selectedFormat:n};chrome.runtime.sendMessage({action:"downloadVideo",videoInfo:S});try{document.removeEventListener("click",r,!0)}catch{}try{o.remove()}catch{}}),d.appendChild(i)});const f=o.querySelector(".ph-popover-content");f.innerHTML="",f.appendChild(d)})}function E(t,e,o){try{const r=e.getBoundingClientRect(),a=o.getBoundingClientRect(),c=r.top-a.top+e.offsetHeight+8,l=10;t.style.position="absolute",t.style.top=`${c}px`,t.style.right=`${l}px`,t.style.zIndex=10021}catch{}}})();
