Block Ads

Block ads integrated into webpages via iframes, CSS, JavaScript, Bootstrap, and other tools.

// ==UserScript==
// @name         Block Ads
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  Block ads integrated into webpages via iframes, CSS, JavaScript, Bootstrap, and other tools.
// @author       iamnobody
// @license      MIT
// @match        *://*/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // Function to remove elements matching a given selector
    function removeElements(selector) {
        const elements = document.querySelectorAll(selector);
        elements.forEach(element => {
            element.remove(); // Remove the element from the DOM
        });
    }

    // Remove ads integrated via iframes
    removeElements('iframe[src*="ad"], iframe[src*="ads"], iframe[src*="banner"]');

    // Remove elements with ad-related class names
    removeElements('.ad, .ads, .advert, .advertisement, .banner, .promo');

    // Remove elements with ad-related IDs
    removeElements('[id*="ad"], [id*="ads"], [id*="banner"], [id*="promo"], [id*="sponsor"]');

    // Remove elements with ad-related CSS styles
    removeElements('[style*="ad"], [style*="ads"], [style*="banner"], [style*="promo"], [style*="sponsor"]');

    // Remove elements with ad-related JavaScript events
    removeElements('[onmouseover*="ad"], [onmouseout*="ad"], [onmouseenter*="ad"], [onmouseleave*="ad"], [onload*="ad"], [onclick*="ad"], [onfocus*="ad"], [onblur*="ad"], [onkeypress*="ad"], [onkeydown*="ad"], [onkeyup*="ad"], [onsubmit*="ad"], [onreset*="ad"], [onchange*="ad"], [ondblclick*="ad"], [onmousedown*="ad"], [onmouseup*="ad"]');

    // Remove elements with ad-related Bootstrap classes
    removeElements('.navbar-ad, .sidebar-ad, .footer-ad, .jumbotron-ad');

    // Remove elements with ad-related attributes
    removeElements('[data-ad], [data-ads], [data-banner], [data-promo], [data-sponsor]');

    // Function to remove ad-related elements
    function removeAds() {
        // Remove iframes
        document.querySelectorAll('iframe').forEach(iframe => {
            iframe.remove();
        });

        // Remove script tags
        document.querySelectorAll('script').forEach(script => {
            script.remove();
        });

        // Remove elements by class or ID
        const adClasses = ['ad', 'advertisement', 'ad-container'];
        adClasses.forEach(className => {
            document.querySelectorAll(`.${className}`).forEach(adElement => {
                adElement.remove();
            });
        });

        // Remove specific elements by ID
        const adIds = ['ad-banner', 'ad-sidebar'];
        adIds.forEach(id => {
            const adElement = document.getElementById(id);
            if (adElement) {
                adElement.remove();
            }
        });
    }

    // Run the ad blocking function when the page is fully loaded
    window.addEventListener('load', function() {
        removeAds();
    });
})();