Undo a craft with Ctrl + Z and redo with Ctrl + Y
当前为
// ==UserScript==
// @name IC Instance Restore on Ctrl + Z
// @namespace http://tampermonkey.net/
// @version 1.2
// @license MIT
// @description Undo a craft with Ctrl + Z and redo with Ctrl + Y
// @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';
window.controzdata = { // Plaural variables are for the ones used to craft
deletedInstances: [],
deletedInstanceId: 0,
deletedInstanceIds: [],
deletedInstance: null
};
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 = // Wait for Nuxt
async function (instanceA, instanceB) {
const response = await ogGCR.apply(this, arguments);
window.controzdata.deletedInstances = [{ ...instanceA }, { ...instanceB }]; // Simple patch of getCraftResponse to backup instances
window.controzdata.deletedInstanceId = this.instanceId += 2; // id is saved and not index because index can change if other instances disappear
return response;
};
} else {
setTimeout(waitForNuxt, 100);
}
}
function restoreInstances() {
if (window.controzdata.deletedInstances.length > 0) {
window.controzdata.deletedInstances.forEach(instance => { // Displace instance because it will use the built in duplicateInsance which displaces it in the other direction
instance.left -= 10;
instance.top += 10;
});
const [instanceA, instanceB] = window.controzdata.deletedInstances;
// I was too stupid to replicate duplicateInstance() so...
window.$nuxt.$root.$children[1].$children[0].$children[0].duplicateInstance(instanceA);
const instancesAfterDuplication1 = window.$nuxt.$root.$children[1].$children[0].$children[0].instances;
const instanceAId = instancesAfterDuplication1[instancesAfterDuplication1.length - 1].id;
window.$nuxt.$root.$children[1].$children[0].$children[0].duplicateInstance(instanceB);
const instancesAfterDuplication2 = window.$nuxt.$root.$children[1].$children[0].$children[0].instances;
const instanceBId = instancesAfterDuplication2[instancesAfterDuplication2.length - 1].id;
window.controzdata.deletedInstanceIds = [instanceAId, instanceBId]; // Update the ids for unrestoring/removing
window.controzdata.deletedInstances = [];
const instances = window.$nuxt.$root.$children[1].$children[0].$children[0].instances;
const indexToRemove = instances.findIndex(instance => instance.id === window.controzdata.deletedInstanceId); // The id is not the same as the index so this is mandnetory.
if (indexToRemove !== -1) {
window.controzdata.deletedInstance = { ...instances[indexToRemove] };
instances.splice(indexToRemove, 1);
}
}
}
function unrestoreInstances() { // Mostly the same as restoreInstances but just reverses it
if (window.controzdata.deletedInstanceIds.length > 0) {
const lastRestoredData = window.controzdata.deletedInstanceIds;
window.controzdata.deletedInstance.left -= 10;
window.controzdata.deletedInstance.top += 10;
const [instanceAId, instanceBId] = lastRestoredData;
const instances = window.$nuxt.$root.$children[1].$children[0].$children[0].instances;
const instanceAIndex = instances.findIndex(instance => instance.id === instanceAId);
const instanceBIndex = instances.findIndex(instance => instance.id === instanceBId);
window.controzdata.deletedInstances = [instances[instanceAIndex], instances[instanceBIndex]]; // Backup again if you redecide and pres Ctrl+z again
if (instanceAIndex !== -1) { // Redelete the instances
instances.splice(instanceAIndex, 1);
}
if (instanceBIndex !== -1) {
instances.splice(instanceBIndex - 1, 1);
}
if (window.controzdata.deletedInstance) {
window.$nuxt.$root.$children[1].$children[0].$children[0].duplicateInstance(window.controzdata.deletedInstance);
window.controzdata.deletedInstance = null;
window.controzdata.deletedInstanceId = instances[instances.length - 1].id; // same here
}
}
}
document.addEventListener("keydown", function(event) {
if (event.ctrlKey && event.key === "z") {
restoreInstances();
}
if (event.ctrlKey && event.key === "y") {
unrestoreInstances();
}
});
waitForNuxt();
})();