节点延迟排序

try to take over the world!

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         节点延迟排序
// @namespace    http://tampermonkey.net/
// @version      0.3
// @description  try to take over the world!
// @author       StephZard
// @include     *://*.monster*
// @grant        none
// ==/UserScript==

(function() {
    var tableDom = document.querySelector('.table') // 获取table对象
    var tbodyDom = tableDom.querySelector('tbody') // 获取tbody对象
    addEvent()
    // 添加事件
    function addEvent () {
        var thDomArr = tbodyDom.firstChild.querySelectorAll('th') // 获取th对象数组
        thDomArr.forEach((v, i) => {
            if (i > 0) {
                v.style.cursor = 'pointer'
                v.addEventListener('click', function () {
                    debounce(sortBySpeed.call(v, i))
                })
            }
        })
    }
    // 点击事件 - 根据速度排序
    var sortBySpeed = debounce(function (index) {
        var trDomArr = Array.from(tbodyDom.querySelectorAll('tr'))
        trDomArr.forEach((v, i) => {
            if (i > 0) { // 第一个节点不处理
                v.aaa = parseFloat(nthNode.call(v, 'td', index).innerText) // 添加当前速度值
            } else { // 第一个节点速度默认为0
                v.aaa = 0
            }
        })
        trDomArr.sort(function (a, b) {
            return a.aaa - b.aaa
        })
        tbodyDom.innerHTML = ''
        trDomArr.forEach(v => {
            tbodyDom.appendChild(v)
        })
        addEvent()
    })
    // 获取第n个节点
    function nthNode (name, n) {
        const nodeArr = this.querySelectorAll(name)
        return nodeArr[n]
    }
    // 延迟点击
    function debounce (func, delay) {
        let timer
        delay = delay || 200
        return function (...args) {
            return new Promise((resolve, reject) => {
                if (timer) {
                    clearTimeout(timer)
                }
                timer = setTimeout(() => {
                    resolve(func.apply(this, args))
                }, delay)
            })
        }
    }
})();