Hacker News Comment Sorter

Sort Hacker News comments by various attributes

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Hacker News Comment Sorter
// @namespace    http://tampermonkey.net/
// @version      0.2
// @description  Sort Hacker News comments by various attributes
// @match        https://news.ycombinator.com/item?id=*
// @grant        none
// @author       Osiris-Team
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    function createSortButton(text, sortFunction) {
        const button = document.createElement('button');
        button.textContent = text;
        button.addEventListener('click', sortFunction);
        return button;
    }

    function getComments() {
        return Array.from(document.querySelectorAll('tr.athing.comtr'));
    }

    function sortComments(compareFn) {
        const comments = getComments();
        const sortedComments = comments.sort(compareFn);
        const commentList = document.querySelector('table.comment-tree');
        sortedComments.forEach(comment => commentList.appendChild(comment));
    }

    function sortByAge() {
        sortComments((a, b) => {
            const aAge = a.querySelector('.age').getAttribute('title');
            const bAge = b.querySelector('.age').getAttribute('title');
            return new Date(bAge) - new Date(aAge);
        });
    }

    function sortByUser() {
        sortComments((a, b) => {
            const aUser = a.querySelector('.hnuser').textContent.toLowerCase();
            const bUser = b.querySelector('.hnuser').textContent.toLowerCase();
            return aUser.localeCompare(bUser);
        });
    }

    function sortByIndentation() {
        sortComments((a, b) => {
            const aIndent = parseInt(a.querySelector('.ind img').getAttribute('width'));
            const bIndent = parseInt(b.querySelector('.ind img').getAttribute('width'));
            return aIndent - bIndent;
        });
    }

    function sortByScore() {
        sortComments((a, b) => {
            const aScore = parseInt(a.querySelector('.score')?.textContent) || 0;
            const bScore = parseInt(b.querySelector('.score')?.textContent) || 0;
            return bScore - aScore;
        });
    }

    function sortByCommentLength() {
        sortComments((a, b) => {
            const aLength = a.querySelector('.commtext').textContent.length;
            const bLength = b.querySelector('.commtext').textContent.length;
            return bLength - aLength;
        });
    }

    function sortByCommentId() {
        sortComments((a, b) => {
            const aId = parseInt(a.id);
            const bId = parseInt(b.id);
            return aId - bId;
        });
    }

    function sortByUserColor() {
        sortComments((a, b) => {
            const aColor = a.querySelector('.hnuser font')?.getAttribute('color') || '';
            const bColor = b.querySelector('.hnuser font')?.getAttribute('color') || '';
            return aColor.localeCompare(bColor);
        });
    }

    function init() {
        const sortingDiv = document.createElement('div');
        sortingDiv.style.margin = '10px 0';
        sortingDiv.appendChild(createSortButton('Sort by Age', sortByAge));
        sortingDiv.appendChild(createSortButton('Sort by User', sortByUser));
        sortingDiv.appendChild(createSortButton('Sort by Indentation', sortByIndentation));
        sortingDiv.appendChild(createSortButton('Sort by Score', sortByScore));
        sortingDiv.appendChild(createSortButton('Sort by Comment Length', sortByCommentLength));
        sortingDiv.appendChild(createSortButton('Sort by Comment ID', sortByCommentId));
        sortingDiv.appendChild(createSortButton('Sort by User Color', sortByUserColor));

        const commentSection = document.querySelector('table.comment-tree');
        commentSection.parentNode.insertBefore(sortingDiv, commentSection);
    }

    init();
})();