table-sorter

传统表格排序

Stan na 06-10-2024. Zobacz najnowsza wersja.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Greasemonkey lub Violentmonkey.

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

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana będzie instalacja rozszerzenia Tampermonkey lub Userscripts.

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

Aby zainstalować ten skrypt, musisz zainstalować rozszerzenie menedżera skryptów użytkownika.

(Mam już menedżera skryptów użytkownika, pozwól mi to zainstalować!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Musisz zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

(Mam już menedżera stylów użytkownika, pozwól mi to zainstalować!)

// ==UserScript==
// @name         table-sorter
// @namespace    https://greasyfork.org/
// @version      0.0.1
// @description  传统表格排序
// @author       HqLin
// @match        https://tfgames.site/*
// @grant        GM.setClipboard
// @grant        GM.addStyle
// @license      AGPLv3
// ==/UserScript==
'use strict';

(function () {
  document.querySelectorAll('table').forEach((tbl) => {
    const thead = tbl.querySelector('thead')
    const theads = tbl.querySelectorAll('thead th')
    const trows = tbl.querySelectorAll('tbody tr')
    if (trows.length === 0 || theads.length === 0 || !thead) return

    theads.forEach((head, idx) => head.dataset.index = idx)

    let sortingIdx = 0, reverse = false
    thead.addEventListener('click', (ev) => {
      const header = ev.target.closest('th')
      reverse = header.dataset.index === sortingIdx ? !reverse : false
      sortingIdx = header.dataset.index
      const tbody = tbl.querySelector('tbody')
      tbody.innerHTML = ''
      const trowsSorted = Array.from(trows).sort((rowA, rowB) => {
        const cellA = rowA.querySelectorAll('td')[sortingIdx]?.textContent
        const cellB = rowB.querySelectorAll('td')[sortingIdx]?.textContent
        const delta = cellA - cellB
        return (reverse ? -1 : 1) * (Number.isNaN(delta) ? cellA.localeCompare(cellB) : delta)
      })

      trowsSorted.forEach(row => tbody.appendChild(row))
    })
  })
})()