MAX OWNED LAND

Keeps track of "max owned land"

За да инсталирате този скрипт, трябва да имате инсталирано разширение като Tampermonkey, Greasemonkey или Violentmonkey.

За да инсталирате този скрипт, трябва да инсталирате разширение, като например Tampermonkey .

За да инсталирате този скрипт, трябва да имате инсталирано разширение като Tampermonkey или Violentmonkey.

За да инсталирате този скрипт, трябва да имате инсталирано разширение като Tampermonkey или Userscripts.

За да инсталирате скрипта, трябва да инсталирате разширение като Tampermonkey.

За да инсталирате този скрипт, трябва да имате инсталиран скриптов мениджър.

(Вече имам скриптов мениджър, искам да го инсталирам!)

За да инсталирате този стил, трябва да инсталирате разширение като Stylus.

За да инсталирате този стил, трябва да инсталирате разширение като Stylus.

За да инсталирате този стил, трябва да инсталирате разширение като Stylus.

За да инсталирате този стил, трябва да имате инсталиран мениджър на потребителски стилове.

За да инсталирате този стил, трябва да имате инсталиран мениджър на потребителски стилове.

За да инсталирате този стил, трябва да имате инсталиран мениджър на потребителски стилове.

(Вече имам инсталиран мениджър на стиловете, искам да го инсталирам!)

// ==UserScript==
// @name         MAX OWNED LAND
// @namespace    http://tampermonkey.net/
// @version      1.6
// @description  Keeps track of "max owned land"
// @author       OpenFront Masters x NewYearNewPhil
// @match        https://openfront.io/*
// @license      MIT
// @grant        none
// @run-at       document-idle
// @dowloadURL   https://greasyfork.org/en/scripts/558114-max-owned-land/code
// ==/UserScript==

(function() {
    'use strict';

    let maxOwned = JSON.parse(localStorage.getItem('maxOwned')) || {};
    let lastUrl = location.href;

    function getGameId(url) {
        const match = url.match(/\/game\/([^?]+)/);
        return match ? match[1] : null;
    }

    function formatPercentage(str) {
        return parseFloat(str.replace('%','').trim());
    }

    function updateMax(name, value) {
        if(!maxOwned[name] || value > maxOwned[name]) {
            maxOwned[name] = value;
            localStorage.setItem('maxOwned', JSON.stringify(maxOwned));
            return true;
        }
        return false;
    }

    function displayMax(node, name, currentValue) {
        let maxValue = maxOwned[name] || currentValue;
        let span = node.querySelector('.max-owned');
        if(!span) {
            span = document.createElement('span');
            span.className = 'max-owned';
            span.style.fontSize = '0.8em';
            span.style.color = '#ccc';
            span.style.marginLeft = '3px';
            node.appendChild(span);
        }
        span.textContent = `(max: ${maxValue}%)`;
    }

    function processRow(cols, nameIndex, ownedIndex) {
        const name = cols[nameIndex].textContent.trim();
        const ownedNode = cols[ownedIndex];
        const currentOwned = formatPercentage(ownedNode.textContent);
        const isNewMax = updateMax(name, currentOwned);
        displayMax(ownedNode, name, currentOwned);
        if(isNewMax) {
            ownedNode.querySelector('.max-owned').style.color = '#0f0';
            setTimeout(() => {
                ownedNode.querySelector('.max-owned').style.color = '#ccc';
            }, 1000);
        }
    }

    function processLeaderboard() {
        document.querySelectorAll('leader-board .contents').forEach(row => {
            const cols = row.children;
            if(cols.length >= 5) processRow(cols, 1, 2);
        });
    }

    function processTeamStats() {
        document.querySelectorAll('team-stats .contents').forEach(row => {
            const cols = row.children;
            if(cols.length >= 4) processRow(cols, 0, 1);
        });
    }

    function resetMaxOwned() {
        console.log('[MAX OWNED] Resetting data for new game');
        maxOwned = {};
        localStorage.removeItem('maxOwned');
        document.querySelectorAll('.max-owned').forEach(span => span.remove());
    }

    function checkGameChange() {
        const currentGameId = getGameId(location.href);
        const storedGameId = localStorage.getItem('gameId');

        if (!currentGameId) return;

        if (!storedGameId) {
            console.log('[MAX OWNED] Setting initial gameId:', currentGameId);
            localStorage.setItem('gameId', currentGameId);
            return;
        }

        if (currentGameId !== storedGameId) {
            resetMaxOwned();
            localStorage.setItem('gameId', currentGameId);
        }
    }

    function processAll() {
        if(location.href !== lastUrl) {
            checkGameChange();
            lastUrl = location.href;
        }

        processLeaderboard();
        processTeamStats();
    }

    let scheduled = false;
    const observer = new MutationObserver(() => {
        if(!scheduled) {
            scheduled = true;
            setTimeout(() => {
                processAll();
                scheduled = false;
            }, 200);
        }
    });

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

    checkGameChange();
    processAll();

})();