YouTube - Lazy Load Comments

Загружает комментарии на YouTube только по клику.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         YouTube - Lazy Load Comments
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  Загружает комментарии на YouTube только по клику.
// @author       You
// @match        https://www.youtube.com/watch*
// @grant        GM_addStyle
// @license MIT 
// ==/UserScript==

(function() {
    'use strict';

    // Сразу скрываем комментарии
    GM_addStyle('#comments { display: none; }');

    function createLoadButton() {
        const commentsSection = document.querySelector('#comments');
        if (!commentsSection || document.getElementById('load-comments-btn')) {
            return;
        }

        const button = document.createElement('button');
        button.id = 'load-comments-btn';
        button.textContent = '👇 Показать комментарии';
        button.style.cssText = `
            width: 100%;
            padding: 15px;
            font-size: 16px;
            font-weight: bold;
            cursor: pointer;
            border: 1px solid #ccc;
            border-radius: 8px;
            margin: 20px 0;
            background-color: #f0f0f0;
        `;
        // Стили для темной темы
        if (document.documentElement.hasAttribute('dark')) {
            button.style.backgroundColor = '#272727';
            button.style.borderColor = '#444';
            button.style.color = '#fff';
        }


        commentsSection.parentNode.insertBefore(button, commentsSection);

        button.addEventListener('click', () => {
            commentsSection.style.display = 'block';
            button.remove();
        }, { once: true }); // Обработчик сработает только один раз
    }

    // YouTube - одностраничное приложение, поэтому нужно следить за изменениями
    const observer = new MutationObserver(() => {
        // Мы ищем не сам блок #comments, а его "окрестности", чтобы вставить кнопку
        if (document.querySelector('#below')) {
            createLoadButton();
        }
    });

    // Начинаем наблюдение после загрузки основной части страницы
    window.addEventListener('yt-navigate-finish', () => {
        // Даем небольшую задержку, чтобы элементы успели появиться
        setTimeout(createLoadButton, 500);
        observer.observe(document.body, { childList: true, subtree: true });
    });

    // Для первой загрузки страницы
    setTimeout(createLoadButton, 1000);

})();