Greasy Fork

Kbin Collapsible Comments

On the KBin website, support collapsing and expanding comments

目前为 2023-06-11 提交的版本。查看 最新版本

// ==UserScript==
// @name         Kbin Collapsible Comments
// @namespace    http://tampermonkey.net/
// @version      0.4
// @description  On the KBin website, support collapsing and expanding comments
// @author       CodingAndCoffee
// @match        https://kbin.social/m/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=kbin.social
// @grant        none
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    const COLLAPSE_PARENTS_BY_DEFAULT = false;

    const getNumericId = function (comment) {
        return comment.id.split("-").reverse()[0];
    };

    const getComment = function (numericId) {
        return document.querySelector('#comments blockquote#entry-comment-' + numericId);
    };

    const getChildrenOf = function (numericId) {
        return document.querySelectorAll('#comments blockquote[data-subject-parent-value="' + numericId + '"]');
    }

    const getDescendentsOf = function (numericId) {
        var parent = getComment(numericId);
        var children = getChildrenOf(numericId);

        var descendents = [];
        children.forEach(function (child) {
            descendents.push(child);
            var childDescendents = getDescendentsOf(getNumericId(child));
            childDescendents.forEach (function (cd) {
                descendents.push(cd);
            });
        });
        return descendents;
    };

    const makeCollapseLabel = function (isVisible, childrenCount) {
        var label = (isVisible ? '- collapse' : '+ show');
        return (childrenCount > 0 ?
            '[' + label + ' ' + childrenCount + ']' :
            '[' + label + ']'
        );
    };

    window.toggleChildren = function (numericId) {
        var parent = getComment(numericId);
        var figure = parent.querySelector('figure');
        var footer = parent.querySelector('footer');
        var content = parent.querySelector('.content');

        // get visibility status
        var childrenVisible = (parent.dataset['childrenVisible'] === 'true');
        var toggledStatus = !childrenVisible;

        // update dataset
        parent.setAttribute('data-children-visible', toggledStatus);
        if (!COLLAPSE_PARENTS_BY_DEFAULT) {
            if (toggledStatus) {
                content.style.display = '';
                footer.style.display = '';
                figure.style.display = '';
                parent.style.height = '';
            } else {
                content.style.display = 'none';
                footer.style.display = 'none';
                figure.style.display = 'none';
                parent.style.height = '40px';
                parent.style.paddingTop = '0.53rem';
            }
        }

        // toggle visibility of the descendents
        var descendents = getDescendentsOf(numericId);
        descendents.forEach(function(c) {
            c.style.display = (toggledStatus ? 'grid' : 'none');
        });

        // update the link text
        var childrenCount = parent.dataset['childrenCount'];
        if (childrenCount > 0) {
            var link = document.querySelector('#comment-'+numericId+'-collapse-link');
            if (link) {
                link.innerHTML = makeCollapseLabel(toggledStatus, childrenCount);
            }
        }
    };

    const comments = document.querySelectorAll('#comments blockquote.comment');
    comments.forEach(function (comment) {
        var numericId = getNumericId(comment);
        var children = getChildrenOf(numericId);
        var childrenCount = children.length;
        // add some metadata
        comment.setAttribute('data-children-visible', true);
        comment.setAttribute('data-children-count', childrenCount);

        var header = comment.querySelector('header');
        var span = document.createElement('span');
        var spanSpacer = document.createTextNode(' | ');
        span.append(spanSpacer);
        header.appendChild(span);

        var a = document.createElement('a');
        a.id = 'comment-'+numericId+'-collapse-link';
        var link = document.createTextNode(makeCollapseLabel(true, childrenCount));
        a.appendChild(link);
        a.href = 'javascript:window.toggleChildren('+numericId+')';
        span.appendChild(a);
    });

    if (COLLAPSE_PARENTS_BY_DEFAULT) {
        comments.forEach(function (comment) {
            var numericId = getNumericId(comment);

            var isTopLevel = (typeof comment.dataset['subject-parent-value'] !== 'string');
            if (isTopLevel) {
                window.toggleChildren(numericId);
            }
        });
    }
})();