var youtubePlayerBridge=function(){"use strict";function g(e){return e}const p={matches:["*://www.youtube.com/*"],runAt:"document_start",world:"MAIN",main(){console.log("[Kagi Bridge] YouTube player bridge initialized");const e=new Set,l=XMLHttpRequest.prototype.open,u=XMLHttpRequest.prototype.send;XMLHttpRequest.prototype.open=function(i,t,o=!0,s,n){return this._kagiUrl=t==null?void 0:t.toString(),l.apply(this,[i,t,o,s,n])},XMLHttpRequest.prototype.send=function(i){const t=this._kagiUrl;if(t&&typeof t=="string"&&(t.includes("/api/timedtext")||t.includes("timedtext"))&&!t.includes("type=list")&&t.includes("lang=")){console.log("[Kagi Bridge] Intercepted subtitle URL:",t),e.add(t);const o={};if(this.getAllResponseHeaders)try{this._kagiHeaders=o}catch{}window.dispatchEvent(new CustomEvent("kagi-subtitle-url-captured",{detail:{url:t,headers:o}}))}return u.apply(this,[i])},window.addEventListener("kagi-get-subtitle-data",async()=>{var i,t,o,s;try{const n=document.querySelector("#movie_player");let a={hasPlayer:!!n,hasGetOption:!1,hasGetPlayerResponse:!1,currentTrack:null,captionTracks:null,ytInitialData:null};if(n){if(a.hasGetOption=typeof n.getOption=="function",a.hasGetPlayerResponse=typeof n.getPlayerResponse=="function",a.hasGetOption)try{a.currentTrack=n.getOption("captions","track")}catch(r){console.error("[Kagi Bridge] Error getting current track:",r)}if(a.hasGetPlayerResponse)try{const r=n.getPlayerResponse();(t=(i=r==null?void 0:r.captions)==null?void 0:i.playerCaptionsTracklistRenderer)!=null&&t.captionTracks&&(a.captionTracks=r.captions.playerCaptionsTracklistRenderer.captionTracks)}catch(r){console.error("[Kagi Bridge] Error getting player response:",r)}}if(window.ytInitialPlayerResponse){const r=window.ytInitialPlayerResponse;(s=(o=r==null?void 0:r.captions)==null?void 0:o.playerCaptionsTracklistRenderer)!=null&&s.captionTracks&&(a.ytInitialData=r.captions.playerCaptionsTracklistRenderer.captionTracks)}window.dispatchEvent(new CustomEvent("kagi-subtitle-data",{detail:a}))}catch(n){console.error("[Kagi Bridge] Error accessing player:",n),window.dispatchEvent(new CustomEvent("kagi-subtitle-data",{detail:{error:n instanceof Error?n.toString():String(n)}}))}}),window.addEventListener("kagi-fetch-subtitle",async i=>{try{const{url:t}=i.detail;console.log("[Kagi Bridge] Fetching subtitle from:",t);const s=await(await fetch(t)).json();window.dispatchEvent(new CustomEvent("kagi-subtitle-fetched",{detail:{success:!0,data:s}}))}catch(t){window.dispatchEvent(new CustomEvent("kagi-subtitle-fetched",{detail:{success:!1,error:t instanceof Error?t.toString():String(t)}}))}})}};function y(){}function c(e,...l){}const d={debug:(...e)=>c(console.debug,...e),log:(...e)=>c(console.log,...e),warn:(...e)=>c(console.warn,...e),error:(...e)=>c(console.error,...e)};return(async()=>{try{return await p.main()}catch(e){throw d.error('The content script "youtube-player-bridge" crashed on startup!',e),e}})()}();
youtubePlayerBridge;