window.addEventListener('DOMContentLoaded', (event) => { const video = document.getElementById('player_video'); const audio = document.getElementById('player_audio'); if (localStorage.getItem("volume") != null) { setVolume(localStorage.getItem("volume")); } const playButton = document.querySelector('.ytp-button[aria-label="Play"]'); function toggleVideo(play) { const { classList } = playButton; const shouldPlay = typeof play === 'boolean' ? play : video.paused; try { if (shouldPlay) { classList.remove('ytp-button-play'); classList.add('ytp-button-pause'); if (video.ended) { video.currentTime = 0; audio.currentTime = 0; } video.play(); if (audio.src) { audio.play(); } } else { classList.remove('ytp-button-pause'); classList.add('ytp-button-play'); video.pause(); if (audio.src) { audio.pause(); } } } catch (e) { console.error(e); } } function updateTime() { document.querySelector('.ytp-time-current').innerHTML = Math.floor(video.currentTime / 60) + ':' + Math.floor(video.currentTime % 60) .toString() .padStart(2, '0'); document.querySelector('.ytp-time-duration').innerHTML = Math.floor(video.duration / 60) + ':' + Math.floor(video.duration % 60) .toString() .padStart(2, '0'); document.querySelector('.ytp-play-progress').style.transform = 'scaleX(' + video.currentTime / video.duration + ')'; document.querySelector('.html5-scrubber-button').style.left = (video.currentTime / video.duration) * 100 + '%'; } function scrub(e) { const w = document.querySelector('.ytp-progress-list').getBoundingClientRect(); const amount = (e.clientX - w.x) / w.width; video.currentTime = video.duration * amount; audio.currentTime = audio.duration * amount; console.log(e); } function volume(e) { const v = document.querySelector('.ytp-volume-slider').getBoundingClientRect(); const amount = (e.clientX - v.x) / v.width; setVolume(amount); } function setVolume(amount) { if (audio.src) { audio.volume = amount; } else { video.volume = amount; } document.querySelector('.ytp-volume-slider-foreground').style.left = amount * 51 + 'px'; localStorage.setItem("volume", video.volume); const icon = document.querySelector('.ytp-button-volume'); if (amount == 0) { icon.dataset.value = 'off'; } else if (amount < 0.2) { icon.dataset.value = 'min'; } else if (amount < 0.4) { icon.dataset.value = 'quiet'; } else if (amount < 0.6) { icon.dataset.value = 'normal'; } else if (amount < 0.8) { icon.dataset.value = 'loud'; } else { icon.dataset.value = 'max'; } } function toggleFullscreen() { if (document.fullscreenElement || document.mozFullScreenElement || document.webkitFullscreenElement) { if (document.mozCancelFullScreen) { document.mozCancelFullScreen(); } else if (document.cancelFullScreen) { document.cancelFullScreen(); } else if (document.webkitCancelFullScreen) { document.webkitCancelFullScreen(); } } else { movie = document.getElementById('movie_player'); if (movie.requestFullscreen) { movie.requestFullscreen(); } else if (movie.mozRequestFullScreen) { movie.mozRequestFullScreen(); } else if (movie.webkitRequestFullscreen) { movie.webkitRequestFullscreen(); } } } const formats = JSON.parse(document.getElementById('yt_formats').innerText) .filter((fmt) => ['http', 'https'].includes(fmt.protocol)) .sort((a, b) => { if (a.vcodec === 'none' && b.vcodec !== 'none') { return -1; } if (a.vcodec !== 'none' && b.vcodec === 'none') { return 1; } if (a.vcodec !== 'none' && b.vcodec !== 'none') { return b.height - a.height; } if (a.acodec !== 'none' && b.acodec !== 'none') { return b.abr - a.abr; } }); console.log(formats); const videoFormat = formats.find((fmt) => fmt.vcodec !== 'none'); const audioFormat = videoFormat.acodec === 'none' ? formats.find((fmt) => fmt.acodec !== 'none') : null; console.log('selected video format', videoFormat); console.log('selected audio format', audioFormat); video.src = videoFormat.url; if (audioFormat) { audio.src = audioFormat.url; } // document.querySelector('.ytp-button-play').addEventListener('click', () => toggleVideo()); playButton.addEventListener('click', () => toggleVideo()); video.addEventListener('click', () => toggleVideo()); video.addEventListener('dblclick', () => toggleFullscreen()); let videoWaiting = false; let audioWaiting = false; video.addEventListener('waiting', () => { toggleVideo(false); videoWaiting = true; }); video.addEventListener('playing', () => { videoWaiting = false; console.log(`videoWaiting ${videoWaiting}, audioWaiting ${audioWaiting}`); if (!audioWaiting) { toggleVideo(true); } }); audio.addEventListener('waiting', () => { toggleVideo(false); audioWaiting = true; }); audio.addEventListener('playing', () => { audioWaiting = false; console.log(`videoWaiting ${videoWaiting}, audioWaiting ${audioWaiting}`); if (!videoWaiting) { toggleVideo(true); } }); video.addEventListener('timeupdate', () => updateTime()); video.addEventListener('ended', () => toggleVideo(false)); // just to change the classes document.querySelector('.ytp-progress-list').addEventListener('click', (e) => scrub(e)); // document.querySelector('.ytp-play-progress').addEventListener('click', (e) => scrub(e)); document.querySelector('.ytp-volume-slider').addEventListener('click', (e) => volume(e)); document.querySelector('.ytp-button-fullscreen-enter').addEventListener('click', () => toggleFullscreen()); // autoplay toggleVideo(true); });