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

目前为 2024-07-05 提交的版本。查看 最新版本

// ==UserScript==
// @name         XP Calculator helper
// @namespace    com.zlef.ChatGPTMadeThis
// @version      1.0.0
// @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()
    }

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