Shared_Storage_Bridge

Shared_Storage_Bridge script for the extension

Dieses Skript sollte nicht direkt installiert werden. Es handelt sich hier um eine Bibliothek für andere Skripte, welche über folgenden Befehl in den Metadaten eines Skriptes eingebunden wird // @require https://update.greasyfork.org/scripts/566657/1756895/Shared_Storage_Bridge.js

Du musst eine Erweiterung wie Tampermonkey, Greasemonkey oder Violentmonkey installieren, um dieses Skript zu installieren.

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.

Sie müssten eine Skript Manager Erweiterung installieren damit sie dieses Skript installieren können

(Ich habe schon ein Skript Manager, Lass mich es installieren!)

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