Pixel sites real online

makes online of pixel sites real

Fra 06.05.2025. Se den seneste versjonen.

// ==UserScript==
// @name         Pixel sites real online
// @namespace    https://pixuniverse.fun/
// @version      1.4
// @license      MIT
// @description  makes online of pixel sites real
// @author       small bee
// @match        *://*.fun/*
// @grant        none
// @run-at       document-end
// ==/UserScript==

(function() {
    'use strict';

    let lastSum = 0;
    let textNode = null;

    async function updateOnlineCount() {
        const res = await fetch('/api/shards', { credentials: 'same-origin' });
        if (!res.ok) return;
        const data = await res.json();
        let sum = 0;
        data.forEach(item => {
            if (Array.isArray(item) && item[1]) {
                Object.entries(item[1]).forEach(([k, v]) => {
                    if (k !== 'total' && typeof v === 'number') sum += v;
                });
            }
        });

        const span = document.querySelector('.onlinebox span[title="Total Online Users"]');
        if (!span) return;

        if (!textNode) {
            textNode = Array.from(span.childNodes).find(n => n.nodeType === Node.TEXT_NODE);
            if (!textNode) {
                textNode = document.createTextNode('');
                span.insertBefore(textNode, span.firstChild);
            }
        }

        lastSum = sum;
        textNode.textContent = lastSum;
    }

    function observeExternalChanges() {
        const span = document.querySelector('.onlinebox span[title="Total Online Users"]');
        if (!span) return;

        const observer = new MutationObserver(mutations => {
            mutations.forEach(m => {
                // if the text node was changed externally, revert it
                if (textNode && textNode.textContent !== String(lastSum)) {
                    textNode.textContent = lastSum;
                }
            });
        });

        observer.observe(span, {
            characterData: true,
            childList: true,
            subtree: true
        });
    }

    updateOnlineCount().then(observeExternalChanges);
    setInterval(updateOnlineCount, 5000);
})();