Greasy Fork

Greasy Fork is available in English.

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

Hide specified Bilibili elements using MutationObserver

当前为 2024-01-21 提交的版本,查看 最新版本

// ==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.1
// @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 hide the specified elements
    function hideElements() {
        var adBanner = document.getElementById('right-bottom-banner');
        var liveCard = document.querySelector('.pop-live-small-mode.part-undefined');

        if (adBanner) {
            adBanner.style.display = 'none';
        }

        if (liveCard) {
            liveCard.style.display = 'none';
        }
    }

    // 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, hide the elements
                hideElements();
            }
        }
    });

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

    // Initial hiding of elements when the script is executed
    hideElements();
})();