table-sorter

强制排序表格

当前为 2024-10-06 提交的版本,查看 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         table-sorter
// @namespace    https://greasyfork.org/
// @version      0.1.0
// @description  强制排序表格
// @author       HqLin
// @match        https://*/*
// @license      AGPLv3
// ==/UserScript==
'use strict';

(function () {
  document.querySelectorAll('table').forEach((tbl) => {
    const trows = Array.from(tbl.querySelectorAll('tbody tr'))
    const theads = tbl.querySelectorAll('th')
    const thead = theads?.[0]?.parentElement
    const trueHeader = !!tbl.querySelector('thead')

    if (trows.length === 0 || theads.length === 0 || !thead) return
    if (!trueHeader) trows.shift()

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

    let sortingIdx = 0, reverse = false, triggering = false

    document.addEventListener('keydown', ev => triggering = ev.ctrlKey)
    document.addEventListener('keyup', _ => triggering = false)
    thead.addEventListener('click', (ev) => {
      if (!triggering) return
      const header = ev.target.closest('th')
      console.log('Sort by', triggering, sortingIdx, header.textContent)
      reverse = header.dataset.index === sortingIdx ? !reverse : false
      sortingIdx = header.dataset.index
      const tbody = tbl.querySelector('tbody')
      tbody.innerHTML = ''
      // 有的 table 把表头写在 tbody 里面
      if (!trueHeader) tbody.appendChild(thead)
      const trowsSorted = 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))
    })
  })
})()