Key-Based Config

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

Tính đến 11-01-2021. Xem phiên bản mới nhất.

Script này sẽ không được không được cài đặt trực tiếp. Nó là một thư viện cho các script khác để bao gồm các chỉ thị meta // @require https://update.greasyfork.org/scripts/419978/889804/Key-Based%20Config.js

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

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.

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

(Tôi đã có Trình quản lý tập lệnh người dùng, hãy cài đặt nó!)

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        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 = "http://ctl-bucket-1.s3-website.eu-west-2.amazonaws.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.error("Unrecognised message 'event' value observed by key-based config script: '" + message.data.type + "'");
                }
            });

        }

    });

}