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 เพื่อติดตั้งสคริปต์นี้

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

คุณจะต้องติดตั้งส่วนขยาย เช่น Tampermonkey หรือ Violentmonkey เพื่อติดตั้งสคริปต์นี้

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         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);