Block Ads

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

当前为 2024-03-14 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==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();
    });
})();