IC Instance Restore on Ctrl + Z

Restores crafted instances when Ctrl + Z is pressed

Stan na 22-12-2024. Zobacz najnowsza wersja.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Greasemonkey lub Violentmonkey.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana będzie instalacja rozszerzenia Tampermonkey lub Userscripts.

You will need to install an extension such as Tampermonkey to install this script.

Aby zainstalować ten skrypt, musisz zainstalować rozszerzenie menedżera skryptów użytkownika.

(Mam już menedżera skryptów użytkownika, pozwól mi to zainstalować!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Musisz zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

(Mam już menedżera stylów użytkownika, pozwól mi to zainstalować!)

// ==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();
})();