Geoguessr Random Map Button (Stable)

Adds a visible floating button to get a random Geoguessr map

Dovrai installare un'estensione come Tampermonkey, Greasemonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Userscripts per installare questo script.

Dovrai installare un'estensione come ad esempio Tampermonkey per installare questo script.

Dovrai installare un gestore di script utente per installare questo script.

(Ho già un gestore di script utente, lasciamelo installare!)

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

(Ho già un gestore di stile utente, lasciamelo installare!)

// ==UserScript==
// @name         Geoguessr Random Map Button (Stable)
// @namespace    http://tampermonkey.net/
// @version      1.1
// @description  Adds a visible floating button to get a random Geoguessr map
// @author       you
// @match        https://www.geoguessr.com/*
// @grant        none
// @license MIT
// ==/UserScript==

(function () {
    'use strict';

    function injectButton() {
        // Avoid duplicate buttons
        if (document.getElementById('random-map-button')) return;

        const button = document.createElement('button');
        button.id = 'random-map-button';
        button.textContent = '🎲 Random Map';
        Object.assign(button.style, {
            position: 'fixed',
            top: '20px',
            right: '20px',
            padding: '10px 15px',
            fontSize: '14px',
            fontWeight: 'bold',
            backgroundColor: '#00AA88',
            color: 'white',
            border: 'none',
            borderRadius: '5px',
            cursor: 'pointer',
            zIndex: '9999',
            opacity: '1',
            display: 'block',
            boxShadow: '0 2px 5px rgba(0, 0, 0, 0.3)',
        });

        button.onclick = async () => {
            try {
                const res = await fetch('https://www.geoguessr.com/api/v3/social/maps/browse/random', {
                    credentials: 'include'
                });
                const data = await res.json();
                const mapUrl = 'https://www.geoguessr.com' + data.url;
                window.open(mapUrl, '_blank');
            } catch (err) {
                alert('Error fetching map. Are you logged in?');
                console.error(err);
            }
        };

        document.body.appendChild(button);
    }

    // Retry until body is loaded
    function waitAndInject() {
        if (document.body) {
            injectButton();
        } else {
            setTimeout(waitAndInject, 500); // retry every 0.5s
        }
    }

    waitAndInject();
})();