Key-Based Config

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

Stan na 11-01-2021. Zobacz najnowsza wersja.

Ten skrypt nie powinien być instalowany bezpośrednio. Jest to biblioteka dla innych skyptów do włączenia dyrektywą meta // @require https://update.greasyfork.org/scripts/419978/889804/Key-Based%20Config.js

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Greasemonkey lub Violentmonkey.

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

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana będzie instalacja rozszerzenia Tampermonkey lub Userscripts.

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

Aby zainstalować ten skrypt, musisz zainstalować rozszerzenie menedżera skryptów użytkownika.

(Mam już menedżera skryptów użytkownika, pozwól mi to zainstalować!)

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.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Musisz zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

(Mam już menedżera stylów użytkownika, pozwól mi to zainstalować!)

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

        }

    });

}