简体   繁体   中英

Transferring data between userscripts on different domains

There's a way to transfer data between pages on the same domain , using localStorage, but I need to transfer data between different domains. In Chrome, I tried using symbolic links so the domains share the storage, but items set in one domain aren't found at the other domain until I reboot chrome. How do I transfer data between userscripts on different domains? I'll use any browser that'll work.

This'll only be for personal use

I would include both domains using @include and then use GM_getValue and GM_setValue to store and retrieve the data.

I've also included an example on how to use the GM_registerMenuCommand function which opens a prompt when the user selects the option from the userscript addon popup.

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

})();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM