AWBW Join Game (Lobby) shows player's Official Score

Fetch and display official scores for users from their profile pages next to their name on Join Game page.

目前为 2024-01-30 提交的版本。查看 最新版本

// ==UserScript==
// @name         AWBW Join Game (Lobby) shows player's Official Score
// @namespace    https://awbw.amarriner.com/
// @version      0.2
// @description  Fetch and display official scores for users from their profile pages next to their name on Join Game page.
// @author       Hollen9
// @match        https://awbw.amarriner.com/gameswait.php
// @grant        GM.xmlHttpRequest
// @grant        GM_setValue
// @grant        GM_getValue
// @license MIT
// ==/UserScript==

(function() {
    'use strict';

    var fetchInterval = 600000; // 10 minutes in milliseconds
    var delayBetweenRequests = 1000; // 1 second in milliseconds

    function fetchUserScore(username) {
        var userData = GM_getValue(username);
        var currentTime = Date.now();

        if (userData && currentTime - userData.lastFetch < fetchInterval) {
            updateLinkDisplay(username, userData.score);
            scheduleNextUser(false); // No delay if data is recent
            return;
        }

        GM.xmlHttpRequest({
            method: "GET",
            url: 'https://awbw.amarriner.com/profile.php?username=' + username,
            onload: function(response) {
                var parser = new DOMParser();
                var doc = parser.parseFromString(response.responseText, "text/html");
                var scoreElement = doc.querySelector('#profile_page > table:nth-child(1) > tbody > tr > td:nth-child(1) > table > tbody > tr:nth-child(2) > td > table > tbody > tr:nth-child(5) > td:nth-child(2)');
                var score = scoreElement ? scoreElement.innerText.trim() : 'N/A';

                GM_setValue(username, { score: score, lastFetch: Date.now() });
                updateLinkDisplay(username, score);
                scheduleNextUser(true); // Delay after a new fetch
            }
        });
    }

    function updateLinkDisplay(username, score) {
        var linksToUpdate = document.querySelectorAll('.norm[href*="profile.php?username=' + username + '"]');
        linksToUpdate.forEach(function(link) {
            link.innerText = username + ' (' + score + ')';
            link.dataset.score = score;
        });
    }

    function scheduleNextUser(shouldDelay) {
        if (userQueue.length > 0) {
            var delay = shouldDelay ? delayBetweenRequests : 0;
            setTimeout(function() {
                var nextUsername = userQueue.shift();
                fetchUserScore(nextUsername);
            }, delay);
        }
    }

    function extractUsernameFromLink(link) {
        var match = link.match(/username=(.*)$/);
        return match ? decodeURIComponent(match[1]) : null;
    }

    // Initialize the queue
    var userQueue = Array.from(document.querySelectorAll('.norm[href*="profile.php?username="]'))
        .map(link => extractUsernameFromLink(link.href))
        .filter(username => username != null);

    // Start processing the queue
    scheduleNextUser(false);
})();