NOTICE: By continued use of this site you understand and agree to the binding Terms of Service and Privacy Policy.
// ==UserScript== // @name Youtube enhancer // @namespace youtube.com // @version 1.1.2 // @match https://*.youtube.com/watch* // @license MIT // ==/UserScript== let listenerIsSet = false; let manualPause = false; let videoSrc = ""; let startTime = Date.now(); let startTimeOffset = 0; let elapsedTime = 0.0; let pausedTime = 0.0; let jumps = 0; (function () { let manualPause = false; let videoSrc = ''; let startTime = Date.now(); let startTimeOffset = 0; let elapsedTime = 0.0; let pausedTime = 0.0; let jumps = 0; let freqCounterLast = 0; const initializationInterval = setInterval(() => { const video = document.querySelector('video'); if (video) { clearInterval(initializationInterval); registerKeyDownListener(); startPlaybackTracking(); } }, 120); const registerKeyDownListener = () => { document.addEventListener('keydown', event => { if (event.keyCode === 19 || event.keyCode === 32 || event.keyCode === 75) { manualPause = !manualPause; } }); }; const startPlaybackTracking = () => { setInterval(() => { const video = document.querySelector('video'); if (videoSrc === video.currentSrc) { /*video cannot be played => prevent the reset of the playback*/ if (video.readyState < 3) { pausedTime = Date.now() - (startTime + elapsedTime); if (jumps !== video.played.length) { startTime = Date.now(); video.currentTime = Math.round((elapsedTime + startTimeOffset) / 1000); } } /*video can be played => track the playback time*/ if (video.readyState > 2) { if (video.paused) { pausedTime = Date.now() - (startTime + elapsedTime); } else if (jumps === video.played.length) { elapsedTime = Date.now() - (startTime + pausedTime); } else { jumps = video.played.length; freqCounterLast = 0; elapsedTime = 0.0; pausedTime = 0.0; startTime = Date.now(); startTimeOffset = Math.round(video.currentTime * 1000); } } } else //initial load { videoSrc = video.currentSrc; jumps = 0; freqCounterLast = 0; elapsedTime = 0.0; pausedTime = 0.0; startTime = Date.now(); startTimeOffset = Math.round(video.currentTime * 1000); } let freqCounterActual = Math.floor((elapsedTime + pausedTime) / 1000); if (!manualPause && freqCounterLast !== freqCounterActual) { freqCounterLast = freqCounterActual; const wrapperClassList = document.querySelector('.html5-video-player').classList; if (wrapperClassList.contains('paused-mode') || (wrapperClassList.contains('unstarted-mode') && jumps) || (wrapperClassList.contains('unstarted-mode') && /&index=\d+/.test(window.location.href))) { video.click(); } } }, 240); }; })();