Sort THSC Online Pages by Year

Sort pages on thsconline.github.io by year (newest to oldest) and display as a numbered list

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Sort THSC Online Pages by Year
// @namespace    http://tampermonkey.net/
// @version      1.2
// @description  Sort pages on thsconline.github.io by year (newest to oldest) and display as a numbered list
// @author       stuffed
// @match        *://thsconline.github.io/*
// @grant        none
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    function extractYear(text) {
        const match = text.match(/\d{4}/);
        return match ? parseInt(match[0], 10) : 0;
    }

    function sortLinks() {
        let container = document.querySelector('.listing'); // Updated selector
        if (!container) return;

        let links = Array.from(container.querySelectorAll('a')) // Get all links
            .filter(link => !/upload files here/i.test(link.textContent)); // Remove 'upload files here'

        links.sort((a, b) => extractYear(b.textContent) - extractYear(a.textContent));

        // Clear container before reappending sorted elements
        container.innerHTML = '';

        // Create ordered list
        let ol = document.createElement('ol');
        ol.style.paddingLeft = '20px'; // Ensure proper indentation

        // Append sorted links as list items
        links.forEach(link => {
            let li = document.createElement('li');
            li.style.marginBottom = '0.5em'; // Adds half a line space between items
            li.appendChild(link);
            ol.appendChild(li);
        });

        container.appendChild(ol);
    }

    sortLinks();
})();