YouTube 展开评论

自动展开六条以内的评论,支持手动缩起。

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         YouTube 展开评论
// @namespace    https://greasyfork.org/users/1171320
// @version      1.0
// @description  自动展开六条以内的评论,支持手动缩起。
// @author         yzcjd
// @author2       ChatGPT4 辅助
// @match        https://www.youtube.com/*
// @grant        none
// @license MIT
// ==/UserScript==

(function () {
    'use strict';

    const processedSet = new WeakSet();

    const clickLimitedReplies = () => {
        const buttons = document.querySelectorAll('ytd-comment-replies-renderer #more-replies');

        buttons.forEach(button => {
            // 已处理的按钮跳过
            if (processedSet.has(button)) return;

            // 查找按钮对应的回复数文本,例如 "查看 3 条回复"
            const text = button.innerText || "";

            const match = text.match(/(\d+)\s*条/);
            const count = match ? parseInt(match[1], 10) : NaN;

            if (!isNaN(count) && count <= 6) {
                button.click();
                processedSet.add(button); // 标记为已点击
            }
        });
    };

    const observer = new MutationObserver(() => {
        clickLimitedReplies();
    });

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

    // 初始延迟展开
    setTimeout(clickLimitedReplies, 2000);
})();