Key-Based Config

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

As of 2021-01-11. See the latest version.

This script should not be not be installed directly. It is a library for other scripts to include with the meta directive // @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.

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

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

        }

    });

}