Shared_Storage_Bridge

Shared_Storage_Bridge script for the extension

Script này sẽ không được không được cài đặt trực tiếp. Nó là một thư viện cho các script khác để bao gồm các chỉ thị meta // @require https://update.greasyfork.org/scripts/566657/1756895/Shared_Storage_Bridge.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.

(Tôi đã có Trình quản lý tập lệnh người dùng, hãy cài đặt nó!)

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