Hide Promotion Advertisement at Zhihu Site

Hide specific advertisement elements on Zhihu

// ==UserScript==
// @name         Hide Promotion Advertisement at Zhihu Site
// @namespace    http://tampermonkey.net/
// @version      0.1.4
// @description  Hide specific advertisement elements on Zhihu
// @author       aspen138
// @match        *://*.zhihu.com/*
// @grant        none
// @license      MIT
// ==/UserScript==




(function() {
    'use strict';

    // Function to hide the advertisement elements
    function hideAds() {
        // Hide elements with class name 'Pc-word-card'
        var adElements1 = document.querySelectorAll('.Pc-word-card');
        adElements1.forEach(function(element) {
            element.style.display = 'none';
        });

        // Hide elements with class name 'Banner-link' (common in ads)
        var adElements2 = document.querySelectorAll('.Banner-link');
        adElements2.forEach(function(element) {
            element.style.display = 'none';
        });

        // Hide elements with class name 'Banner-adTag' (label for ads)
        var adElements3 = document.querySelectorAll('.Banner-adTag');
        adElements3.forEach(function(element) {
            element.style.display = 'none';
        });

        // Hide elements with class containing 'AdvertImg' (common in ads)
        var adElements4 = document.querySelectorAll('.AdvertImg');
        adElements4.forEach(function(element) {
            element.style.display = 'none';
        });

        // Hide iframe advertisements
        var adIframes = document.querySelectorAll('iframe[src*="baidu.com"]');
        adIframes.forEach(function(iframe) {
            iframe.style.display = 'none';
        });

        // Hide close button on ads (optional)
        var closeButtons = document.querySelectorAll('.Pc-card-button-close');
        closeButtons.forEach(function(button) {
            button.style.display = 'none';
        });

        // Hide elements with class name 'TopstoryItem--advertCard'
        var adElements5 = document.querySelectorAll('.TopstoryItem--advertCard');
        adElements5.forEach(function(element) {
            element.style.display = 'none';
        });

        // Hide the element with class 'Pc-Business-Card-PcTopFeedBanner'
        var adElements6 = document.querySelectorAll('.Pc-Business-Card-PcTopFeedBanner');
        adElements6.forEach(function(element) {
            element.style.display = 'none';
        });
    }

    // Run the function to hide the elements when the page loads
    window.addEventListener('load', hideAds);

    // Observe the page for dynamic content loading and hide ads accordingly
    var observer = new MutationObserver(hideAds);
    observer.observe(document.body, { childList: true, subtree: true });

    // Function to remove the ad element
    function removeAdElement() {
        const adElement = document.querySelector('.Business-Card-PcRightBanner-link');
        if (adElement) {
            adElement.remove();
        }
    }

    // Wait for the page to load before removing the element
    window.addEventListener('load', removeAdElement);

    // Also observe for any dynamic content loading
    const observer1 = new MutationObserver(() => {
        removeAdElement();
    });

    observer1.observe(document.body, { childList: true, subtree: true });

})();