modd.io servers sync

try fixing servers missing issues

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey, Greasemonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

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

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

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

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्क्रिप्ट व्यवस्थापक एक्स्टेंशन इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्क्रिप्ट व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्टाईल व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

// ==UserScript==
// @name         modd.io servers sync
// @namespace    http://tampermonkey.net/
// @version      1.4
// @description  try fixing servers missing issues
// @author       n0fir3
// @match        *://*.modd.io/play/*
// @grant        unsafeWindow
// @grant        GM_xmlhttpRequest
// @connect      www.modd.io
// @run-at       document-start
// @license      MIT
// ==/UserScript==

(function () {
    'use strict';

    function fetchServers(gameId) {
        GM_xmlhttpRequest({
            method: "GET",
            url: `https://www.modd.io/api/v1/game-server/${gameId}/`,
            onload: function (response) {
                try {
                    const data = JSON.parse(response.responseText);
                    const serverList = data.gameServers || data.servers;

                    if (!serverList || !Array.isArray(serverList)) return;

                    const servers = serverList.filter(s => s.status === 'started' || s.isReachable);
                    const select = document.querySelector('#server-list');

                    if (!select) return;

                    select.innerHTML = '';

                    servers.forEach((server) => {
                        const option = document.createElement('option');
                        option.className = 'game-server';
                        option.value = server.id;
                        option.setAttribute('data-url', `wss://${server.ip}:${server.httpsPort || server.wsPort}`);
                        option.setAttribute('data-server-id', server.id);
                        option.textContent = `${server.name || 'Server'} (${server.playerCount || 0}/${server.maxPlayers || 0})`;
                        select.appendChild(option);
                    });

                    select.dispatchEvent(new Event('change', { bubbles: true }));
                } catch (e) {}
            }
        });
    }

    function init() {
        const runLogic = () => {
            const select = document.querySelector('#server-list');
            const gId = unsafeWindow.gameId;

            if (select && gId) {
                fetchServers(gId);
                return true;
            }
            return false;
        };

        if (runLogic()) return;

        const observer = new MutationObserver(() => {
            if (runLogic()) observer.disconnect();
        });

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

    if (document.readyState === 'loading') {
        document.addEventListener('DOMContentLoaded', init);
    } else {
        init();
    }
})();