Skip to content

Instantly share code, notes, and snippets.

@clayfreeman
Last active January 16, 2025 01:15
Show Gist options
  • Save clayfreeman/14061c45935ce7b6359c486fe55859c6 to your computer and use it in GitHub Desktop.
Save clayfreeman/14061c45935ce7b6359c486fe55859c6 to your computer and use it in GitHub Desktop.
Automatically restart video playback when buffering
// ==UserScript==
// @name Twitch Playback Fix
// @version 2024-11-20
// @description Automatically restart video playback when buffering.
// @author Clay Freeman
// @match http*://*.twitch.tv/*
// @grant none
// ==/UserScript==
(function () {
'use strict';
const video = document.querySelector('video');
if (video) {
console.debug('Waiting to attach event listener for Twitch Playback Fix...');
setTimeout(() => {
video.addEventListener('pause', ({ target }) => {
// Ignore videos with a duration of 48 hours or less.
//
// This ensures that only live streams are affected by this script since
// VODs can only be up to 48 hours long.
if (target instanceof HTMLMediaElement && target.duration > 60 * 60 * 24 * 2) {
setTimeout(() => {
if (document.querySelector('[data-a-player-state="playing"]')) {
console.warn('Buffer was starved; restarting playback ...');
target.currentTime = 0;
}
});
}
});
console.debug('Attached event listener for Twitch Playback Fix.');
}, 30_000);
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment