GeoGuessr Mode and Leaderboard Sync

Leaderboard is synced with the selected game mode.

目前為 2024-12-10 提交的版本,檢視 最新版本

// ==UserScript==
// @name         GeoGuessr Mode and Leaderboard Sync
// @namespace    http://tampermonkey.net/
// @version      5.4
// @description  Leaderboard is synced with the selected game mode.
// @author       Rotski
// @license      MIT
// @match        https://www.geoguessr.com/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    let lastURL = window.location.href;
    let observer;

    function simulateClickOnSwitch(labelText) {
        const targetSwitch = [...document.querySelectorAll('.switch_label__KrnMF')].find(label => label.textContent.trim() === labelText);
        if (targetSwitch) {
            targetSwitch.click();
            console.log(`Leaderboard synced to: ${labelText}`);
        } else {
            console.warn(`Leaderboard switch for "${labelText}" not found.`);
        }
    }

    function checkAndSyncLeaderboard() {
        const activeModeButton = document.querySelector('.play-setting-button_root__AfG8z.play-setting-button_selected__A0_ik label');
        if (activeModeButton) {
            const activeModeText = activeModeButton.textContent.trim();
            simulateClickOnSwitch(activeModeText);
            console.log(`Leaderboard updated to: ${activeModeText}`);
        } else {
            console.error('Active mode button not found.');
        }
    }

    function setupObserver() {
        observer = new MutationObserver(mutations => {
            mutations.forEach(mutation => {
                if (mutation.addedNodes.length || mutation.attributeName) {
                    checkAndSyncLeaderboard();
                }
            });
        });

        observer.observe(document.body, {
            childList: true,
            subtree: true,
            attributes: true,
            attributeFilter: ['class']
        });
    }

    function monitorUrlChanges() {
        setInterval(() => {
            const currentURL = window.location.href;
            if (currentURL !== lastURL) {
                console.log('URL changed, checking relevance...');
                if (currentURL.includes('/maps/')) {
                    if (!observer) {
                        setupObserver();
                    }
                    checkAndSyncLeaderboard();
                } else if (observer) {
                    console.log('Navigating away from maps, disconnecting observer...');
                    observer.disconnect();
                    observer = null; // Ensure the observer is cleared
                }
                lastURL = currentURL;
            }
        }, 1000); // Check every second
    }

    document.addEventListener('click', function(event) {
        if (event.target.closest('.play-setting-button_root__AfG8z.play-setting-button_selected__A0_ik')) {
            console.log('Mode button clicked, updating leaderboard...');
            setTimeout(checkAndSyncLeaderboard, 100); // Delay to allow any page scripts to process the change
        }
    });

    window.addEventListener('load', () => {
        setTimeout(() => {
            console.log('Page loaded. Initializing leaderboard sync...');
            setupObserver();
            checkAndSyncLeaderboard(); // Perform an initial check in case the observer setup misses the initial state
            monitorUrlChanges(); // Start monitoring URL changes
        }, 200);
    });
})();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址