sortColumns

Sort Redactle guessed-words table by a column

이 스크립트를 설치하려면 Tampermonkey, Greasemonkey 또는 Violentmonkey와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 Tampermonkey 또는 Violentmonkey와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 Tampermonkey 또는 Violentmonkey와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 Tampermonkey 또는 Userscripts와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 Tampermonkey와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 유저 스크립트 관리자 확장 프로그램이 필요합니다.

(이미 유저 스크립트 관리자가 설치되어 있습니다. 설치를 진행합니다!)

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 유저 스타일 관리자 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 유저 스타일 관리자 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 유저 스타일 관리자 확장 프로그램이 필요합니다.

(이미 유저 스타일 관리자가 설치되어 있습니다. 설치를 진행합니다!)

// ==UserScript==
// @name         sortColumns
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  Sort Redactle guessed-words table by a column
// @author       gauss256
// @match        https://www.redactle.com/
// @icon         https://www.google.com/s2/favicons?sz=64&domain=redactle.com
// @grant        none
// @license      MIT
// ==/UserScript==

/* jshint esversion:6 */

// The lovely code to do the sorting comes from:
// https://stackoverflow.com/a/50127768/80309

(function () {
    'use strict';

    function hash(node) {
        return parseInt(node.querySelector('td:nth-child(1)').innerHTML);
    }

    function guess(node) {
        return node.querySelector('td:nth-child(2)').innerHTML;
    }

    function hits(node) {
        return parseInt(node.querySelector('td:nth-child(3)').innerHTML);
    }

    function sortByHash() {
        const guessLogBody = document.getElementById('guessLogBody');
        [...guessLogBody.children]
            .sort((a, b) => hash(b) - hash(a))
            .forEach(node => guessLogBody.appendChild(node));
    }

    function sortByGuess() {
        const guessLogBody = document.getElementById('guessLogBody');
        [...guessLogBody.children]
            .sort((a, b) => guess(a) > guess(b))
            .forEach(node => guessLogBody.appendChild(node));
    }

    function sortByHits() {
        const guessLogBody = document.getElementById('guessLogBody');
        [...guessLogBody.children]
            .sort((a, b) => hits(b) - hits(a))
            .forEach(node => guessLogBody.appendChild(node));
    }

    // Set up click events on the column headers
    const columnHeads = document
        .getElementById('tableHolder')
        .querySelectorAll('th');
    let hashBtn;
    let guessBtn;
    let hitsBtn;
    for (let i = 0; i < columnHeads.length; i++) {
        let headElem = columnHeads[i];
        let headLabel = headElem.innerHTML;
        switch(headLabel) {
            case '#':
                hashBtn = headElem;
                break;
            case 'Guess':
                guessBtn = headElem;
                break;
            default:
                hitsBtn = headElem;
        }
    }
    hashBtn.style = 'cursor: pointer';
    guessBtn.style = 'cursor: pointer';
    hitsBtn.style = 'cursor: pointer';

    // Set up event handlers
    hashBtn.onclick = function () {
        sortByHash();
    };
    guessBtn.onclick = function () {
        sortByGuess();
    };
    hitsBtn.onclick = function () {
        sortByHits();
    };

})();