Skip to content

Instantly share code, notes, and snippets.

@iRi-E
Last active April 27, 2024 15:28
Show Gist options
  • Save iRi-E/030d515c0990329630acebbf443442cb to your computer and use it in GitHub Desktop.
Save iRi-E/030d515c0990329630acebbf443442cb to your computer and use it in GitHub Desktop.
Userscript that automatically clicks a skip button on YouTube video ads
// ==UserScript==
// @name YouTube Autoskip Ads
// @namespace https://gist.github.com/iRi-E
// @version 0.3.2
// @description Automatically click skip button on YouTube video ads
// @author iRi_E
// @license MIT
// @match https://www.youtube.com/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=youtube.com
// @grant none
// ==/UserScript==
(function() {
'use strict';
let player = null;
let videoAds = null;
function findVideoAds(observer) {
videoAds = player.querySelector(':scope > .video-ads');
if (videoAds) {
observer.observe(videoAds, { attributeFilter: ['style'], subtree: true });
console.log('[YAA] Found video ads');
}
return videoAds;
}
const observer = new MutationObserver(() => {
if (videoAds || findVideoAds(observer)) {
const skipButton = videoAds.querySelector('.ytp-ad-skip-button-container, .ytp-skip-ad-button');
if (skipButton && skipButton.style.display != 'none') {
skipButton.click();
console.log('[YAA] Click skip button');
}
}
});
const timer = setInterval(() => {
player = document.getElementById('movie_player');
if (player) {
clearInterval(timer);
if (!findVideoAds(observer)) {
observer.observe(player, { childList: true });
}
console.log('[YAA] Observer started');
}
}, 1000);
console.log('[YAA] Waiting for player to load...');
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment