Created
June 25, 2022 15:56
-
-
Save Ascor8522/335ef058970cbf3ba83863c0f866ecc9 to your computer and use it in GitHub Desktop.
Enable sorting proxies by column in table
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Proxy sorter | |
// @version 0.1 | |
// @description Enable sorting proxies by column in table | |
// @author Ascor8522 | |
// @match https://spys.one/* | |
// @grant none | |
// ==/UserScript== | |
(function() { | |
"use strict"; | |
const tableRow = document.querySelector("tr.spy1x"); | |
if (!tableRow) return; | |
const tableBody = tableRow.parentElement; | |
Array.from(tableRow.children).forEach((tableColumn, index) => { | |
tableColumn.style.cursor = "pointer"; | |
tableColumn.addEventListener("click", columnClicked.bind(tableColumn, index)); | |
}); | |
/** | |
* Sorts the table by the given column | |
* @param index The index of the column to sort the table by | |
*/ | |
function columnClicked(index) { | |
console.log(`Sorting on column ${index} (${this.innerText})`); | |
const asc = this.classList.toggle("asc") ? 1 : -1; // Toggle asc/desc class | |
Array | |
.from(tableBody.children) | |
.slice(2, -1) // Remove everything except the filters (0), header (1) and the legend (n-1) | |
.sort((row1, row2) => { | |
const cell1 = row1.children[index]; | |
const cell2 = row2.children[index]; | |
let sort; | |
switch (index) { | |
case 6: { // Speed | |
const speed1 = Number(cell1.firstElementChild.width); | |
const speed2 = Number(cell2.firstElementChild.width); | |
return asc * (speed1 - speed2); | |
} | |
case 8: { // Check date | |
const date1 = new Date(cell1.innerText.split(" ").slice(0, -1).join(" ")); | |
const date2 = new Date(cell2.innerText.split(" ").slice(0, -1).join(" ")); | |
return asc * (date1.getTime() - date2.getTime()); | |
} | |
default: { | |
return asc * cell1.innerText.localeCompare(cell2.innerText, "en", { sensitivity: "case", numeric: true }); | |
} | |
} | |
}) | |
.map(row => (void tableBody.removeChild(row)) || row) | |
.map(row => (void tableBody.insertBefore(row, tableBody.lastElementChild))); | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment