Greasy Fork

Greasy Fork is available in English.

隐藏b站视频详情页右侧的"活动推广"和"大家围观的直播"

Hide specified Bilibili elements using MutationObserver

当前为 2024-03-08 提交的版本,查看 最新版本

// ==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.2
// @description  Hide specified Bilibili elements using MutationObserver
// @description:en  Hide specified Bilibili elements using MutationObserver
// @author       aspen138
// @match        *://www.bilibili.com/video/*
// @grant        none
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    // Function to change the specified elements' background to white
    function whiteOutElements() {
        var adBanner = document.getElementById('right-bottom-banner');
        var liveCard = document.querySelector('.pop-live-small-mode.part-undefined');

        if (adBanner) {
            adBanner.style.background = 'white';
            adBanner.style.color = 'white'; // Hides text
            // Hide children elements by iterating through them
            Array.from(adBanner.children).forEach(child => {
                child.style.visibility = 'hidden';
            });
        }

        if (liveCard) {
            liveCard.style.background = 'white';
            liveCard.style.color = 'white'; // Hides text
            // Hide children elements by iterating through them
            Array.from(liveCard.children).forEach(child => {
                child.style.visibility = 'hidden';
            });
        }
    }

    // Create a MutationObserver to watch for changes in the DOM
    var observer = new MutationObserver(function(mutationsList) {
        // Check each mutation for added nodes
        for (var mutation of mutationsList) {
            if (mutation.type === 'childList') {
                // If added nodes are detected, change the elements' background to white
                whiteOutElements();
            }
        }
    });

    // Start observing changes in the body and its subtree
    observer.observe(document.body, { childList: true, subtree: true });

    // Initial change of elements' background to white when the script is executed
    whiteOutElements();
})();