YouTube: Expand All Video Comments(L2)

Adds a "Expand all" button to video comments which expands every comment and replies - no more clicking "Read more".

目前為 2025-09-28 提交的版本,檢視 最新版本

// ==UserScript==
// @name            YouTube: Expand All Video Comments(L2)
// @namespace       org.sidneys.userscripts
// @homepage        https://gist.githubusercontent.com/sidneys/6756166a781bd76b97eeeda9fb0bc0c1/raw/
// @version         4.7.8
// @description     Adds a "Expand all" button to video comments which expands every comment and replies - no more clicking "Read more".
// @author          sidneys
// @icon            https://www.youtube.com/favicon.ico
// @noframes
// @match           http*://www.youtube.com/*
// @require https://update.greasyfork.org/scripts/550956/1668280/Library%3A%20onElementReady%20ES7.js
// @run-at          document-end
// @grant           GM_addStyle
// @license      MIT
// ==/UserScript==


/* global Debug, onElementReady */

/**
 * ESLint
 * @global
 */
Debug = false


/**
 * Applicable URL paths
 * @default
 * @constant
 */
const urlPathList = [
    '/watch','/post/'
]


/**
 * Inject Stylesheet
 */
let injectStylesheet2 = () => {
    console.debug('injectStylesheet2')

    GM_addStyle(`
        /* =======================================
           ELEMENTS
           ======================================= */

        /* Button: Expand all Comments
           --------------------------------------- */

        .expand-all-comments-button2
        {
            padding: 0;
            align-self: start;
            margin: 0;
            font-size:20px;
            background: red;
        }

    `)
}

/**
 * Set global busy2 mode
 * @param {Boolean} isbusy2 - Yes/No
 * @param {String=} selector - Contextual element selector
 */
let setbusy2 = (isbusy2, selector = 'ytd-comments') => {
    // console.debug('setbusy2', 'isbusy2:', isbusy2)

    let element = document.querySelector(selector)

    if (isbusy2 === true) {
        element.classList.add('busy2')
        return
    } else {
        element.classList.remove('busy2')
    }
}

/**
 * Get Button element
 * @returns {Boolean} - On/Off
 */
let getbuttonElement2 = () => document.querySelector('.expand-all-comments-button2')

/**
 * Get toggle state2
 * @returns {Boolean} - On/Off
 */
//let gettoggleState2 = () => Boolean(getbuttonElement2() && getbuttonElement2().checked)


/**
 * Expand all comments
 */
let expandAllComments2 = () => {
    console.debug('expandAllComments2')

    // Look for "View X replies" buttons in comment section
    onElementReady('ytd-comment-replies-renderer #more-replies.ytd-comment-replies-renderer', false, (buttonElement2) => {

        //  Click button
        //buttonElement2.click()
    })
  //ExpandNestedReplies顯示更多回復
  /*
  onElementReady('ytd-comment-replies-renderer ytd-continuation-item-renderer button', false, (buttonElement2) => {

        //  Click button
        buttonElement2.click()
    })
    */
    // Look for "Read More" buttons in comment section顯示完整內容(ytd-comments刪除開頭,才能支援兼容post)
    onElementReady('tp-yt-paper-button.ytd-expander#more:not([hidden])', false, (buttonElement2) => {

        //  Click button
        buttonElement2.click()
    })
  //"less"隱藏"顯示部分內容"(ytd-comments刪除開頭,才能支援兼容post)
  onElementReady('tp-yt-paper-button.ytd-expander#less:not([hidden])', false, (buttonElement2) => {

        //  Click button
        buttonElement2.style.display = 'none';
    })
}


/**
 * Check if the toggle is enabled, if yes, start expanding
 */
let tryexpandAllComments2 = () => {
  expandAllComments2()


    // Abort if toggle disabled

}


/**
 * Render button: 'Expand all Comments'
 * @param {Element} element - Container element
 */
let renderButton2 = (element) => {
    console.debug('renderButton2')

    const buttonElement2 = document.createElement('tp-yt-paper-checkbox')
    buttonElement2.className = 'expand-all-comments-button2'
    buttonElement2.innerHTML = `
    <div id="icon-label" class="yt-dropdown-menu">
        M
    </div>
    `

    // Add button
    element.appendChild(buttonElement2)

    // Handle button toggle
    buttonElement2.onchange = tryexpandAllComments2
  //預設執行
buttonElement2.click()
    // Status
    console.debug('rendered button')
}


/**
 * init2
 */
let init2 = () => {
    console.info('init2')

    // Verify URL path

    // Add Stylesheet
    injectStylesheet2()

    // Wait for menu container
    onElementReady('ytd-comments ytd-comments-header-renderer > #title', false, (element) => {
        console.debug('onElementReady', 'ytd-comments ytd-comments-header-renderer > #title')

        // Render button
        renderButton2(element)
    })


}


/**
 * YouTube: Detect in-page navigation
 * @listens window:Event#yt-navigate-finish
 */
window.addEventListener('yt-navigate-finish', () => {

    init2()
})