Greasy Fork

Greasy Fork is available in English.

MWI Simulator Auto Import

Tools for Milky Way Idle. Automatically imports set group settings from URL parameters.

目前为 2025-05-10 提交的版本。查看 最新版本

// ==UserScript==
// @name         MWI Simulator Auto Import
// @namespace    http://tampermonkey.net/
// @version      0.0.2
// @description  Tools for Milky Way Idle. Automatically imports set group settings from URL parameters.
// @author       AyajiLin
// @match        https://amvoidguy.github.io/MWICombatSimulatorTest/*
// @match        https://shykai.github.io/MWICombatSimulatorTest/dist/*
// @grant        GM_xmlhttpRequest
// @connect      textdb.online
// @license      MIT
// ==/UserScript==

(function () {
    'use strict';

    // Function to create and show a floating message
    function showFloatingMessage(message) {
        const messageDiv = document.createElement('div');
        messageDiv.style.cssText = `
            position: fixed;
            top: 20px;
            left: 50%;
            transform: translateX(-50%);
            background-color: rgba(0, 0, 0, 0.8);
            color: white;
            padding: 10px 20px;
            border-radius: 5px;
            z-index: 9999;
            font-family: Arial, sans-serif;
            font-size: 14px;
        `;
        messageDiv.textContent = message;
        document.body.appendChild(messageDiv);
        return messageDiv;
    }

    // Function to show error message
    function showErrorMessage(messageDiv, message) {
        messageDiv.textContent = message;
        messageDiv.style.backgroundColor = 'rgba(255, 0, 0, 0.8)';
        setTimeout(() => messageDiv.remove(), 3000);
    }

    // Function to show success message
    function showSuccessMessage(messageDiv, message) {
        messageDiv.textContent = message;
        messageDiv.style.backgroundColor = 'rgba(0, 128, 0, 0.8)';
        setTimeout(() => messageDiv.remove(), 3000);
    }

    function clickGetPriceButton() {
        const getPriceButton = document.querySelector(`button#buttonGetPrices`);
        if (getPriceButton) {
            console.log("Click getPriceButton");
            getPriceButton.click();
        }
    }

    // Function that runs when the page is fully loaded
    // This function is used to automatically import the set group combat all
    function onPageLoadForAutoImport() {
        // parse the url for URL parameters
        const urlParams = new URLSearchParams(window.location.search);
        // get url parameter "textdb" if exists
        const textdbID = urlParams.get('textdb');
        // get the import input element
        const importInputElem = document.querySelector(`input#inputSetGroupCombatAll`);
        if (importInputElem == null) {
            return;
        }
        if (textdbID) {
            const messageDiv = showFloatingMessage('Loading settings from textdb...');
            // Fetch text from textdb.online using the token
            GM_xmlhttpRequest({
                method: 'GET',
                url: `https://textdb.online/${textdbID}`,
                onload: function(response) {
                    if (response.status === 200) {
                        if (response.responseText) {
                            importInputElem.value = response.responseText;
                            document.querySelector(`button#buttonImportSet`).click();
                            showSuccessMessage(messageDiv, 'Settings loaded successfully!');
                            clickGetPriceButton();
                        } else {
                            showErrorMessage(messageDiv, 'No settings found!');
                        }
                    } else {
                        showErrorMessage(messageDiv, 'Error loading settings!');
                    }
                },
                onerror: function(error) {
                    console.error('Error fetching from textdb.online:', error);
                    showErrorMessage(messageDiv, 'Error loading settings!');
                }
            });
        }
    }

    // Add event listener for page load
    window.addEventListener('load', onPageLoadForAutoImport);
})();