tieba

隐藏百度贴吧广告

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey, το Greasemonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

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

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Userscripts για να εγκαταστήσετε αυτόν τον κώδικα.

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

Θα χρειαστεί να εγκαταστήσετε μια επέκταση διαχείρισης κώδικα χρήστη για να εγκαταστήσετε αυτόν τον κώδικα.

(Έχω ήδη έναν διαχειριστή κώδικα χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Έχω ήδη έναν διαχειριστή στυλ χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

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