Greasy Fork 支持简体中文。

AWBW Join Game (Lobby) Show Players Official Scores

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

目前為 2024-01-31 提交的版本,檢視 最新版本

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

(function() {
    'use strict';

    var fetchInterval = 60 * 60 * 1000; // 60 minutes in milliseconds
    var delayBetweenRequests = 1000; // 1 second in milliseconds

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

        // Update display with existing data if available
        if (userData) {
            updateLinkDisplay(username, userData.score);
        }

        // Fetch new data if none exists or if it's outdated
        if (!userData || currentTime - userData.lastFetch >= fetchInterval) {
            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';
                    var ratingLabelElement = Array.from(doc.querySelectorAll('#profile_page td')).find(td => td.textContent.trim() === "Official Rating:");
                    var scoreElement = ratingLabelElement ? ratingLabelElement.nextElementSibling : null;
                    var score = scoreElement ? scoreElement.innerText.trim() : 'N/A';

                    GM_setValue(username, { score: score, lastFetch: Date.now() });
                    updateLinkDisplay(username, score);
                    scheduleNextUser(true); // Delay only after a new fetch
                }
            });
        } else {
            scheduleNextUser(false); // No delay for local data
        }
    }

    function updateLinkDisplay(username, score) {
        var linksToUpdate = document.querySelectorAll('.norm[href*="profile.php?username=' + username + '"]');
        linksToUpdate.forEach(function(link) {
            // Check if the text is wrapped in <b> or <i>
            var bold = link.querySelector('b');
            var italic = link.querySelector('i');

            if (bold) {
                bold.textContent = username + ' (' + score + ')';
            } else if (italic) {
                italic.textContent = username + ' (' + score + ')';
            } else {
                link.textContent = username + ' (' + score + ')';
            }
        });
    }

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

    function shiftNext() {
        var nextUsername = userQueue.shift();
        fetchUserScore(nextUsername);
    }

    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);

    // First, update all users with local data
    userQueue.forEach(username => {
        var userData = GM_getValue(username);
        if (userData) {
            updateLinkDisplay(username, userData.score);
        }
    });

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