Idle Pixel XP Calculator helper

When clicking on the XP under a skill, loads the XP calculator page with the current XP and a target level of +1 of current already filled in

// ==UserScript==
// @name         Idle Pixel XP Calculator helper
// @namespace    com.zlef.ChatGPTMadeThis
// @version      1.0.1
// @description  When clicking on the XP under a skill, loads the XP calculator page with the current XP and a target level of +1 of current already filled in
// @author       ChatGPT (I literally added 1 line of code - Zlef)
// @license      MIT
// @match        *://idle-pixel.com/login/play*
// @match        *://data.idle-pixel.com/xp/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // Function to strip commas and convert to integer
    function parseNumberWithCommas(numString) {
        return parseInt(numString.replace(/,/g, ''), 10);
    }

    // Function to extract XP and Level and redirect
    function handleXPClick(event) {
        event.preventDefault(); // Prevent the default link click action

        // Find the closest parent div
        const parentDiv = event.target.closest('div');

        // Extract XP value from the second child of the link
        const xpElement = parentDiv.querySelector('item-display[data-key*="_xp"]');
        if (!xpElement) {
            console.error('XP element not found');
            return;
        }
        const xpValue = parseNumberWithCommas(xpElement.textContent.trim());

        // Extract current level
        const levelElement = parentDiv.querySelector('span[id^="panel-"][id$="-level"]');
        if (!levelElement) {
            console.error('Level element not found');
            return;
        }
        const currentLevel = parseInt(levelElement.textContent.trim(), 10);

        // Construct the new URL with query parameters
        const newUrl = `https://data.idle-pixel.com/xp/?current-xp=${xpValue}&current-level=${currentLevel}`;

        // Open the new URL in a new tab
        window.open(newUrl, '_blank');
    }

    // Attach click event listener to the specified link
    document.querySelectorAll('a[href="https://data.idle-pixel.com/xp/"]').forEach(link => {
        link.addEventListener('click', handleXPClick);
    });

    // Function to populate the XP calculator form
    function populateXPForm() {
        // Get URL parameters
        const urlParams = new URLSearchParams(window.location.search);
        const currentXP = urlParams.get('current-xp');
        const currentLevel = parseInt(urlParams.get('current-level'), 10);

        if (currentXP && currentLevel) {
            // Populate the form fields
            document.getElementById('current-input').value = currentXP;
            document.getElementById('target-input').value = currentLevel + 1;
        }
        recompute();
    }

    // Function to append new HTML elements to the input section
    function appendNewSubSections() {
        const inputSection = document.querySelector('.input-section');

        const stardustPerXpDiv = document.createElement('div');
        stardustPerXpDiv.className = 'input-sub-section';
        stardustPerXpDiv.innerHTML = `
            <div>SD per xp:</div>
            <input id="sd-per-xp-input" type="number" min="5" max="13" placeholder="For Mining & Crafting">
        `;

        const stardustNeededDiv = document.createElement('div');
        stardustNeededDiv.className = 'input-sub-section';
        stardustNeededDiv.innerHTML = `
            <div>SD needed:</div>
            <input id="sd-needed-input" type="text" placeholder="Stardust needed" readonly="">
        `;

        inputSection.appendChild(stardustPerXpDiv);
        inputSection.appendChild(stardustNeededDiv);
    }

    // Modified recompute function to include stardust calculation
    function recompute() {
        const remainingElement = $("#remaining-input");
        const currentXP = parseInt($("#current-input").val()) || 0;
        const targetLevel = parseInt($("#target-input").val()) || 0;
        const sdPerXpInput = parseInt($("#sd-per-xp-input").val()) || 0;
        const sdNeededElement = $("#sd-needed-input");

        function set(result = "¯\\_(ツ)_/¯") {
            console.log(result);
            remainingElement.val(result);
            return false;
        }

        if (targetLevel < 1 || targetLevel > MAX_LEVEL) {
            return set();
        }
        const targetXP = LEVELS[targetLevel];
        if (currentXP >= targetXP) {
            return set(0);
        }

        const remainingXP = targetXP - currentXP;
        const stardustNeeded = remainingXP * sdPerXpInput;
        sdNeededElement.val(stardustNeeded.toLocaleString());

        return set(remainingXP.toLocaleString());
    }

    // Append new sub-sections on page load
    appendNewSubSections();

    // Check if we are on the XP calculator page
    if (window.location.href.startsWith('https://data.idle-pixel.com/xp/')) {
        populateXPForm();
    }

    // Attach recompute to relevant input changes
    $('#current-input, #target-input, #sd-per-xp-input').on('input', recompute);
})();