Created
May 6, 2025 05:39
-
-
Save qatoqat/5988a8028197dac4f775b27d4fb31bbc to your computer and use it in GitHub Desktop.
Auto Hide & Mute YouTube Sponsored Ads
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ==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