Greasy Fork

Greasy Fork is available in English.

Google广告 全网站广告拦截

拦截所有网站的Google AdSense广告,移除广告节点,阻止广告脚本加载

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Google广告 全网站广告拦截
// @namespace    https://openuserjs.org/scripts/ner0/Google_AdSense_Ads_Remover
// @version      1.0.0
// @description  拦截所有网站的Google AdSense广告,移除广告节点,阻止广告脚本加载
// @author       ner0 & 社区优化
// @match        *://*/*
// @grant        none
// @license      MIT
// @run-at       document-start
// ==/UserScript==

(function() {
    'use strict';

    // 1. 拦截Google广告脚本加载
    const blockAdScripts = () => {
        const adScripts = document.querySelectorAll('script');
        adScripts.forEach(script => {
            // 匹配Google广告核心脚本域名
            if (
                script.src.includes('pagead2.googlesyndication.com') ||
                script.src.includes('adsbygoogle.js') ||
                script.src.includes('googleads.g.doubleclick.net') ||
                script.src.includes('static.doubleclick.net')
            ) {
                script.remove();
                script.type = 'javascript/blocked'; // 阻止脚本执行
                console.log('已拦截Google广告脚本:', script.src);
            }
        });
    };

    // 2. 移除已渲染的广告节点
    const removeAdNodes = () => {
        const adSelectors = [
            'ins.adsbygoogle', // AdSense核心广告容器
            'div[id*="google_ads_"]',
            'div[class*="google-ad"]',
            'iframe[src*="doubleclick.net"]',
            'iframe[src*="googleadservices.com"]',
            'div[data-google-av-ad-container]',
        ];

        adSelectors.forEach(selector => {
            const ads = document.querySelectorAll(selector);
            ads.forEach(ad => ad.remove());
        });
    };

    // 页面加载早期拦截脚本
    document.addEventListener('beforescriptexecute', (e) => {
        const src = e.target.src;
        if (
            src.includes('pagead2.googlesyndication.com') ||
            src.includes('adsbygoogle.js') ||
            src.includes('doubleclick.net')
        ) {
            e.preventDefault();
            e.stopPropagation();
            console.log('已提前阻止Google广告脚本执行');
        }
    });

    // 全周期执行拦截
    window.addEventListener('DOMContentLoaded', () => {
        blockAdScripts();
        removeAdNodes();
    });
    window.addEventListener('load', () => {
        blockAdScripts();
        removeAdNodes();
    });

    // 动态监听拦截
    const observer = new MutationObserver(() => {
        blockAdScripts();
        removeAdNodes();
    });
    observer.observe(document.documentElement, {
        childList: true,
        subtree: true
    });

})();