在不同域上的脚本之间传输数据
问题描述:
使用localStorage有一种方法transfer data between pages on the same domain,但我需要在不同域之间传输数据。在Chrome中,我尝试使用符号链接,以便域共享存储,但在其他域中找不到设置在一个域中的项目,直到我重新启动Chrome。如何在不同域的用户脚本之间传输数据?我将使用任何可用的浏览器。在不同域上的脚本之间传输数据
这将只能用于个人用途
答
我会用@include
包括两个域,然后使用GM_getValue
和GM_setValue
来存储和检索数据。
我还介绍了如何使用GM_registerMenuCommand
函数的示例,该函数在用户从userscript插件弹出窗口中选择选项时打开提示。
// ==UserScript==
// @name Pinky
// @namespace http://pinkyAndTheBrain.net/
// @version 0.1
// @description try to take over the world!
// @author You
// @include https://domain1.com
// @include https://domain2.com
// @grant GM_getValue
// @grant GM_setValue
// @grant GM_registerMenuCommand
// ==/UserScript==
/* global GM_getValue, GM_setValue, GM_registerMenuCommand */
/* jshint esnext:true */
(() => {
'use strict';
// get previous setting (or set to Pinky as default)
let char = GM_getValue('character', 'Pinky');
// do something fun!
// called through the userscript addon
GM_registerMenuCommand('Are you Pinky or Brain?',() => {
const value = prompt('Enter "p" or "b"', char);
if (value !== null) {
// default to Pinky
char = /^b/i.test(value) ? 'Brain' : 'Pinky';
GM_setValue('character', char);
}
});
})();
我不知道的,可以解决这个问题为您带来任何浏览器提供的方式,也有一些很好的理由,这将是一件坏事(安全性,隐私,存储限制,安全!保密!)。然而,我想如果你热衷于解决这个问题,并且不关心你自己的用户脚本的这些东西,你可以将数据发布到不同的资源。例如你在'website-a'和'website-b'上运行userscript,并使用AJAX他们从/到'website-c'获取/发布数据。如果这是你想与其他人分享的东西,那么对于个人资料来说,再好不过了,情况会变得更加复杂。 – Dymos