// in the beginning.... god made mrrprpmnaynayaynaynayanyuwuuuwmauwnwanwaumawp :p var _yt_player = videojs; var versionclient = "youtube.player.web_20250917_22_RC00" document.addEventListener("DOMContentLoaded", () => { const player = videojs("video", { controls: true, autoplay: false, preload: "auto", errorDisplay: false, }); const qs = new URLSearchParams(location.search); const qua = qs.get("quality") || ""; const vidKey = qs.get("v"); try { if (vidKey) localStorage.setItem(`progress-${vidKey}`, 0); } catch {} // raw media nodes const videoEl = document.getElementById("video"); const audioEl = document.getElementById("aud"); // keep the hidden audio truly hidden/quiet try { audioEl.controls = false; audioEl.setAttribute("aria-hidden", "true"); audioEl.setAttribute("tabindex", "-1"); audioEl.setAttribute("controlslist", "noplaybackrate nodownload noremoteplayback"); audioEl.disableRemotePlayback = true; videoEl.setAttribute("playsinline", ""); audioEl.setAttribute("playsinline", ""); audioEl.preload = "auto"; } catch {} // initial source probes const pickAudioSrc = () => { const s = audioEl?.getAttribute?.("src"); if (s) return s; const child = audioEl?.querySelector?.("source"); if (child?.getAttribute?.("src")) return child.getAttribute("src"); if (audioEl?.currentSrc) return audioEl.currentSrc; return null; }; let audioSrc = pickAudioSrc(); const srcObj = player.src(); const initialVideoSrc = Array.isArray(srcObj) ? (srcObj[0] && srcObj[0].src) : srcObj; const initialVideoType = Array.isArray(srcObj) ? (srcObj[0] && srcObj[0].type) : undefined; // small state bag let audioReady = false, videoReady = false; let mediaSessionReady = false; let desiredPlay = false; // user's last intent (play/pause) let buffering = false; // are we buffering right now? // sync knobs const BIG_DRIFT = 0.45; // snap threshold (s) const MICRO_DRIFT = 0.035; // subtle rate nudge (s) const EPS = 0.12; // buffer epsilon (s) // guards let volGuard = false; let syncGuard = false; let couplingGuard = false; // autosync + stall watchdog let autosyncTimer = null; const AUTOSYNC_MS = 400; // stall detection let lastProgressCT = 0; let lastProgressAt = 0; const STALL_MS = 800; // if time doesn't advance for this long → buffering // loop detection let lastVideoTime = 0; // helpers function timeInBuffered(media, t) { try { const br = media.buffered; if (!br || br.length === 0 || !isFinite(t)) return false; for (let i = 0; i < br.length; i++) { const s = br.start(i) - EPS, e = br.end(i) + EPS; if (t >= s && t <= e) return true; } } catch {} return false; } function canPlayAt(media, t) { try { const rs = Number(media.readyState || 0); if (!isFinite(t)) return false; if (rs >= 3) return true; // HAVE_FUTURE_DATA return timeInBuffered(media, t); } catch { return false; } } function bothPlayableAt(t) { return canPlayAt(videoEl, t) && canPlayAt(audioEl, t); } function safeSetCT(media, t) { try { if (isFinite(t) && t >= 0) media.currentTime = t; } catch {} } const clamp01 = v => Math.max(0, Math.min(1, Number(v))); function isLooping() { try { return !!player.loop?.() || !!videoEl.loop; } catch { return !!videoEl.loop; } } // quick play/pause helpers function playBoth() { if (buffering) return; // do not play while buffering if (couplingGuard) return; couplingGuard = true; player.play()?.catch(()=>{}); audioEl.play()?.catch(()=>{}); couplingGuard = false; } function pauseBoth() { if (couplingGuard) return; couplingGuard = true; player.pause(); audioEl.pause(); couplingGuard = false; } // mute/volume mirror function mirrorFromPlayerVolumeMute() { if (volGuard) return; volGuard = true; try { const m = !!player.muted(); const v = clamp01(player.volume()); audioEl.muted = m; if (!m) audioEl.volume = v; try { videoEl.muted = m; } catch {} } catch {} volGuard = false; } function mirrorFromAudioVolumeMute() { if (volGuard) return; volGuard = true; try { const m = !!audioEl.muted; const v = clamp01(audioEl.volume); player.muted(m); if (!m) player.volume(v); try { videoEl.muted = m; } catch {} } catch {} volGuard = false; } player.on("volumechange", mirrorFromPlayerVolumeMute); audioEl.addEventListener("volumechange", mirrorFromAudioVolumeMute); player.ready(() => mirrorFromPlayerVolumeMute()); // track user's intent and enforce coupling player.on("play", () => { desiredPlay = true; if (buffering) { pauseBoth(); return; } if (audioEl.paused) audioEl.play()?.catch(()=>{}); try { if ("mediaSession" in navigator) navigator.mediaSession.playbackState = "playing"; } catch {} }); player.on("pause", () => { desiredPlay = false; if (!audioEl.paused) audioEl.pause(); try { if ("mediaSession" in navigator) navigator.mediaSession.playbackState = "paused"; } catch {} }); // kill autonomous audio play/pause unless matching desired state audioEl.addEventListener("play", () => { if (!desiredPlay) pauseBoth(); if (buffering) pauseBoth(); }); audioEl.addEventListener("pause", () => { if (desiredPlay && !buffering) playBoth(); }); // media-session timeline let lastMSPos = 0, lastMSAt = 0; const MS_THROTTLE_MS = 250; function getDuration() { let d = Number(videoEl.duration); if (!isFinite(d) || d <= 0) d = Number(player.duration()); if (!isFinite(d) || d <= 0) d = Number(audioEl.duration); return isFinite(d) && d > 0 ? d : null; } function updateMSPositionState(throttle = true) { if (!("mediaSession" in navigator)) return; const now = performance.now(); if (throttle && (now - lastMSAt) < MS_THROTTLE_MS) return; const dur = getDuration(); if (!dur) return; let pos = Number(player.currentTime()); if (!isFinite(pos) || pos < 0) pos = 0; if (pos + 0.2 < lastMSPos && isLooping()) lastMSPos = 0; else { if (pos < lastMSPos && (lastMSPos - pos) < 0.2) pos = lastMSPos; lastMSPos = pos; } lastMSAt = now; try { if ("setPositionState" in navigator.mediaSession) { navigator.mediaSession.setPositionState({ duration: dur, playbackRate: Number(player.playbackRate()) || 1, position: Math.max(0, Math.min(dur, pos)), }); } } catch {} } function setupMediaSession() { if (mediaSessionReady) return; if (!("mediaSession" in navigator)) return; try { navigator.mediaSession.metadata = new MediaMetadata({ title: document.title || "Video", artist: "", album: "", artwork: [] }); } catch {} const tagState = () => { try { navigator.mediaSession.playbackState = player.paused() ? "paused" : "playing"; } catch {} updateMSPositionState(false); }; function msSeekTo(targetTime) { const wasDesired = desiredPlay; desiredPlay = wasDesired; // keep intent pauseBoth(); // pause hard during seek to avoid ping-pong syncGuard = true; try { if ("fastSeek" in audioEl) { try { audioEl.fastSeek(targetTime); } catch { safeSetCT(audioEl, targetTime); } } else { safeSetCT(audioEl, targetTime); } player.currentTime(targetTime); } finally { syncGuard = false; } tagState(); // only resume if playable and the user wanted play if (wasDesired && bothPlayableAt(Number(player.currentTime()))) playBoth(); } navigator.mediaSession.setActionHandler("play", () => { desiredPlay = true; if (!buffering) playBoth(); tagState(); }); navigator.mediaSession.setActionHandler("pause", () => { desiredPlay = false; pauseBoth(); tagState(); }); navigator.mediaSession.setActionHandler("stop", () => { desiredPlay = false; pauseBoth(); try { player.currentTime(0); } catch {} try { audioEl.currentTime = 0; } catch {} tagState(); }); navigator.mediaSession.setActionHandler("seekbackward", ({ seekOffset }) => { const skip = seekOffset || 10; msSeekTo(Math.max(0, Number(player.currentTime()) - skip)); }); navigator.mediaSession.setActionHandler("seekforward", ({ seekOffset }) => { const skip = seekOffset || 10; msSeekTo(Number(player.currentTime()) + skip); }); navigator.mediaSession.setActionHandler("seekto", ({ seekTime }) => { if (!isFinite(seekTime)) return; msSeekTo(seekTime); }); player.on("timeupdate", () => updateMSPositionState(true)); player.on("ratechange", () => updateMSPositionState(false)); player.on("loadedmetadata", () => { lastMSPos = 0; updateMSPositionState(false); }); player.on("durationchange", () => updateMSPositionState(false)); player.on("play", () => { try { navigator.mediaSession.playbackState = "playing"; } catch {} updateMSPositionState(false); }); player.on("pause", () => { try { navigator.mediaSession.playbackState = "paused"; } catch {} updateMSPositionState(false); }); audioEl.addEventListener("loadedmetadata", () => updateMSPositionState(false)); audioEl.addEventListener("timeupdate", () => updateMSPositionState(true)); mediaSessionReady = true; } // media keys fallback document.addEventListener("keydown", e => { switch (e.code) { case "AudioPlay": case "MediaPlayPause": desiredPlay = player.paused(); if (desiredPlay && !buffering) playBoth(); else pauseBoth(); break; case "AudioPause": desiredPlay = false; pauseBoth(); break; case "AudioNext": case "MediaTrackNext": { const t = Number(player.currentTime()) + 10; pauseBoth(); player.currentTime(t); safeSetCT(audioEl, t); if (desiredPlay && bothPlayableAt(t)) playBoth(); break; } case "AudioPrevious": case "MediaTrackPrevious": { const t = Math.max(0, Number(player.currentTime()) - 10); pauseBoth(); player.currentTime(t); safeSetCT(audioEl, t); if (desiredPlay && bothPlayableAt(t)) playBoth(); break; } } }); // single sync step function doSyncOnce() { if (syncGuard) return; const vt = Number(player.currentTime()); const at = Number(audioEl.currentTime); if (!isFinite(vt) || !isFinite(at)) return; // stall watchdog: track real progress if (!player.paused()) { if (vt !== lastProgressCT) { lastProgressCT = vt; lastProgressAt = Date.now(); } else if ((Date.now() - lastProgressAt) > STALL_MS) { // considered buffering → hard pause both buffering = true; pauseBoth(); } } // loop wrap if (vt + 0.02 < lastVideoTime && isLooping()) { safeSetCT(audioEl, vt); lastMSPos = 0; updateMSPositionState(false); lastVideoTime = vt; return; } lastVideoTime = vt; // if buffering or not playable, skip sync if (buffering || !bothPlayableAt(vt)) return; const delta = vt - at; if (Math.abs(delta) > BIG_DRIFT) { safeSetCT(audioEl, vt); try { audioEl.playbackRate = 1; } catch {} return; } if (Math.abs(delta) > MICRO_DRIFT) { const target = 1 + (delta * 0.12); try { audioEl.playbackRate = Math.max(0.97, Math.min(1.03, target)); } catch {} } else { try { audioEl.playbackRate = 1; } catch {} } } // event-driven sync player.on("timeupdate", doSyncOnce); // small autosync (also runs stall watchdog) function startAutosync() { if (autosyncTimer) return; autosyncTimer = setInterval(doSyncOnce, AUTOSYNC_MS); } function stopAutosync() { if (!autosyncTimer) return; clearInterval(autosyncTimer); autosyncTimer = null; } // treat any of these as "buffering now" → pause both, resume when playable function markBuffering() { buffering = true; pauseBoth(); } function tryUnbufferAndResume() { // become unbuffered only when both sides can actually play at this time const t = Number(player.currentTime()); if (bothPlayableAt(t)) { buffering = false; if (desiredPlay) playBoth(); } } player.on("waiting", markBuffering); player.on("stalled", markBuffering); player.on("suspend", markBuffering); player.on("emptied", markBuffering); player.on("canplay", tryUnbufferAndResume); player.on("canplaythrough", tryUnbufferAndResume); player.on("playing", () => { tryUnbufferAndResume(); startAutosync(); }); // simple error surfacing const errorBox = document.getElementById("loopedIndicator"); player.on("error", () => { const mediaError = player.error(); let message = "An unknown error occurred."; if (mediaError) { if (mediaError.code === 1) return; message = `Error ${mediaError.code} : ${mediaError.message || "No message provided"} try to refresh the page?`; } if (errorBox) { errorBox.textContent = message; errorBox.style.display = "block"; errorBox.style.width = "fit-content"; } }); // loop/end handling player.on("ended", () => { if (isLooping()) { safeSetCT(audioEl, 0); player.currentTime(0); if (!buffering && desiredPlay) playBoth(); lastMSPos = 0; updateMSPositionState(false); } else { desiredPlay = false; pauseBoth(); stopAutosync(); } }); audioEl.addEventListener("ended", () => { if (isLooping()) { safeSetCT(audioEl, 0); if (desiredPlay && !buffering && player.paused()) { player.currentTime(0); playBoth(); } lastMSPos = 0; updateMSPositionState(false); } else { stopAutosync(); } }); // leave fullscreen → match intent but never play while buffering document.addEventListener("fullscreenchange", () => { if (!document.fullscreenElement) { desiredPlay = false; pauseBoth(); } }); // tiny, quiet retry for