Bilibili 根据是否存在置顶评论自动屏蔽评论区

检测 Bilibili 页面是否有置顶评论,并根据结果显示或隐藏评论区。

// ==UserScript==
// @name         Bilibili 根据是否存在置顶评论自动屏蔽评论区
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  检测 Bilibili 页面是否有置顶评论,并根据结果显示或隐藏评论区。
// @author       Cyke
// @match        https://www.bilibili.com/video/*
// @grant        none
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    // 定义一个函数来检查是否有置顶评论
    function checkForPinnedComment() {
        let pinnedComment;
        try {
            // 查找置顶评论的元素
            pinnedComment = document.querySelector("#commentapp > bili-comments")
                .shadowRoot.querySelector("#feed > bili-comment-thread-renderer:nth-child(1)")
                .shadowRoot.querySelector("#comment")
                .shadowRoot.querySelector("#top");
        } catch (e) {
            //console.log('正常报错,不要慌', e);
            return;
        }

        // 获取评论区容器
        const commentSection = document.querySelector("#commentapp > bili-comments");
        if (pinnedComment) {
            // 如果有置顶评论,确保评论区是可见的
            if (commentSection) {
                commentSection.style.display = 'block';
                //console.log('置顶评论已找到,评论区已显示');
            }
        } else {
            // 如果没有置顶评论,隐藏评论区
            if (commentSection) {
                commentSection.style.display = 'none';
                //console.log('没有置顶评论,评论区已隐藏');
            }
        }
    }

    // 由于 Bilibili 页面可能会动态加载评论,设置一个定时器定期检查
    setInterval(checkForPinnedComment, 2000);

    // 初始化时立即执行一次检查
    checkForPinnedComment();
})();