Key-Based Config

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

当前为 2021-08-01 提交的版本,查看 最新版本

此脚本不应直接安装。它是供其他脚本使用的外部库,要使用该库请加入元指令 // @require https://update.cn-greasyfork.org/scripts/419978/956385/Key-Based%20Config.js

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

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

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 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.
// @grant       GM.setValue
// @grant       GM.getValue
// ==/UserScript==

const FRAME_URL = 'https://s3.eu-west-2.amazonaws.com/callumlatham.com/key-based-config/index.html';
const STYLE = {
    'position': 'fixed',
    'height': '100vh',
    'width': '100vw'
};

let isOpen = false;

function kbcConfigure(storageKey, title, metaTree, customStyle = {}) {
    return new Promise((resolve, reject) => {
        if (isOpen) {
            reject(new Error('A Key-Based Config iFrame is already open.'));
        } 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 {
            const iframe = document.createElement('iframe');
            const style = {
                ...STYLE,
                ...customStyle
            }

            iframe.src = FRAME_URL;

            for (const [property, value] of Object.entries(style)) {
                iframe.style[property] = value;
            }

            window.document.body.appendChild(iframe);

            isOpen = true;

            // Listen for iFrame communication
            window.addEventListener('message', async (message) => {
                switch (message.data.event) {
                    case 'open':
                        // Pass initialisation 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
                        isOpen = false;
                        iframe.remove();

                        // Resolve promise
                        resolve(message.data.valueForest);

                        break;

                    case 'error':
                        // Close iFrame
                        isOpen = false;
                        iframe.remove();

                        // Resolve promise
                        reject(message.data.reason);

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