Minecraft Helper

为 Minecraft 玩家定制的实用脚本

Version vom 19.05.2023. Aktuellste Version

Du musst eine Erweiterung wie Tampermonkey, Greasemonkey oder Violentmonkey installieren, um dieses Skript zu installieren.

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

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

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

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

Sie müssten eine Skript Manager Erweiterung installieren damit sie dieses Skript installieren können

(Ich habe schon ein Skript Manager, Lass mich es installieren!)

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.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name         Minecraft Helper
// @namespace    http://tampermonkey.net/
// @version      0.4.0
// @description  为 Minecraft 玩家定制的实用脚本
// @author       PRO
// @license      gpl-3.0
// @match        https://www.minecraft.net/*
// @match        https://www.curseforge.com/*
// @match        https://modrinth.com/*
// @icon         https://www.minecraft.net/etc.clientlibs/minecraft/clientlibs/main/resources/img/minecraft-creeper-face.jpg
// @grant        none
// ==/UserScript==

(function () {
    'use strict';
    let name = 'Minecraft Helper';
    let toast = (s, error=false) => {
        if (error) {
            console.error(`[${name}] ${s}`);
        } else {
            console.log(`[${name}] ${s}`);
        }
    };
    if (window.invoke_toastify) {
        window.invoke_toastify();
        toast = (s, error = false) => {
            window.toast(`[${name}] ${s}`, error);
        };
    }
    let config = {
        "general": {
            "timeout": 500              // 部分功能的延迟
        },
        "minecraft": {
            "auto-stay": true           // 自动点击“留在 Minecraft.net”
        },
        "curseforge": {
            "auto-mod": true,           // 首页自动转到 MC 模组界面
            "highlight-files": true,    // 突出显示 "Files" 项目
            "highlight-border": "rgb(241, 100, 54) 0.2em solid",
            "default-download": true,   // 默认直接下载
            "shortcut": true            // 快捷键
        },
        "modrinth": {
            "auto-mod": true,           // 首页自动转到 MC 模组界面
            "shortcut": true            // 快捷键: <F>iles, <-, ->
        }
    };
    function try_click(selector) {
        let ele = document.querySelector(selector);
        if (ele) ele.click();
    }
    function setup_shortcuts(selectors) {
        document.addEventListener("keydown", (e) => {
            if (document.activeElement.nodeName != "INPUT") {
                switch (e.key) {
                    case "ArrowLeft":
                        try_click(selectors[0]);
                        break;
                    case "ArrowRight":
                        try_click(selectors[1]);
                        break;
                    case "f":
                        try_click(selectors[2]);
                        break;
                    case "s":
                        if (selectors[3].length) {
                            try_click(selectors[3]);
                            window.setTimeout(() => {
                                let search = document.querySelector(selectors[4]);
                                if (search) search.focus();
                            }, config.general.timeout);
                        } else {
                            let search = document.querySelector(selectors[4]);
                            if (search) search.focus();
                        }
                        e.preventDefault();
                        break;
                    default:
                        break;
                }
            } else if (document.activeElement.value == "") {
                switch (e.key) {
                    case "Escape":
                        document.activeElement.blur();
                        break;
                    case "ArrowLeft":
                        try_click(selectors[0]);
                        break;
                    case "ArrowRight":
                        try_click(selectors[1]);
                        break;
                    default:
                        break;
                }
            }
        })
        toast("⚙️ Shortcuts installed!");
    }
    switch (window.location.host) {
        case 'www.minecraft.net': {
            if (config.minecraft["auto-stay"]) {
                try_click("#popup-btn");
                toast("✋ Auto stayed!");
            }
            break;
        }
        case 'www.curseforge.com': {
            if (config.curseforge["auto-mod"] && window.location.pathname == '/') {
                toast("🛣️ Navigating to mc mods...");
                window.location.pathname = "/minecraft/mc-mods";
            }
            if (config.curseforge["highlight-files"] && window.location.pathname != "/") {
                let tabs = document.getElementsByClassName("tabs");
                if (tabs.length) {
                    tabs = tabs[0];
                    for (let tab of tabs.children) {
                        if (tab.textContent == "Files") {
                            tab.style.border = config.curseforge["highlight-border"];
                            break;
                        }
                    }
                }
            }
            if (config.curseforge["default-download"]) {
                function cf_replace(menu) {
                    menu.children[0].remove();
                    let down = menu.querySelector("#contextMenu").children[1];
                    let new_btn = document.createElement("button");
                    new_btn.innerHTML = '<svg><use href="/images/sprite.svg#icon-download"></use></svg><span>Download</span>';
                    new_btn.setAttribute("class", "btn-cta");
                    new_btn.onclick = (e) => { window.location.href = down.children[0].href; };
                    menu.insertBefore(new_btn, menu.children[0]);
                }
                let cfg = { attributes: false, childList: true, subtree: true };
                let callback = (mutations, observer) => {
                    for (let mutation of mutations) {
                        if (mutation.addedNodes.length) {
                            let menus = mutation.addedNodes[0].querySelectorAll("#menuButton");
                            if (menus.length) {
                                for (let menu of menus) {
                                    cf_replace(menu);
                                }
                            }
                        }
                    }
                };
                let content;
                if (window.location.pathname == "/minecraft/search") {
                    content = document.querySelector(".search-page");
                } else if (window.location.pathname.startsWith("/minecraft/mc-mods/")) {
                    content = document.getElementsByClassName("tab-content")[0];
                } else {
                    break;
                }
                let observer = new MutationObserver(callback);
                observer.observe(content, cfg);
                let menus = document.querySelectorAll("#menuButton");
                if (menus.length) {
                    let cnt = 0;
                    for (let menu of menus) {
                        cnt++;
                        cf_replace(menu);
                    }
                    toast(`📥 Successfully replaced ${cnt} "Install" button(s).`);
                }
            }
            if (config.curseforge.shortcut) {
                setup_shortcuts([".btn-prev", ".btn-next", ".tabs > li > a[href$='/files']", "", "input.search-input-field"]);
            }
            break;
        }
        case "modrinth.com": {
            if (window.location.pathname == "/" && config.modrinth["auto-mod"]) {
                toast("🛣️ Navigating to mod search page...");
                try_click(".button-group > a");
            }
            if (config.modrinth.shortcut) {
                setup_shortcuts([".left-arrow", ".right-arrow", ".goto-link", ".navigation > a.nav-link", "#search"]);
            }
            break;
        }
    }
})();