🎉 欢迎访问GreasyFork.Org 镜像站!本镜像站由公众号【爱吃馍】搭建,用于分享脚本。联系邮箱📮

Greasy fork 爱吃馍镜像

Greasy Fork is available in English.

📂 缓存分发状态(共享加速已生效)
🕒 页面同步时间:2026/01/15 04:46:01
🔄 下次更新时间:2026/01/15 05:46:01
手动刷新缓存

proxy-storage

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

This script should not be not be installed directly. It is a library for other scripts to include with the meta directive // @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.

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