IC Instance Restore on Ctrl + Z

Restores crafted instances when Ctrl + Z is pressed

当前为 2024-12-22 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         IC Instance Restore on Ctrl + Z
// @namespace    http://tampermonkey.net/
// @version      1.0
// @license      MIT
// @description  Restores crafted instances when Ctrl + Z is pressed
// @icon         https://i.imgur.com/WlkWOkU.png
// @author       @activetutorial on discord
// @match        https://neal.fun/infinite-craft/
// @run-at       document-end
// @grant        none
// ==/UserScript==

(function () {
    'use strict';

    let craftedInstances = [];
    let globalInstanceId = 0;

    function waitForNuxt() {
        if (window.$nuxt && window.$nuxt.$root && window.$nuxt.$root.$children[1] && window.$nuxt.$root.$children[1].$children[0]) {
            const ogGCR = window.$nuxt.$root.$children[1].$children[0].$children[0].getCraftResponse;
            window.$nuxt.$root.$children[1].$children[0].$children[0].getCraftResponse =
                async function (instanceA, instanceB) {
                    const response = await ogGCR.apply(this, arguments);

                    craftedInstances = [{ ...instanceA }, { ...instanceB }];
                    
                    craftedInstances.forEach(instance => {
                        instance.left -= 10;
                        instance.top += 10;
                    });

                    globalInstanceId = this.instanceId += 2;

                    return response;
                };
        } else {
            setTimeout(waitForNuxt, 100);
        }
    }

    function restoreInstances() {
        if (craftedInstances.length > 0) {
            const [instanceA, instanceB] = craftedInstances;

            window.$nuxt.$root.$children[1].$children[0].$children[0].duplicateInstance(instanceA);
            window.$nuxt.$root.$children[1].$children[0].$children[0].duplicateInstance(instanceB);

            craftedInstances = [];

            const instances = window.$nuxt.$root.$children[1].$children[0].$children[0].instances;
            const indexToRemove = instances.findIndex(instance => instance.id === globalInstanceId);
            
            if (indexToRemove !== -1) {
                instances.splice(indexToRemove, 1);
            }
        }
    }

    document.addEventListener("keydown", function(event) {
        if (event.ctrlKey && event.key === "z") {
            restoreInstances();
        }
    });

    waitForNuxt();
})();