Skip to content

Instantly share code, notes, and snippets.

@Anderson-Juhasc
Last active January 5, 2026 13:28
Show Gist options
  • Select an option

  • Save Anderson-Juhasc/cf3d89e5e05ffd3a7143245659b8ffb6 to your computer and use it in GitHub Desktop.

Select an option

Save Anderson-Juhasc/cf3d89e5e05ffd3a7143245659b8ffb6 to your computer and use it in GitHub Desktop.
Youtube feature of repeat
(function () {
'use strict';
let currentVideo = null;
function setupRepeat(video) {
if (!video || video === currentVideo) return;
currentVideo = video;
console.log('[YT Replay] Repeat enabled');
// Native loop (most reliable)
video.loop = true;
// Fallback in case loop is disabled by YouTube
video.removeEventListener('ended', onEnded);
video.addEventListener('ended', onEnded);
}
function onEnded() {
console.log('[YT Replay] Video ended, restarting');
this.currentTime = 0;
this.play().catch(() => {
console.warn('[YT Replay] Autoplay blocked, waiting for user interaction');
});
}
function findVideo() {
const video = document.querySelector('video.html5-main-video');
if (video) setupRepeat(video);
}
// Observe DOM changes (YouTube is an SPA)
const observer = new MutationObserver(findVideo);
observer.observe(document.documentElement, {
childList: true,
subtree: true
});
// Initial attempt
findVideo();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment