proxy-storage

Proxy wrapper around GM storage API, with support for nested object by proxy.

لا ينبغي أن لا يتم تثبيت هذا السكريت مباشرة. هو مكتبة لسكبتات لتشمل مع التوجيه الفوقية // @require https://update.greasyfork.org/scripts/488549/1335042/proxy-storage.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.

(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.

ستحتاج إلى تثبيت إضافة مثل Stylus لتثبيت هذا النمط.

ستحتاج إلى تثبيت إضافة لإدارة أنماط المستخدم لتتمكن من تثبيت هذا النمط.

ستحتاج إلى تثبيت إضافة لإدارة أنماط المستخدم لتثبيت هذا النمط.

ستحتاج إلى تثبيت إضافة لإدارة أنماط المستخدم لتثبيت هذا النمط.

(لدي بالفعل مثبت أنماط للمستخدم، دعني أقم بتثبيته!)

// ==UserScript==
// @name         proxy-storage
// @description  Proxy wrapper around GM storage API, with support for nested object by proxy.
// @version      0.0.1
// @namespace    owowed.moe
// @author       owowed <[email protected]>
// @grant        GM_getValue
// @grant        GM_setValue
// @grant        GM_deleteValue
// @grant        GM_listValues
// @license      LGPL-3.0
// ==/UserScript==

function createGmProxyStorage({
    get = GM_getValue,
    set = GM_setValue,
    deleteValue = GM_deleteValue,
    list = GM_listValues
} = {}) {
    const storage = { get, set, deleteValue, list };
    return new Proxy({}, {
        get(target, prop, receiver) {
            const value = storage.get(prop);
            if (isObjectOrArray(value)) {
                return makeNestedTargetProxy(value, prop, storage.set);
            }
            else {
                return value;
            }
        },
        set(target, prop, newValue, receiver) {
            storage.set(prop, newValue);
            return true;
        },
        deleteProperty(target, prop) {
            storage.deleteValue(prop);
            return true;
        },
        ownKeys(target) {
            return storage.list();
        },
        has(target, prop) {
            return this.ownKeys(target).includes(prop);
        },
        getOwnPropertyDescriptor(target, prop) {
            if (this.has(target, prop)) {
                return {
                    enumerable: true,
                    configurable: true,
                    writable: false,
                    value: this.get(target, prop)
                };
            }
        },
    });
}

function isObjectOrArray(value) {
    return Array.isArray(value) || (typeof value == "object" && value != null);
}

function makeNestedTargetProxy(rootTarget, rootTargetProp, setter) {
    const set = (target, prop, value) => {
        target[prop] = value;

        setter(rootTargetProp, rootTarget); /* `rootTarget` reference will always get updated */

        return value;
    };

    const get = (target, prop) => {
        const value = target[prop];

        if (isObjectOrArray(value)) {
            return new Proxy(value, { get, set });
        }

        return value;
    };

    return new Proxy(rootTarget, { get, set });
}