【问题标题】:Transferring data between userscripts on different domains在不同域上的用户脚本之间传输数据
【发布时间】:2016-11-13 02:22:41
【问题描述】:

transfer data between pages on the same domain 有一种方法,使用 localStorage,但我需要在不同域之间传输数据。在 Chrome 中,我尝试使用符号链接,以便域共享存储,但在一个域中设置的项目在我重新启动 chrome 之前在另一个域中找不到。如何在不同域的用户脚本之间传输数据?我会使用任何可以工作的浏览器。

仅供个人使用

【问题讨论】:

  • 我不知道有任何浏览器提供的方法可以为您解决这个问题,有一些很好的理由说明这将是一件坏事(安全、隐私、存储限制、安全!、隐私! )。但是,我想如果您热衷于解决它并且对您自己的用户脚本不太关心这些事情,您可以将数据发布到不同的资源。例如您在website-awebsite-b 上运行用户脚本,并使用AJAX 他们从website-c 获取/发布数据。同样,非常适合个人内容,如果这是您想与其他人分享的内容,则会变得更加复杂。

标签: javascript userscripts


【解决方案1】:

我将使用@include 包含两个域,然后使用GM_getValueGM_setValue 来存储和检索数据。

我还提供了一个关于如何使用 GM_registerMenuCommand 函数的示例,当用户从用户脚本插件弹出窗口中选择选项时,该函数会打开一个提示。

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

})();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多