Lightweight YouTube AdBlock

Blocks ads on YouTube without eating up your resources

As of 2023-05-23. See the latest version.

// ==UserScript==
// @name         Lightweight YouTube AdBlock
// @version      1.4
// @description  Blocks ads on YouTube without eating up your resources
// @author       equmaq
// @match        https://www.youtube.com/*
// @license      MIT
// @namespace https://greasyfork.org/users/990886
// ==/UserScript==

(function() {
    'use strict';

    // Function to remove YouTube video ads and specified elements
    function removeAds() {
        // Remove the video ads
        var ads = document.querySelectorAll('.ad-showing');
        ads.forEach(function(ad) {
            ad.remove();
        });

        // Remove the overlay ads
        var overlays = document.querySelectorAll('.ytp-ad-overlay-slot');
        overlays.forEach(function(overlay) {
            overlay.remove();
        });

        // Remove the first specified element by full XPath
        var targetElement1 = document.evaluate("/html/body/ytd-app/div[1]/ytd-page-manager/ytd-browse/ytd-two-column-browse-results-renderer/div[1]/ytd-rich-grid-renderer/div[3]", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
        if (targetElement1) {
            targetElement1.remove();
        }

        // Remove the second specified element by full XPath
        var targetElement2 = document.evaluate("/html/body/ytd-app/div[1]/ytd-page-manager/ytd-browse/ytd-two-column-browse-results-renderer/div[1]/ytd-rich-grid-renderer/div[3]/ytd-banner-promo-renderer/div", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
        if (targetElement2) {
            targetElement2.remove();
        }

        // Remove the third specified element by full XPath
        var targetElement3 = document.evaluate("[Full x-path of element to remove]", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
        if (targetElement3) {
            targetElement3.remove();
        }

        // Remove the fourth specified element by full XPath
        var targetElement4 = document.evaluate("[Full x-path of element to remove]", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
        if (targetElement4) {
            targetElement4.remove();
        }

        // Remove the fift specified element by full XPath
        var targetElement5 = document.evaluate("[Full x-path of element to remove]", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
        if (targetElement5) {
            targetElement5.remove();
        }

        // Remove the sixth specified element by full XPath
        var targetElement6 = document.evaluate("[Full x-path of element to remove]", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
        if (targetElement6) {
            targetElement6.remove();
        }

        // Resume video playback if paused due to an ad
        var player = document.querySelector('video');
        if (player && player.paused) {
            player.play();
        }
    }

    // Mutation observer to detect and remove new ads
    var observer = new MutationObserver(function(mutations) {
        mutations.forEach(function(mutation) {
            var addedNodes = mutation.addedNodes;
            for (var i = 0; i < addedNodes.length; i++) {
                var node = addedNodes[i];
                if (node.classList && (node.classList.contains('ad-showing') || node.classList.contains('ytp-ad-overlay-slot'))) {
                    removeAds();
                }
            }
        });
    });

    // Configuration of the mutation observer
    var config = { childList: true, subtree: true };

    // Start observing the changes
    observer.observe(document.body, config);

    // Remove ads on initial page load
    removeAds();
})();