NitroType Race Difference Calculator

Shows how many races you're ahead or behind compared to other racers

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला 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         NitroType Race Difference Calculator
// @namespace    http://tampermonkey.net/
// @version      1.5
// @description  Shows how many races you're ahead or behind compared to other racers
// @author       Malakai
// @license       MIt
// @match        https://www.nitrotype.com/racer/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    function getMyRacesPlayed() {
        try {
            const persistData = localStorage.getItem('persist:nt');
            if (!persistData) {
                return null;
            }

            const parsedData = JSON.parse(persistData);

            if (parsedData.user) {
                const userData = JSON.parse(parsedData.user);
                if (userData.racesPlayed !== undefined) {
                    return userData.racesPlayed;
                }
            }

            return null;

        } catch (error) {
            return null;
        }
    }

    function getTheirRacesPlayed() {
        try {
            if (window.NTGLOBALS && window.NTGLOBALS.RACER_INFO) {
                return window.NTGLOBALS.RACER_INFO.racesPlayed;
            }
            return null;
        } catch (error) {
            return null;
        }
    }

    function displayRaceDifference(myRaces, theirRaces) {
        if (myRaces === null || theirRaces === null) {
            return;
        }

        const difference = myRaces - theirRaces;

        if (difference === 0) return;

        let text = '';
        let color = '';

        if (difference > 0) {
            text = `+${difference.toLocaleString()}`;
            color = '#59FFA0'; // Green for positive
        } else {
            text = `${difference.toLocaleString()}`;
            color = '#D62F3A'; // Red for negative
        }

        const totalRacesElement = document.querySelector('.profile-totalRaces');
        if (!totalRacesElement) {
            return;
        }

        const diffElement = document.createElement('div');
        diffElement.textContent = text;
        diffElement.style.cssText = `
            font-size: 18px;
            font-weight: bold;
            color: ${color};
            margin-top: 5px;
            line-height: 1;
            text-align: center;
            font-family: 'Arial', sans-serif;
        `;


        const totalRacesSpan = totalRacesElement.querySelector('.tss.plxxs');
        if (totalRacesSpan) {
            totalRacesElement.insertBefore(diffElement, totalRacesSpan);
        } else {
            totalRacesElement.appendChild(diffElement);
        }
    }

    function init() {
        const myRaces = getMyRacesPlayed();
        const theirRaces = getTheirRacesPlayed();

        if (myRaces !== null && theirRaces !== null) {
            displayRaceDifference(myRaces, theirRaces);
        }
    }

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