Last active
September 16, 2021 12:25
-
-
Save paulera/671daf5b9a5f6502697359941ba524dc to your computer and use it in GitHub Desktop.
Close youtube ads as soon as they show (video ads and banners)
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 AdSkipper | |
// @namespace https://gist.github.com/paulera/671daf5b9a5f6502697359941ba524dc | |
// @version 1.3 | |
// @description Skip youtube ads automatically | |
// @author paulera | |
// @match https://www.youtube.com/* | |
// @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."),null!=window.adSkipTimer&&(console.log("Cancelling previous instance of Ad Skipper in this tab."),window.clearInterval(window.adSkipTimer));var sleepInterval=100,adSkipperRepeatInterval=4*sleepInterval,sleep=function(){for(var e=(new Date).getTime();(new Date).getTime()<e+sleepInterval;);};window.adSkipTimer=window.setInterval(function(){null!=document.querySelector(".ytp-ad-skip-button.ytp-button")?(console.log("AdSkipper: skipping Ad!"),document.querySelector(".ytp-ad-skip-button.ytp-button").click()):null!=document.querySelector(".ytp-ad-overlay-close-button")?(console.log("AdSkipper: skipping Ad!"),document.querySelector(".ytp-ad-overlay-close-button").click()):null!=document.querySelector(".ytp-ad-button-icon")?(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()):null!=document.querySelector(".style-scope.ytd-popup-container yt-confirm-dialog-renderer a.yt-simple-endpoint.style-scope.yt-button-renderer")?(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()):null!=document.querySelector(".ytd-mealbar-promo-renderer #dismiss-button a.yt-simple-endpoint.style-scope.ytd-button-renderer")&&(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); | |
// --------------------------------------------------------------------------- | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment