隐藏b站视频详情页右侧的"活动推广"和"大家围观的直播",评论区和视频简介之间的广告,弹幕下方的广告

Hide specified Bilibili elements using MutationObserver

// ==UserScript==
// @name         隐藏b站视频详情页右侧的"活动推广"和"大家围观的直播",评论区和视频简介之间的广告,弹幕下方的广告
// @name:en      Hide the promotion on the right side of Bilibili's video details page
// @namespace    http://tampermonkey.net/
// @version      0.1.10
// @description  Hide specified Bilibili elements using MutationObserver
// @description:en  Hide specified Bilibili elements using MutationObserver
// @author       aspen138
// @match        *://www.bilibili.com/video/*
// @icon         https://www.bilibili.com/favicon.ico
// @grant        none
// @license      MIT
// ==/UserScript==






(function() {
    'use strict';

    // Function to change the specified elements' background to white, prevent clicks, and disable hover effects
    function whiteOutElements() {
        var adBanner = document.getElementById('right-bottom-banner');
        var liveCard = document.querySelector('.pop-live-small-mode.part-1');
        var adFloorCover = document.querySelector('.ad-floor-cover.b-img'); // Select the additional element
        var bannerAd = document.getElementById('bannerAd'); // Select the <a> element with id="bannerAd"
        var vcdElement = document.querySelector('.vcd'); // Select the element with the class 'vcd'
        var adLinkElement = document.querySelector('a[data-loc-id="4331"]'); // Select the <a> element using the specific data-loc-id attribute
        var activityVote = document.getElementById('activity_vote'); // Select the 'activity_vote' element by ID

        const preventClicksAndHideElementStyles = (element) => {
            element.style.background = 'white';
            element.style.color = 'white'; // Hides text
            element.style.pointerEvents = 'none'; // Disables mouse events including hover and clicks

            // Hide children elements by iterating through them
            Array.from(element.children).forEach(child => {
                child.style.visibility = 'hidden';
                preventClicksAndHideElementStyles(child); // Apply recursively to child elements
            });
        };

        if (adBanner) {
            preventClicksAndHideElementStyles(adBanner);
        }

        if (liveCard) {
            preventClicksAndHideElementStyles(liveCard);
        }

        if (adFloorCover) { // Apply to the additional element
            preventClicksAndHideElementStyles(adFloorCover);
        }

        if (bannerAd) { // Apply to the <a> element
            preventClicksAndHideElementStyles(bannerAd);
        }

        if (vcdElement) { // Apply to the 'vcd' class element
            preventClicksAndHideElementStyles(vcdElement);
        }

        if (adLinkElement) { // Apply to the new <a> element
            preventClicksAndHideElementStyles(adLinkElement);
        }

        if (activityVote) { // Apply to the 'activity_vote' element
            preventClicksAndHideElementStyles(activityVote);
        }

        // Hide the new element
        if (document.getElementById('slide_ad')) {
            preventClicksAndHideElementStyles(document.getElementById('slide_ad'));
        }
    }


    // Function to remove a specified element by its class name
    function removeElementByClassName(className) {
        var elements = document.getElementsByClassName(className);
        Array.from(elements).forEach(element => {
            element.remove();
        });
    }

    // Function to remove a specified element by its ID
    function removeElementById(elementId) {
        var element = document.getElementById(elementId);
        if (element) {
            element.remove();
        }
    }

    // Create a MutationObserver to watch for changes in the DOM
    var observer = new MutationObserver(function(mutationsList) {
        for (var mutation of mutationsList) {
            if (mutation.type === 'childList') {
                whiteOutElements(); // Apply changes when new nodes are added
                removeElementById('activity_vote'); // Remove the 'activity_vote' element when new nodes are added
            }
        }
    });

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

    // Initial application of the changes
    whiteOutElements();
    setTimeout(whiteOutElements, 5 * 1000);
    removeElementById('activity_vote'); // Remove the 'activity_vote' element initially
    setTimeout(()=>{removeElementByClassName('ad-report video-card-ad-small');}, 5*1000);
})();