You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何让两个不同TamperMonkey脚本共享并编辑同一JSON文件?

TamperMonkey多脚本共享可编辑JSON物品列表的实现方案

针对你在Neopets的Safety Deposit和Quick Stock页面运行两个TamperMonkey脚本,需要共享可编辑JSON物品列表的需求,以下是两个可行的优化方案,避免合并脚本的麻烦:

方案1:利用同源localStorage存储(推荐)

由于两个脚本都运行在Neopets域名下,属于同源环境,浏览器原生的localStorage可被同一域名下的所有脚本访问和修改,完全满足共享+编辑的需求,且无需额外配置,适合分发给其他玩家。

实现代码

在两个脚本中都封装以下读写函数:

// 读取共享物品列表,无数据时返回空数组
function getSharedItemList() {
    const rawData = localStorage.getItem('neopets_inventory_db');
    return rawData ? JSON.parse(rawData) : [];
}

// 保存共享物品列表,并广播通知其他脚本数据更新
function saveSharedItemList(itemList) {
    localStorage.setItem('neopets_inventory_db', JSON.stringify(itemList));
    // 用BroadcastChannel发送更新通知
    const updateChannel = new BroadcastChannel('neopets_inventory_updates');
    updateChannel.postMessage({ type: 'db_updated', data: itemList });
    updateChannel.close();
}

在需要监听数据更新的脚本中添加监听逻辑(比如页面初始化时):

// 监听数据更新事件
const updateChannel = new BroadcastChannel('neopets_inventory_updates');
updateChannel.addEventListener('message', (event) => {
    if (event.data.type === 'db_updated') {
        // 这里处理数据更新后的业务逻辑,比如刷新界面、更新本地缓存
        const latestItemList = event.data.data;
        // 执行你的操作
    }
});

优势

  • 浏览器原生支持,分发脚本时用户无需额外配置或搭建服务器
  • 读写速度快,数据持久化(关闭浏览器后仍保留)
  • 支持实时通知,确保两个脚本数据同步

方案2:TamperMonkey GM存储+跨脚本消息传递

如果不想使用localStorage,可以结合TamperMonkey的GM存储API和window.postMessage实现跨脚本数据交互,将其中一个脚本作为数据存储的主节点。

实现代码

主脚本(比如Safety Deposit页面的脚本)

负责存储数据并响应其他脚本的请求:

// 注册菜单命令,方便调试查看数据
GM.registerMenuCommand('查看物品数据库', async () => {
    const data = await GM.getValue('neopets_shared_inventory');
    console.log(data || []);
});

// 监听其他脚本的消息请求
window.addEventListener('message', async (event) => {
    // 验证消息来源,防止恶意请求
    if (event.origin !== window.location.origin) return;

    switch(event.data.action) {
        case 'fetch_db':
            // 返回当前存储的物品列表
            const currentData = await GM.getValue('neopets_shared_inventory') || [];
            event.source.postMessage({ type: 'db_data', data: currentData }, event.origin);
            break;
        case 'update_db':
            // 更新存储的物品列表,并广播通知
            await GM.setValue('neopets_shared_inventory', event.data.data);
            window.postMessage({ type: 'db_updated', data: event.data.data }, window.location.origin);
            break;
    }
});

子脚本(Quick Stock页面的脚本)

通过消息请求数据或触发更新:

// 获取最新物品列表
async function fetchSharedInventory() {
    return new Promise((resolve) => {
        const listener = (event) => {
            if (event.data.type === 'db_data') {
                window.removeEventListener('message', listener);
                resolve(event.data.data);
            }
        };
        window.addEventListener('message', listener);
        window.postMessage({ action: 'fetch_db' }, window.location.origin);
    });
}

// 更新物品列表
function updateSharedInventory(itemList) {
    window.postMessage({ action: 'update_db', data: itemList }, window.location.origin);
}

// 监听数据更新通知
window.addEventListener('message', (event) => {
    if (event.origin !== window.location.origin) return;
    if (event.data.type === 'db_updated') {
        // 处理数据更新逻辑
        const latestItemList = event.data.data;
    }
});

优势

  • 数据与网站自身的localStorage隔离,避免冲突
  • 可通过TamperMonkey菜单直接查看数据,方便调试

方案选择建议

优先选择方案1,实现简单、无额外依赖,分发脚本时用户直接安装就能用;如果需要数据隔离或更灵活的调试方式,再考虑方案2。

内容的提问来源于stack exchange,提问作者rxtre

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.12 07:05:32