1

How to store information globally so multiple TamperMonkey/GreaseMonkey scripts can access it and is also shared across multiple domains (cross-domain)?

There is no documentation available on this in either TamperMonkey or GreaseMonkey.

I tried using GM_SuperValue and GM_setValue/GM_getValue scripts but the scope of these stored values are limited to the script from which it is stored as mentioned here. As a result, a value stored from script A cannot be retried from script B.

LocalStorage solves the problem is script A and B are being used on the same domain. But doesn't solve the problem for cross-domain.

Is there a way to use a shared memory for cross-domain across multiple scripts?

Update after comments:
I have a flag which I got from domain A in script X. I need to use this flag in domain B in script Y.

Community
  • 1
  • 1
kvn
  • 2,210
  • 5
  • 20
  • 47
  • Do you have control over both domains? – guest271314 Mar 06 '17 at 07:48
  • No. If I have the control, I wouldn't be using TamperMonkey at all. I building a feature to automate my daily routine. – kvn Mar 06 '17 at 07:50
  • _"I building a feature to automate my daily routine."_ Can you further describe what you are trying to achieve? – guest271314 Mar 06 '17 at 07:51
  • 2
    Just use a single script which would do things based on the current url. – wOxxOm Mar 06 '17 at 07:53
  • What do you mean by "do things"? Can you include `javascript` you have tried at Question? – guest271314 Mar 06 '17 at 07:54
  • You can use ` – guest271314 Mar 06 '17 at 07:57
  • @wOxxOm Yes, I had that idea in mind but that would make the code very messy. IMHO for most cases, the url match would be complex if the script is meant to work on multiple domains. – kvn Mar 06 '17 at 07:58
  • @guest271314 Updated the question with the description of the problem in simpler terms. – kvn Mar 06 '17 at 08:02
  • The simplest way is to do as wOxxOm suggested and merge the scripts into one "state machine" script. Or, if *Script A* opens *Script B*, you can use URL parameters or the URL hash to pass data. ... Google docs/drive can be user scripted but it's a PITA. ... The other alternatives require a paid service or you hosting your own web app. How much data are you talking about? – Brock Adams Mar 06 '17 at 08:53
  • At the moment, @wOxxOm suggestion is my only way to go. I tried using URL parameters, but _Script A_ opens _URL A_ which redirects to another _URL B_ (server-side URL redirection). This URL B triggers my _Script B_, so couldn't use that option either. _"How much data are you talking about?"_ - I just need to store a flag (boolean). – kvn Mar 06 '17 at 09:05

1 Answers1

1

You can use <iframe> elements and window.postMessage() to communicate across different domains. Where the second parameter to postMessage() is the targetOrigin where message is sent. The targetOrigin, if not "*", must match protocol, port and hostname of the target window.

guest271314
  • 1
  • 15
  • 104
  • 177
  • @SGSVenkatesh See also [How do you use window.postMessage across domains?](http://stackoverflow.com/questions/3457391/how-do-you-use-window-postmessage-across-domains), [How to clear the contents of an iFrame from another iFrame](http://stackoverflow.com/questions/33645685/how-to-clear-the-contents-of-an-iframe-from-another-iframe/), [How can I load a shared web worker with a user-script?](http://stackoverflow.com/questions/38810002/how-can-i-load-a-shared-web-worker-with-a-user-script) – guest271314 Mar 06 '17 at 08:09
  • Specifications [HTML - Living Standard - 9 Communication](https://html.spec.whatwg.org/multipage/comms.html#comms) – guest271314 Mar 06 '17 at 08:17