PM Username flags

Affiche le drapeau du pays à côté des pseudos

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         PM Username flags
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  Affiche le drapeau du pays à côté des pseudos
// @author       Danatru
// @match        https://platesmania.com/*
// @grant        GM_xmlhttpRequest
// @connect      https://platesmania.com/*
// @license MIT
// ==/UserScript==

(function() {
    'use strict';

    const JSON_URL = "https://gist.githubusercontent.com/danatro/5b0e938537862ff628501b580d36a2db/raw/2e7f9866317a3ba581c5182ad583efe5b217da89/pm.json";
    let countryMap = {};

    function getFlagImg(code) {
        if (!code || code.length !== 2) return "";
        const lower = code.toLowerCase();
        return `https://flagcdn.com/w20/${lower}.png`;
    }

    function addFlags() {
        const players = document.querySelectorAll('a[href^="/user"]');

        players.forEach(player => {
            if (player.dataset.flagAdded) return;

            const pseudo = player.textContent.trim();
            const countryCode = countryMap[pseudo];

            if (countryCode) {
                const img = document.createElement("img");
                img.src = getFlagImg(countryCode);
                img.alt = countryCode;
                img.style.width = "24px";
                img.style.height = "18px";
                img.style.marginLeft = "6px";
                img.style.verticalAlign = "middle";

                player.appendChild(img);
                player.dataset.flagAdded = "true";
            }
        });
    }

    function observeDOM() {
        const observer = new MutationObserver(() => {
            addFlags();
        });

        observer.observe(document.body, {
            childList: true,
            subtree: true
        });
    }

    function loadJSON() {
        GM_xmlhttpRequest({
            method: "GET",
            url: JSON_URL,
            onload: function(response) {
                try {
                    countryMap = JSON.parse(response.responseText);
                    addFlags();
                } catch (e) {
                    console.error("Erreur JSON:", e);
                }
            }
        });
    }

    loadJSON();
    observeDOM();

})();