iTab主页增强快捷键

增强快捷键,提高使用效率

// ==UserScript==
// @name         iTab主页增强快捷键
// @namespace    https://greasyfork.org/zh-CN/users/1267923-samethink
// @version      1.1
// @description  增强快捷键,提高使用效率
// @author       samethink
// @match        https://go.itab.link/
// @icon         https://go.itab.link/favicon.ico
// @grant        GM_registerMenuCommand
// @grant        GM_unregisterMenuCommand
// @grant        GM_getValue
// @grant        GM_setValue
// @license      MIT
// ==/UserScript==

(function () {
    'use strict';

    let appArray = [];
    let groupArray = [];
    let activedGroupIndex = -1;
    let activedGroupIdentifier = null;
    let appTipsExist = false;
    let groupTipsExist = false;

    document.addEventListener("keydown", event => {
        if (event.altKey && !event.ctrlKey) {
            event.preventDefault();
            refreshGroupArray();
            appArray = document.querySelectorAll(".app-icon-item")[activedGroupIndex]?.querySelectorAll(".app-item");

            // [A-Z]
            if (64 < event.keyCode && event.keyCode < 91 && SETTINGS.ALPHA_MODE.enabled) {
                event.shiftKey ? clickAndScrollto(groupArray[event.keyCode - 56]) : appArray[event.keyCode - 56].firstChild.click();
            }
            // [1-9]
            else if (48 < event.keyCode && event.keyCode < 58) {
                event.shiftKey ? clickAndScrollto(groupArray[event.keyCode - 49]) : appArray[event.keyCode - 49].firstChild.click();
            }
            // 0
            else if (event.keyCode === 48) {
                event.shiftKey ? clickAndScrollto(groupArray[groupArray.length - 1]) : appArray[appArray.length - 1].firstChild.click();
            }
            // [-_]
            else if (event.keyCode === 189) {
                event.shiftKey ? undefined : document.querySelector("#menuhomeIcon_3d233b > li:nth-child(4)").click();
            }
            // [=+]
            else if (event.keyCode === 187) {
                event.shiftKey ? document.querySelector("#appGroupAdd").click() : document.querySelector("#menuhome_5dwx > li:nth-child(1)").click();
            }
            // [`~]
            else if (event.keyCode === 192) {
                document.querySelector("#app-main > itab-date").shadowRoot.querySelector("div > div:nth-child(1) > div").click();
            }
            // [,<]
            else if (event.keyCode === 188) {
                rankApp("asc");
            }
            // [.>]
            else if (event.keyCode === 190) {
                rankApp("desc");
            }
            // [/?]
            else if (event.keyCode === 191) {
                document.querySelector("#searchInput").click();
            }

            // 添加应用快捷键提示
            else if (event.key === "Alt" && !event.shiftKey && SETTINGS.SHOW_APP_TIPS.enabled && !appTipsExist) {
                appArray.forEach((item, index) => {
                    let title = item.querySelector(".app-item-title");
                    title.textContent = (index < 9 ? `「${index + 1}」` : index < 35 && SETTINGS.ALPHA_MODE.enabled ? `「${String.fromCharCode(index + 56)}」` : "") + title.textContent;
                });
                appTipsExist = true;
            }
            // 添加分组快捷键提示
            else if ((event.key === "Shift" || event.shiftKey) && SETTINGS.SHOW_GROUP_TIPS.enabled && !groupTipsExist) {
                groupArray.forEach((item, index) => {
                    item.querySelector(".app-group-item-title").textContent += " " + (index < 9 ? (index + 1) : index < 35 && SETTINGS.ALPHA_MODE.enabled ? String.fromCharCode(index + 56) : "");
                });
                groupTipsExist = true;
                removeAppTips();
            }
        }
        else if (!document.querySelector("#app-serach-wrap > div > form").className.includes("active")) {
            // 切换上一分组
            if (event.key === "ArrowUp") {
                refreshGroupArray();
                clickAndScrollto(groupArray[(activedGroupIndex === 0 ? groupArray.length : activedGroupIndex) - 1]);
            }
            // 切换下一分组
            else if (event.key === "ArrowDown") {
                refreshGroupArray();
                clickAndScrollto(groupArray[(activedGroupIndex + 1) % groupArray.length]);
            }
        }
    });

    function refreshGroupArray() {
        groupArray = document.querySelectorAll(".app-sidebar-ul > .app-group-item");
        groupArray.forEach((item, index) => {
            if (item.classList.contains("active")) {
                activedGroupIndex = index;
                activedGroupIdentifier = item.firstChild.id;
                return;
            }
        });
    }

    function clickAndScrollto(element) {
        element.click();
        element.scrollIntoView({ behavior: "smooth", block: "start" });
    }

    // 应用排序
    function rankApp(order) {
        let navConfig = JSON.parse(localStorage.getItem("navConfig"));

        navConfig.forEach((item, index) => {
            if (item.id === activedGroupIdentifier) {
                navConfig[index].children = item.children.sort((a, b) => {
                    if (order === 'asc') {
                        return a.name.localeCompare(b.name);
                    } else if (order === 'desc') {
                        return b.name.localeCompare(a.name);
                    }
                    return 0;
                });
                return;
            }
        });

        localStorage.setItem("navConfig", JSON.stringify(navConfig));
        location.reload();
    }

    // 添加图标时自动跳到自定义选项
    setTimeout(() => {
        document.querySelector("#menuhome_5dwx > li:nth-child(1)").addEventListener("click", () => {
            setTimeout(() => document.querySelector("#app-add-icon > ul > li:nth-child(4)").click(), 100);
            setTimeout(() => document.querySelector(".app-icon-wrap .el-input__wrapper").click(), 200);
        });
    }, 1000);

    document.addEventListener("keyup", event => {
        if (event.key === "Alt") {
            removeAppTips();
            removeGroupTips();
        }
        else if (event.key === "Shift") {
            removeGroupTips();
        }
    });
    window.onblur = removeAppTips;

    function removeAppTips() {
        if (appTipsExist) {
            for (let i = 0; i < appArray.length && (i < 9 || SETTINGS.ALPHA_MODE.enabled && i < 35); ++i) {
                let title = appArray[i].querySelector(".app-item-title");
                title && (title.textContent = title.textContent.substring(3));
            }
            appTipsExist = false;
        }
    }

    function removeGroupTips() {
        if (groupTipsExist) {
            for (let i = 0; i < groupArray.length && (i < 9 || SETTINGS.ALPHA_MODE.enabled && i < 35); ++i) {
                let title = groupArray[i].querySelector(".app-group-item-title");
                title.textContent = title.textContent.slice(0, -2);
            }
            groupTipsExist = false;
        }
    }


    // 设置菜单

    const SETTINGS = {
        SHOW_APP_TIPS: { id: null, name: "应用快捷键提示", enabled: true },
        SHOW_GROUP_TIPS: { id: null, name: "分组快捷键提示", enabled: true },
        ALPHA_MODE: { id: null, name: "使用字母按键", enabled: true },
    };

    function createMenuOptions(init) {
        Object.keys(SETTINGS).forEach(optionKey => {
            const option = SETTINGS[optionKey];
            if (init) {
                let enabled = GM_getValue(optionKey);
                enabled === undefined || (option.enabled = enabled);
            }
            option.id = GM_registerMenuCommand((option.enabled ? "🟢" : "🔴") + option.name, () => {
                const option = SETTINGS[optionKey];
                option.enabled = !option.enabled;
                GM_setValue(optionKey, option.enabled);
                Object.values(SETTINGS).forEach(option => {
                    GM_unregisterMenuCommand(option.id);
                });
                createMenuOptions(false);
            });
        });
    }

    createMenuOptions(true);

})();