tieba

隐藏百度贴吧广告

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         tieba
// @namespace    http://tampermonkey.net/
// @version      2025-05-07
// @description  隐藏百度贴吧广告
// @author       You
// @match        https://tieba.baidu.com/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=microsoft.com
// @grant        GM_log
// @license MIT
// ==/UserScript==

(function() {
    'use strict';
    GM_log("Init tieba");
    // 目标类名
    let targetClassNames = ['custom-custom-ad-container','ylh-ad-container'];

    // 隐藏广告函数
    function hideAdContainers() {
        targetClassNames.forEach(v=>{
            const adContainers = document.getElementsByClassName(v);
            for (let container of adContainers) {
                container.style.display = 'none';
                console.log('已隐藏广告容器:', container);
            }

            if (adContainers.length > 0) {
                console.log(`隐藏了 ${adContainers.length} 个广告容器`);
            }
        })
    }

    // 初始执行
    hideAdContainers();

    // 使用MutationObserver监听DOM变化
    const observer = new MutationObserver(function(mutations) {
        hideAdContainers();
    });

    // 开始观察DOM变化
    observer.observe(document.body, {
        childList: true,
        subtree: true
    });

    // 监听history变化(应对SPA页面导航)
    let pushState = history.pushState;
    history.pushState = function() {
        pushState.apply(history, arguments);
        setTimeout(hideAdContainers, 100);
    };

    let replaceState = history.replaceState;
    history.replaceState = function() {
        replaceState.apply(history, arguments);
        setTimeout(hideAdContainers, 100);
    };

    window.addEventListener('popstate', function() {
        setTimeout(hideAdContainers, 100);
    });
})();