MAX OWNED LAND

Keeps track of "max owned land"

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

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

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

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

你需要先安裝一款使用者腳本管理器擴展,比如 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();

})();