fix stuff + add stuff

This commit is contained in:
ashley 2025-10-06 19:47:45 +02:00
parent 89b405a639
commit f19c3c20f3

View File

@ -2,8 +2,7 @@
var _yt_player = videojs; var _yt_player = videojs;
var versionclient = "youtube.player.web_20250917_22_RC00" var versionclient = "youtube.player.web_20250917_22_RC00"
document.addEventListener("DOMContentLoaded", () => {
document.addEventListener("DOMContentLoaded", () => {
const video = videojs('video', { const video = videojs('video', {
controls: true, controls: true,
autoplay: false, autoplay: false,
@ -147,8 +146,9 @@ document.addEventListener("DOMContentLoaded", () => {
if (bothActivelyPlaying()) { if (bothActivelyPlaying()) {
pendingUnmute = false; pendingUnmute = false;
setTimeout(() => { setTimeout(() => {
try { video.muted(prevVideoMuted); } catch {} // Fix wrong mute restore
try { audio.muted = prevAudioMuted; } catch {} if (video.muted() && !prevVideoMuted) try { video.muted(false); } catch {}
if (audio.muted && !prevAudioMuted) try { audio.muted = false; } catch {}
}, 120); }, 120);
} }
} }
@ -174,6 +174,14 @@ document.addEventListener("DOMContentLoaded", () => {
try { const p = audio.play(); if (p && p.then) await p; } catch {} try { const p = audio.play(); if (p && p.then) await p; } catch {}
} }
// ensure no mismatch
if (!vOk && aOk) {
try { video.play().catch(() => showError('Video failed to start.')); } catch {}
}
if (vOk && !aOk) {
try { audio.play().catch(() => showError('Audio failed to start.')); } catch {}
}
if (!syncInterval) startSyncLoop(); if (!syncInterval) startSyncLoop();
} finally { syncing = false; } } finally { syncing = false; }
} }
@ -200,6 +208,15 @@ document.addEventListener("DOMContentLoaded", () => {
elm.addEventListener('loadedmetadata', onLoaded, { once: true }); elm.addEventListener('loadedmetadata', onLoaded, { once: true });
} }
const errorBox = document.getElementById('loopedIndicator');
function showError(msg) {
if (errorBox) {
errorBox.textContent = msg;
errorBox.style.display = 'block';
errorBox.style.width = 'fit-content';
}
}
function setupMediaSession() { function setupMediaSession() {
if ('mediaSession' in navigator) { if ('mediaSession' in navigator) {
try { try {
@ -239,12 +256,14 @@ document.addEventListener("DOMContentLoaded", () => {
const clamp = v => Math.max(0, Math.min(1, Number(v))); const clamp = v => Math.max(0, Math.min(1, Number(v)));
video.on('volumechange', () => { video.on('volumechange', () => {
try { audio.volume = clamp(video.volume()); audio.muted = video.muted(); } catch {} try {
if (!video.muted()) audio.volume = clamp(video.volume());
audio.muted = video.muted();
} catch {}
}); });
video.on('ratechange', () => { try { audio.playbackRate = video.playbackRate(); } catch {} }); video.on('ratechange', () => { try { audio.playbackRate = video.playbackRate(); } catch {} });
const errorBox = document.getElementById('loopedIndicator');
video.on('error', () => { video.on('error', () => {
const mediaError = video.error(); const mediaError = video.error();
let message = 'An unknown error occurred.'; let message = 'An unknown error occurred.';
@ -252,18 +271,14 @@ document.addEventListener("DOMContentLoaded", () => {
if (mediaError.code === 1) return; if (mediaError.code === 1) return;
message = `Error ${mediaError.code}: ${mediaError.message || 'No message provided'} try to refresh the page?`; message = `Error ${mediaError.code}: ${mediaError.message || 'No message provided'} try to refresh the page?`;
} }
if (errorBox) { showError(message);
errorBox.textContent = message;
errorBox.style.display = 'block';
errorBox.style.width = 'fit-content';
}
}); });
video.on('play', () => { markVPlaying(); if (!aIsPlaying) playTogether(); }); video.on('play', () => { markVPlaying(); if (!aIsPlaying) playTogether(); });
audio.addEventListener('play', () => { audio.addEventListener('play', () => {
markAPlaying(); markAPlaying();
if (!vIsPlaying) { if (!vIsPlaying) {
try { video.play().catch(()=>{}); } catch {} try { video.play().catch(() => showError('Video failed to start.')); } catch {}
} }
}); });
@ -294,7 +309,7 @@ document.addEventListener("DOMContentLoaded", () => {
video.on('play', recordPlayPause); video.on('play', recordPlayPause);
video.on('pause', recordPlayPause); video.on('pause', recordPlayPause);
// --- smarter seek handling: pause only on large jumps (>= 20s) --- // --- adaptive seek threshold ---
let wasPlayingBeforeSeek = false; let wasPlayingBeforeSeek = false;
let lastSeekTime = 0; let lastSeekTime = 0;
video.on('seeking', () => { video.on('seeking', () => {
@ -308,14 +323,17 @@ document.addEventListener("DOMContentLoaded", () => {
const newTime = Number(video.currentTime()); const newTime = Number(video.currentTime());
const seekDiff = Math.abs(newTime - lastSeekTime); const seekDiff = Math.abs(newTime - lastSeekTime);
// Only pause/resync if seek is large (>20 seconds difference) // threshold scales with video duration
if (seekDiff > 20) { const dur = Number(video.duration()) || 60;
const threshold = Math.max(5, Math.min(dur * 0.66, 60)); // e.g., 40s for 1min, 60s max
if (seekDiff > threshold) {
pauseTogether(); pauseTogether();
safeSetCT(audio, newTime); safeSetCT(audio, newTime);
setTimeout(() => { setTimeout(() => {
if (wasPlayingBeforeSeek && bothPlayableAt(newTime)) if (wasPlayingBeforeSeek && bothPlayableAt(newTime))
playTogether({ allowMutedRetry: true }); playTogether({ allowMutedRetry: true });
}, 180); }, 200);
} else { } else {
safeSetCT(audio, newTime); safeSetCT(audio, newTime);
} }