Key-Based Config

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

Verze ze dne 11. 01. 2021. Zobrazit nejnovější verzi.

Tento skript by neměl být instalován přímo. Jedná se o knihovnu, kterou by měly jiné skripty využívat pomocí meta příkazu // @require https://update.greasyfork.org/scripts/419978/889804/Key-Based%20Config.js

K instalaci tototo skriptu si budete muset nainstalovat rozšíření jako Tampermonkey, Greasemonkey nebo Violentmonkey.

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

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Userscripts.

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

K instalaci tohoto skriptu si budete muset nainstalovat manažer uživatelských skriptů.

(Už mám manažer uživatelských skriptů, nechte mě ho nainstalovat!)

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.

(Už mám manažer uživatelských stylů, nechte mě ho nainstalovat!)

// ==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 + "'");
                }
            });

        }

    });

}