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