Key-Based Config

A script for interfacing with my Key-Based Config UI.

目前為 2021-08-01 提交的版本,檢視 最新版本

此腳本不應該直接安裝,它是一個供其他腳本使用的函式庫。欲使用本函式庫,請在腳本 metadata 寫上: // @require https://update.cn-greasyfork.org/scripts/419978/956314/Key-Based%20Config.js

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name        Key-Based Config
// @author      Callum Latham <[email protected]> (https://github.com/ctl2/key-based-config)
// @exclude     *
// @description A script for interfacing with my Key-Based Config UI.
// ==/UserScript==

let iframeExists = false;

function kbcConfigure(storageKey, title, metaTree) {
    return new Promise((resolve, reject) => {
        if (iframeExists) {
            reject(new Error("A key-based-config iFrame already exists."));
        } else if (typeof GM.getValue !== "function" || typeof GM.setValue !== "function") {
            reject(new Error("The key-based config script requires GM.getValue and GM.setValue permissions."));
        } else {
            iframeExists = true;
            const kbcSrc = "https://callumlatham.com/key-based-config/";

            // Make iFrame
            let iframe = document.createElement("iframe");
            iframe.src = kbcSrc;
            iframe.style.position = "fixed";
            iframe.style.height = "100vh";
            iframe.style.width = "100vw";

            // Listen for iFrame communication
            window.addEventListener("message", async (message) => {
                switch (message.data.event) {
                    case "open":
                        // Pass initilisation data
                        const valueForest = await GM.getValue(storageKey);
                        iframe.contentWindow.postMessage({
                            title: title,
                            metaTree: metaTree,
                            valueForest: valueForest === undefined ? [] : valueForest
                        }, "*");
                        break;
                    case "change":
                        // Update stored value
                        GM.setValue(storageKey, message.data.valueForest);
                        break;
                    case "close":
                        // Close iFrame
                        iframeExists = false;
                        iframe.remove();
                        // Resolve promise
                        resolve(message.data.valueForest);
                        break;
                    default:
                        // No need to error the promise here; I'm probably just observing a message from another script
                        console.warn("Unrecognised message 'event' value observed by key-based config script: '" + message.data.type + "'");
                }
            });

        }

    });

}