tyyy

try to take over the world!

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         tyyy
// @namespace    http://tampermonkey.net/
// @version      0.1.6
// @description  try to take over the world!
// @author       You
// @include      *://t66y.com/thread*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    class MinHeap {
        constructor(size, key) {
            this.size = size;
            this.objs = [];
            this.compareFunc = getCompareFunc(key);
            function getCompareFunc(key) {
                if(typeof(key) === "undefined"){
                    return (a, b) => {
                        if(a > b) return 1;
                        if(a < b) return -1;
                        return 0;
                    };
                } else {
                    return (a, b) => {
                        if(a[key] > b[key]) return 1;
                        if(a[key] < b[key]) return -1;
                        return 0;
                    };
                }
            }
        }

        insert(obj) {
            if(this.objs.length < this.size){
                this.objs.push(obj);
                return;
            }

            if(this.compareFunc(obj, this.objs[0]) === 1){
                this.objs.shift();
                this.objs.unshift(obj);
            }
            this.objs.sort(this.compareFunc);
        }
    }

    // Your code here...
    var $ = document.querySelector.bind(document);
    var $$ = document.querySelectorAll.bind(document);

    var table = $('#ajaxtable > tbody:nth-child(2)');
    highlightLines(table);

    function highlightLines(table){
        var trs = getValidLines(table);
        if(trs.length === 0) {
        	return;
        }
        var top15 = getTopReplied(trs, 15);
        const firstLine = trs[0];
        trs = trs.sort((a, b) => {
        	if(getReplyNumber(a) < getReplyNumber(b)) {
        		return -1;
        	}
        	if(getReplyNumber(a) > getReplyNumber(b)) {
        		return 1;
        	}
        	return 0;
        });
        trs.reverse();

        for(let tr of trs) {
        	firstLine.before(tr);
        }
        
        changeStyle(top15,'backgroundColor', '#66ccff');
        showTimeTag(trs);



        function showTimeTag(elements) {
            var todays = elements.filter(e => isToday(e.querySelector('td:nth-child(3) div').textContent));
            todays.forEach(e => e.querySelector('td:nth-child(1) a').textContent = 'T');

            var yestodays = elements.filter(e => isYestoday(e.querySelector('td:nth-child(3) div').textContent));
            yestodays.forEach(e => e.querySelector('td:nth-child(1) a').textContent = 'Y');

            function isToday(date) {
                var datetime = new Date(date);
                var now = new Date();
                return datetime.getFullYear() === now.getFullYear() &&
                    datetime.getMonth() === now.getMonth() &&
                    datetime.getDate() === now.getDate();
            }

            function isYestoday(date) {
                var datetime = new Date(date);
                var now = new Date();
                return datetime.getFullYear() === now.getFullYear() &&
                    datetime.getMonth() === now.getMonth() &&
                    datetime.getDate() + 1 === now.getDate();
            }
        }

        function getTopReplied(elements, size) {
            var replies = elements
                .map(e => parseInt(e.querySelector('td:nth-child(4)').textContent))
                .map((r, index) => ({
                    index: index,
                    replyNum: r
                }));
            var heap = new MinHeap(size, 'replyNum');
            replies.forEach(r => heap.insert(r));
            return heap.objs.map(o => elements[o.index]);
        }

        function getReplyNumber(tr) {
        	return parseInt(tr.querySelector('td:nth-child(4)').textContent);
        }

        function changeStyle(elements, key, value) {
            elements.forEach(tr => tr.style[key] = value);
        }
        function getValidLines(table) {
            var all = Array.from(table.querySelectorAll('tr'));
            var index = all.findIndex(tr => tr.querySelectorAll('.tr2 td').length === 1);
            all = all.slice(index + 1);
            return all.filter(tr => tr.className.includes('tr3'));
        }
    }

})();