IC Recipe Discoveries

Adds indication if you were the first to get a recipe in infinite craft

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

// ==UserScript==
// @name         IC Recipe Discoveries
// @namespace    http://tampermonkey.net/
// @version      1.5
// @license MIT
// @description  Adds indication if you were the first to get a recipe in infinite craft
// @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';

    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 () {
                const firstRecipe = await testRecipe(arguments[0].text, arguments[1].text);
                var thething = await ogGCR.apply(this, arguments);
                var recheck = false;
                if (thething.result == 'Nothing'){
                    recheck = await testRecipe(arguments[0].text, arguments[1].text); // Recheck for 'Nothing' edge case
                }
                thething.recipeNew =
                    firstRecipe && !recheck; // true if it didn't exist before crafting, and is either not 'Nothing' or if it created the recipe to exist.
                thething.acctuallyNothing = !(recheck === false);
                return Promise.resolve(thething);
            };
        } else {
            setTimeout(waitForNuxt, 100);
        }
    }

    // Start checking for $nuxt initialization
    waitForNuxt();

    async function testRecipe(first, second) { // Tests if recipe exists
        try {
            [first, second] = [first, second].map((s) =>
                                                  s.toLowerCase().replace(/(^|\s)\S/g, (match) => match.toUpperCase()) // Make it Neal Case
                                                 );
            const response = await fetch(
                `https://neal.fun/api/infinite-craft/check?first=${first}&second=${second}&result=Nothing`
            );
            if (response.status === 500) return true; // Response code 500 means the recipe doesn't exist
            return false;
        } catch {
            return undefined;
        }
    }
})();