Skip to content

Instantly share code, notes, and snippets.

@ChrisLTD
Created March 16, 2021 13:51
Show Gist options
  • Save ChrisLTD/928b0b087ba5465348072114728c3e93 to your computer and use it in GitHub Desktop.
Save ChrisLTD/928b0b087ba5465348072114728c3e93 to your computer and use it in GitHub Desktop.
(function keepVideoFromSkipping() {
// modified from https://stackoverflow.com/a/39754847
function modifyBehavior(video) {
var timeTracking = {
watchedTime: 0,
currentTime: 0
};
var lastUpdated = 'currentTime';
video.addEventListener('timeupdate', function () {
if (!video.seeking) {
if (video.currentTime > timeTracking.watchedTime) {
timeTracking.watchedTime = video.currentTime;
lastUpdated = 'watchedTime';
}
//tracking time updated after user rewinds
else {
timeTracking.currentTime = video.currentTime;
lastUpdated = 'currentTime';
}
}
});
// prevent user from seeking
video.addEventListener('seeking', function () {
// guard against infinite recursion:
// user seeks, seeking is fired, currentTime is modified, seeking is fired, current time is modified, ....
var delta = video.currentTime - timeTracking.watchedTime;
if (delta > 0) {
video.pause();
//play back from where the user started seeking after rewind or without rewind
video.currentTime = timeTracking[lastUpdated];
video.play();
}
});
}
var existCondition = setInterval(function() {
var video = document.querySelector('.js-my-video');
if (video) {
clearInterval(existCondition);
modifyBehavior(video);
}
}, 100);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment