YouTube - Enable Live Chat By Default

Automatically changes the chat view to Live Chat when it loads. (Works with chat replays, too.)

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name        YouTube - Enable Live Chat By Default
// @namespace   Violentmonkey Scripts
// @description Automatically changes the chat view to Live Chat when it loads. (Works with chat replays, too.)
// @match       https://www.youtube.com/live_chat*
// @include     https://*youtube.*/live_chat*
// @include     https://www.youtube.*/live_chat*
// @include     https://www.youtube.*/live_chat
// @grant       none
// @version     1.0.1
// @author      Jupiter Liar
// @license     CC BY
// @description 3/18/2024, 4:00:00 PM
// ==/UserScript==

(function() {
    'use strict';

    var maxAttempts = 400;
    var currentAttempt = 0;

    // Function to switch chat mode to "Live Chat"
    function changeToLiveChat(liveChatSelector) {
        //console.log("Live chat selector found.");
        // Check if the live chat selector exists
        if (liveChatSelector) {
            // Click the tp-yt-paper-button to switch to "Live Chat" mode
            var paperButton = liveChatSelector.querySelector('tp-yt-paper-button');
            if (paperButton) {
                paperButton.click();
                //console.log("Attempting to switch to Live Chat mode...");
                checkPaperMenuItems();
            } else {
                //console.log("tp-yt-paper-button not found within live chat selector.");
            }
        }
    }

    // Function to click on the appropriate paper item in the paper menu
    function checkPaperMenuItems() {
        var paperMenuItems = document.querySelectorAll('tp-yt-paper-item');
        var foundLiveChat = false;
        var foundUnselected = false;

        paperMenuItems.forEach(function(item) {
            var text = item.innerText.toLowerCase();
            if (text.includes("live chat")) {
                item.click();
                //console.log("Clicked on paper item with 'Live chat'.");
                foundLiveChat = true;
            } else {
                var parentAnchor = item.closest('a');
                if (parentAnchor && parentAnchor.getAttribute('aria-selected') === "false") {
                    item.click();
                    //console.log("Clicked on unselected paper item.");
                    foundUnselected = true;
                }
            }
        });

        if (!foundLiveChat && !foundUnselected) {
            //console.log("Unable to find appropriate paper menu item.");
        }
    }

    // Function to check if the page contains the live chat selector
    function checkLiveChatSelector() {
        //console.log("Attempting to run Live Chat changer script.");
        var liveChatSelector = document.querySelector('#live-chat-view-selector-sub-menu');
        if (liveChatSelector) {
            changeToLiveChat(liveChatSelector);
        }
    }

    // Execute when the page and its resources are fully loaded
    window.onload = checkLiveChatSelector;
})();