GeoGuessr Battle Royale Flag Names

Shows the country's ISO code underneath the flag and the country name on hover in GeoGuessr Battle Royale

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         GeoGuessr Battle Royale Flag Names
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  Shows the country's ISO code underneath the flag and the country name on hover in GeoGuessr Battle Royale
// @author       lacmac
// @match        https://www.geoguessr.com/battle-royale/*
// @grant        none
// @connect      restcountries.eu
// ==/UserScript==

(async function() {
    'use strict';

    const apiURL = "https://restcountries.eu/rest/v2/alpha/";

    // Wait for ui to load
    while (!document.querySelector(".game-state-overview")) {
        await new Promise(r => setTimeout(r, 1000));
    }

    // Call manually once as flags may already be guessed. (Reconnecting, etc)
    showNames();

    // Show country code each time a new flag is guessed
    var observer = new MutationObserver(function(mutations) {
        for (let mutation of mutations) {
            if (mutation.type === "childList" && (mutation.target.className === "wrong-guesses__flags" || mutation.target.className === "game-state-overview")) {
                showNames();
                break;
            }
        }
    });

    // Start the observer on the guessed flags "list" element
    observer.observe(document.getElementsByClassName("game-state-overview")[0], { attributes: false, childList: true, characterData: false, subtree: true });

    function showNames() {
        // Retrieve all guessed flags
        let flags = document.getElementsByClassName("wrong-guesses__flag");
        for (let flag of flags) {
            let flag_div = flag.firstElementChild.firstElementChild.firstElementChild.firstElementChild;
            // Only add the country code once
            if (flag_div.parentNode.children.length === 1) {
                // Retrieve the 'alt' attribute from the flag img element
                let country_code = flag_div.firstElementChild.getAttribute("alt").toUpperCase();
                // Insert the html underneath the image and correct the country code
                flag_div.insertAdjacentHTML("afterend", '<span style="text-align: center;display: block;width: inherit;margin-top: 0.35rem;font-weight: bold;">Temp_Name</span>');
                flag_div.nextElementSibling.textContent = country_code;
                flag.style.marginBottom = "1rem";
                // Set the country name to show on hover
                fetch(apiURL + country_code)
                    .then(res => res.json())
                    .then(country => flag.setAttribute("title", country.name));
                flag.style.zIndex = 1;
            }
        }
    }
})();