GitHub Top Reacted

Show and sort top reacted comments on GitHub

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name            GitHub Top Reacted
// @version         0.1
// @description     Show and sort top reacted comments on GitHub
// @author          Drazen Bjelovuk
// @include         /^https?://github.com/.*/issues/.+/
// @grant           none
// @namespace       https://greasyfork.org/users/11679
// @contributionURL https://goo.gl/dYIygm
// @run-at          document-end
// ==/UserScript==

(function() {
    var comments = document.querySelectorAll('.timeline-comment-wrapper');
    var reacted = [];

    comments.forEach(function(el) {
        if (el.querySelectorAll('.has-reactions').length) {
            reacted.push(el);
        }
    });

    if (!reacted.length) {
        return;
    }

    function countReactions(comment) {
        var count = 0;
        var reactions = comment.querySelectorAll('button.reaction-summary-item');
        reactions.forEach(function(el) {
            count += parseInt(el.innerText.split(' ')[1]);
        });
        return count;
    }

    reacted.sort(function(a, b) {
        return countReactions(b) - countReactions(a);
    });

    var container = document.createElement('div');
    container.className = 'top-reacted';
    container.setAttribute('hidden', '');
    reacted.forEach(function(el) {
        container.appendChild(el.cloneNode(true));
    });

    var discussion = document.querySelector('.discussion-timeline');
    discussion.insertBefore(container, discussion.firstChild);

    var showTopBtn = document.createElement('button');
    showTopBtn.className = 'btn btn-lg';
    showTopBtn.style = 'margin-bottom: 20px;';
    showTopBtn.innerText = 'Show top reacted';
    showTopBtn.addEventListener('click', function() {
        if (container.hasAttribute('hidden')) {
            container.removeAttribute('hidden');
            showTopBtn.innerText = 'Hide top reacted';
        }
        else {
            container.setAttribute('hidden', '');
            showTopBtn.innerText = 'Show top reacted';
        }
    });

    var bucket = document.querySelector('#discussion_bucket');
    bucket.parentNode.insertBefore(showTopBtn, bucket);
})();