Greasy Fork 支持简体中文。

Torn Custom Race Organizer

Move all 1-lap races with 1/2 drivers to the top of the list and highlight them

// ==UserScript==
// @name         Torn Custom Race Organizer
// @namespace    https://greasyfork.org
// @license      MIT
// @version      2.3
// @description  Move all 1-lap races with 1/2 drivers to the top of the list and highlight them
// @author       yoyoYossarian and chatGPT
// @match        https://www.torn.com/loader.php?sid=racing*
// @grant        none
// ==/UserScript==

(function () {
    'use strict';

    console.log("Torn Custom Race Organizer script is running.");

    // Function to rearrange races
    function rearrangeRaces() {
        const eventList = document.querySelector('ul.events-list');
        if (!eventList) {
            console.log('No event list found.');
            return;
        }

        const races = Array.from(eventList.children); // All race list items
        const priorityRaces = []; // For 1-lap races with 1/2 drivers

        races.forEach((race) => {
            const trackElement = race.querySelector('li.track');
            const lapsElement = trackElement?.querySelector('span.laps');
            const driversElement = race.querySelector('li.drivers');

            if (trackElement && lapsElement && driversElement) {
                const laps = lapsElement.textContent.trim();
                const driverCountText = driversElement.textContent.replace(/\s+/g, ' ').trim();

                // Extract current and max drivers safely
                const match = driverCountText.match(/(\d+)\s*\/\s*(\d+)/);
                const currentDrivers = match ? parseInt(match[1], 10) : null;
                const maxDrivers = match ? parseInt(match[2], 10) : null;

                console.log(`Laps: ${laps}, Current Drivers: ${currentDrivers}, Max Drivers: ${maxDrivers}`);

                if (laps === '(1 lap)' && currentDrivers === 1 && maxDrivers === 2) {
                    race.style.backgroundColor = 'rgba(152, 251, 152, 0.5)'; // Highlight joinable 1-lap races with 1/2 drivers
                    priorityRaces.push(race);
                }
            }
        });

        // Clear the event list and reinsert in the desired order
        priorityRaces.forEach((race) => eventList.prepend(race)); // Move priority races to the top
    }

    // Wait for the element and initialize
    function waitForElement(selector, callback) {
        const checkInterval = setInterval(() => {
            const element = document.querySelector(selector);
            if (element) {
                clearInterval(checkInterval);
                callback(element);
            }
        }, 100);
    }

    waitForElement('ul.events-list', () => {
        console.log('Event list found!');
        rearrangeRaces();
    });
})();