Skip to content

Instantly share code, notes, and snippets.

@qatoqat
Created May 6, 2025 05:39
Show Gist options
  • Select an option

  • Save qatoqat/5988a8028197dac4f775b27d4fb31bbc to your computer and use it in GitHub Desktop.

Select an option

Save qatoqat/5988a8028197dac4f775b27d4fb31bbc to your computer and use it in GitHub Desktop.
Auto Hide & Mute YouTube Sponsored Ads
// ==UserScript==
// @name Auto Hide & Mute YouTube Sponsored Ads
// @namespace http://tampermonkey.net/
// @version 1.0
// @description Mute and hide YouTube video when "Sponsored" text appears in the player overlay; otherwise unmute and show.
// @match https://www.youtube.com/*
// @grant none
// ==/UserScript==
(function () {
'use strict';
function getVideo() {
return document.querySelector('video.html5-main-video');
}
function checkSponsored() {
const video = getVideo();
if (!video) return;
const badge = document.querySelector('.ytp-ad-badge__text--clean-player');
const isSponsored = badge && badge.textContent.trim() === 'Sponsored';
if (isSponsored) {
video.muted = true;
video.style.visibility = 'hidden';
} else {
video.muted = false;
video.style.visibility = '';
}
}
function initObserver() {
const player = document.getElementById('movie_player');
if (!player) {
setTimeout(initObserver, 500);
return;
}
checkSponsored();
const observer = new MutationObserver(checkSponsored);
observer.observe(player, {
childList: true,
subtree: true,
characterData: true
});
}
window.addEventListener('yt-navigate-finish', initObserver, false);
initObserver();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment