intrepidOlivia / Youtube Comment Viewer

// ==UserScript==
// @name         Youtube Comment Viewer
// @namespace    pixelstomp.com
// @version      1.2
// @description  Scroll through comments while video is still visible.
// @author       intrepidOlivia
// @match        http*://*.youtube.com/*
// icon          https://www.youtube.com/s/desktop/8800d6c0/img/favicon.ico
// @grant        GPL
// @license      GPL-3.0-or-later
// ==/UserScript==

(function() {
    'use strict';

    function setPage() {
        // Rearrange sections
        const related = document.getElementById('related');
        const relatedParent = related.parentElement;
        const comments = document.getElementById('comments');
        const commentParent = comments.parentElement;
        commentParent.appendChild(related);
        relatedParent.appendChild(comments);

        // Scroll through comments
        comments.style.overflowY = 'auto';
        setInterval(() => {
            const commentRect = comments.getBoundingClientRect();
            if (commentRect.top < window.innerHeight) {
                comments.style.maxHeight = `${window.innerHeight - commentRect.top + 20}px`;
            }
        }, 1000);

        // redirect scrolling events
        function commentListener(event) {
            window.dispatchEvent(new Event('scroll', event));
        }
        comments.addEventListener('scroll', commentListener);
    }

    setPage();
})();