Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save cytsai1008/555d5796f319cce5929f9ada034e5190 to your computer and use it in GitHub Desktop.
Save cytsai1008/555d5796f319cce5929f9ada034e5190 to your computer and use it in GitHub Desktop.
Disable YouTube profile video's auto play while keep auto play feature on normal video play.
// ==UserScript==
// @name Pause YouTube Videos on Profile Pages
// @namespace http://tampermonkey.net/
// @version 1.7
// @description Pause YouTube videos on profile pages while keeping autoplay on for video pages
// @author CYTsai
// @match https://www.youtube.com/*
// @grant none
// @icon https://www.google.com/s2/favicons?domain=youtube.com
// @downloadURL https://gist.github.com/cytsai1008/555d5796f319cce5929f9ada034e5190/raw/disable-youtube-auto-play-on-profile.user.js
// @updateURL https://gist.github.com/cytsai1008/555d5796f319cce5929f9ada034e5190/raw/disable-youtube-auto-play-on-profile.user.js
// ==/UserScript==
(function() {
'use strict';
function pauseVideo() {
let video = document.querySelector('video');
if (video) {
video.addEventListener('loadeddata', function() {
if (!video.paused) {
video.pause();
}
});
}
}
function checkUrlAndPauseVideo() {
let url = window.location.href;
let isUserProfilePage = /^https:\/\/www\.youtube\.com\/(user|channel|c|[^\/]+)$/.test(url) && !url.includes('/watch?') && !url.includes('/embed/');
if (isUserProfilePage) {
console.log("User Profile detected.");
pauseVideo();
}
}
let previousUrl = window.location.href;
function handleNavigation() {
const currentUrl = window.location.href;
if (currentUrl !== previousUrl) {
previousUrl = currentUrl;
checkUrlAndPauseVideo();
}
}
const observer = new MutationObserver(handleNavigation);
observer.observe(document.body, { childList: true, subtree: true });
window.addEventListener('yt-navigate-finish', handleNavigation);
window.addEventListener('load', checkUrlAndPauseVideo);
checkUrlAndPauseVideo();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment