Block Ads

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

目前為 2024-03-14 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 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();
    });
})();