Shared_Storage_Bridge

Shared_Storage_Bridge script for the extension

Αυτός ο κώδικας δεν πρέπει να εγκατασταθεί άμεσα. Είναι μια βιβλιοθήκη για άλλους κώδικες που περιλαμβάνεται μέσω της οδηγίας meta // @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 για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Userscripts για να εγκαταστήσετε αυτόν τον κώδικα.

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

Θα χρειαστεί να εγκαταστήσετε μια επέκταση διαχείρισης κώδικα χρήστη για να εγκαταστήσετε αυτόν τον κώδικα.

(Έχω ήδη έναν διαχειριστή κώδικα χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

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.

(Έχω ήδη έναν διαχειριστή στυλ χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

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