Shared_Storage_Bridge

Shared_Storage_Bridge script for the extension

此脚本不应直接安装。它是供其他脚本使用的外部库,要使用该库请加入元指令 // @require https://update.greasyfork.org/scripts/566657/1756895/Shared_Storage_Bridge.js

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Shared_Storage_Bridge
// @description  Shared_Storage_Bridge script for the extension
// @version      0.1
// ==/UserScript==
 function createSharedStorages({ timeout = 3000, maxRetries = 3 } = {}) {
        const pendingRequests = new Map();

        function generateRequestId() {
            return Math.random().toString(36).substring(2, 10);
        }

        function send(action, key, value = null) {
            const id = generateRequestId();
            let attempts = 0;

            return new Promise((resolve, reject) => {
                const trySend = () => {
                    attempts++;

                    const timer = setTimeout(() => {
                        pendingRequests.delete(id);
                        if (attempts < maxRetries) {
                            trySend(); // Retry
                        } else {
                            reject(new Error(`Request timed out after ${maxRetries} attempts`));
                        }
                    }, timeout);

                    // Store resolver to be called on success
                    pendingRequests.set(id, (msg) => {
                        clearTimeout(timer);
                        resolve(msg);
                    });

                    // Send message
                    window.postMessage(
                        { direction: "toExtension", action, key, value, requestId: id },
                        "*"
                    );
                };

                trySend(); // Initial attempt
            });
        }
        // Listen for responses
        window.addEventListener("message", function (event) {
            const msg = event.data;
            if (msg?.direction !== "fromExtension") return;

            const { requestId } = msg;
            if (requestId && pendingRequests.has(requestId)) {
                const resolver = pendingRequests.get(requestId);
                pendingRequests.delete(requestId);
                resolver(msg);
            }
        });

        return { send };
    }
    window.SharedStorages = createSharedStorages();

    const getSharedStorages = async (key) => {
        let { value } = await window.SharedStorages.send("get", key);
        try { return JSON.parse(value); } catch { return value; }
    };

    const setSharedStorages = (key, value) => window.SharedStorages.send("set", key, JSON.stringify(value));

    const deleteSharedStorages = (key) => window.SharedStorages.send("delete", key);