Toggle YouTube Heart Comments

Toggle YouTube heart comments.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Greasemonkey lub Violentmonkey.

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

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana będzie instalacja rozszerzenia Tampermonkey lub Userscripts.

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

Aby zainstalować ten skrypt, musisz zainstalować rozszerzenie menedżera skryptów użytkownika.

(Mam już menedżera skryptów użytkownika, pozwól mi to zainstalować!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Musisz zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

(Mam już menedżera stylów użytkownika, pozwól mi to zainstalować!)

// ==UserScript==
// @name        Toggle YouTube Heart Comments
// @namespace   https://github.com/gslin/toggle-youtube-heart-comments-userscript
// @match       https://www.youtube.com/*
// @grant       none
// @version     0.20210611.0
// @author      Gea-Suan Lin <[email protected]>
// @description Toggle YouTube heart comments.
// @license     MIT
// ==/UserScript==

(() => {
    'use strict;'

    let ob = new window.MutationObserver(mutations => {
        mutations.forEach(mutation => {
            mutation.addedNodes.forEach(() => {
                // Only filter pages whose path are "/watch".
                if (document.location.pathname !== '/watch') {
                    return;
                }

                let sheet = document.querySelector('.has-creator-heart-style');
                if (!sheet) {
                    sheet = document.createElement('style');
                    sheet.classList.add('has-creator-heart-style');
                    document.getElementsByTagName('head')[0].appendChild(sheet);
                }

                // Wait for ytd-comments element ready.
                let comments_el = document.querySelector('ytd-comments');
                if (!comments_el) {
                    return;
                }

                // Simulate "ytd-comment-thread-renderer:has(ytd-creator-heart-renderer)" selector.
                let comments_heart = document.querySelectorAll('ytd-comment-thread-renderer');
                comments_heart.forEach(el => {
                    if (el.querySelector('ytd-creator-heart-renderer')) {
                        el.classList.add('has-creator-heart');
                    }
                });

                // Don't install twice.
                if (document.getElementById('toggle_youtube_heart_comments')) {
                    return;
                }

                let toggle_el = document.createElement('div');
                toggle_el.innerHTML = '<button data-status="0" id="toggle_youtube_heart_comments">All (All / Heart / Non-Heart)</button>';

                let button_el = toggle_el.querySelector('button');
                button_el.addEventListener('click', () => {
                    if (button_el.dataset.status === '0') {
                        button_el.dataset.status = '1';
                        button_el.innerHTML = 'Heart (All / Heart / Non-Heart)';
                        sheet.innerHTML = 'ytd-comment-thread-renderer:not(.has-creator-heart) { display: none; }'
                    } else if (button_el.dataset.status === '1') {
                        button_el.dataset.status = '2';
                        button_el.innerHTML = 'Non-Heart (All / Heart / Non-Heart)';
                        sheet.innerHTML = 'ytd-comment-thread-renderer.has-creator-heart { display: none; }'
                    } else {
                        button_el.dataset.status = '0';
                        button_el.innerHTML = 'All (All / Heart / Non-Heart)';
                        sheet.innerHTML = '';
                    }
                });

                comments_el.insertAdjacentElement('beforebegin', toggle_el);
                console.debug('Toggle YouTube Heart Comments installed.');
            });
        });
    });

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