YouTube Ad Skipper

userscript console-script

Originally posted as GitHub Gist on 19 Apr 2019 , updated at 22 Jun 2021 (details)


Source: https://gist.github.com/paulera/671daf5b9a5f6502697359941ba524dc

Description: Close youtube ads as soon as they show (video ads and banners)


youtube_ad_skipper.js

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// ==UserScript==
// @name         AdSkipper
// @namespace    https://gist.github.com/paulera/671daf5b9a5f6502697359941ba524dc
// @version      1.3
// @description  Skip youtube ads automatically
// @author       paulera
// @match        https://www.youtube.com/watch?*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';
// ---------------------------------------------------------------------------


// This neat javascript piece monitors when there is an Ad in youtube and closes it.
// The Ad flashes sometimes, but still, it does the job.

/*
Bookmarklet. This is the same code as below, to add in the bookmarks bar for easy access.
javascript: console.log ("Starting AdSkipper."); if (window.adSkipTimer != null) { console.log ("Cancelling previous instance of Ad Skipper in this tab."); window.clearInterval(window.adSkipTimer); } var sleepInterval = 100; var adSkipperRepeatInterval = sleepInterval * 4; var sleep = function () { var now = new Date().getTime(); while ( new Date().getTime() < now + sleepInterval ) {} }; window.adSkipTimer = window.setInterval(function() { if (document.querySelector(".ytp-ad-overlay-close-button") != null) { console.log ("AdSkipper: skipping Ad!"); document.querySelector(".ytp-ad-overlay-close-button").click(); } else if (document.querySelector(".ytp-ad-button-icon") != null) { console.log ("AdSkipper: skipping Ad!"); document.querySelector(".ytp-ad-button-icon").click(); sleep(); document.querySelector(".ytp-ad-button.ytp-ad-info-dialog-mute-button.ytp-ad-button-link").click(); sleep(); document.querySelectorAll(".ytp-ad-feedback-dialog-reason-input")[2].click(); sleep(); document.querySelector(".ytp-ad-feedback-dialog-confirm-button").click(); } else if (document.querySelector(".style-scope.ytd-popup-container yt-confirm-dialog-renderer a.yt-simple-endpoint.style-scope.yt-button-renderer") != null) { console.log ("Youtube thinks you are away, let's slap that popup."); document.querySelector(".style-scope.ytd-popup-container yt-confirm-dialog-renderer a.yt-simple-endpoint.style-scope.yt-button-renderer").click(); } else if (document.querySelector(".ytd-mealbar-promo-renderer #dismiss-button a.yt-simple-endpoint.style-scope.ytd-button-renderer") != null) { console.log ("Ignoring Youtube premium offer."); document.querySelector(".ytd-mealbar-promo-renderer #dismiss-button a.yt-simple-endpoint.style-scope.ytd-button-renderer").click(); document.querySelector(".ytd-mealbar-promo-renderer").parentElement.remove(); } }, adSkipperRepeatInterval);
*/

/* ************************ Ad Skipper code *********************************** */

// Copy the code below and paste in the console to enable the Ad Skipper. Or use the bookmarklet above.

console.log ("Starting AdSkipper.");

// If the ad skipper have been invoked before, reset the timer
if (window.adSkipTimer != null) {
	console.log ("Cancelling previous instance of Ad Skipper in this tab.");
	window.clearInterval(window.adSkipTimer);
}

// this is the sleep interval in between actions. You might need to tweak it and increase if the ad skipper
// is not working properly
var sleepInterval = 100;
var adSkipperRepeatInterval = sleepInterval * 4; // time enough to run the processo of hiding the video Ad.

// javascript sleep function
var sleep = function () { var now = new Date().getTime(); while ( new Date().getTime() < now + sleepInterval ) {} };

// run the ad skipper every adSkipperRepeatInterval miliseconds
window.adSkipTimer = window.setInterval(function() {
    if (document.querySelector(".ytp-ad-skip-button.ytp-button") != null) {
        console.log ("AdSkipper: skipping Ad!");
        document.querySelector(".ytp-ad-skip-button.ytp-button").click();
    } else if (document.querySelector(".ytp-ad-overlay-close-button") != null) {
		console.log ("AdSkipper: skipping Ad!");
		document.querySelector(".ytp-ad-overlay-close-button").click();
	} else if (document.querySelector(".ytp-ad-button-icon") != null) {
		console.log ("AdSkipper: skipping Ad!");
 		document.querySelector(".ytp-ad-button-icon").click();
 		sleep();
 		document.querySelector(".ytp-ad-button.ytp-ad-info-dialog-mute-button.ytp-ad-button-link").click();
 		sleep();
 		document.querySelectorAll(".ytp-ad-feedback-dialog-reason-input")[2].click();
 		sleep();
 		document.querySelector(".ytp-ad-feedback-dialog-confirm-button").click();
	} else if (document.querySelector(".style-scope.ytd-popup-container yt-confirm-dialog-renderer a.yt-simple-endpoint.style-scope.yt-button-renderer") != null) {
		console.log ("Youtube thinks you are away, let's slap that popup.");
		document.querySelector(".style-scope.ytd-popup-container yt-confirm-dialog-renderer a.yt-simple-endpoint.style-scope.yt-button-renderer").click();
	} else if (document.querySelector(".ytd-mealbar-promo-renderer #dismiss-button a.yt-simple-endpoint.style-scope.ytd-button-renderer") != null) {
		console.log ("Ignoring Youtube premium offer.");
		document.querySelector(".ytd-mealbar-promo-renderer #dismiss-button a.yt-simple-endpoint.style-scope.ytd-button-renderer").click();
		document.querySelector(".ytd-mealbar-promo-renderer").parentElement.remove();
	}
}, adSkipperRepeatInterval);



// ---------------------------------------------------------------------------
})();