2

I'm trying to share variable between 2 different Tampermonkey scripts running in 2 separate tabs.

I tried using GM_setValue in one script then retrieving it with GM_getValue in the other one but without any success, so I assume there's separate storage for each script.

Is there any easy way to do this? Am I just missing something simple? Can I somehow make both scripts share the same storage?

Woozie
  • 21
  • 1
  • 3
  • 1
    Merge the scripts, then the script can use `GM_setValue` to communicate between instances. Or use messaging; See http://stackoverflow.com/questions/11769066/how-can-two-instances-of-a-userscript-communicate-between-frames . – Brock Adams Sep 20 '14 at 17:36
  • See also: [How To Share Data Between Scripts on Different Domains](https://stackoverflow.com/questions/77011466/how-do-i-access-the-same-json-file-from-two-different-tampermonkey-scripts/77028459#77028459) – cssyphus Sep 02 '23 at 14:56

1 Answers1

2

Yes, you can. You can use localStorage, or you can use a database at your server and get/set values with AJAX.

Community
  • 1
  • 1
Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
  • 1
    I should've mentioned that both scripts are running on different domains, so localStorage wouldn't work. I don't want to use server for something so simple unless it's really the only way, I'm sure there must be a way to do it locally. – Woozie Sep 20 '14 at 14:50
  • No, actually this is not so simple. Imagine the case when Site1 and Site2 are owned by different people. If the crew of Site1 could reach the script used for Site2, then there was no guarantee they will not steal data from Site2. For reasons of security you need to use the server. – Lajos Arpad Sep 20 '14 at 14:52
  • I understand the security reasons for normal scripts, but since it's userscript that's running in sandbox environment and not directly at the page context I was hoping there's a way to do it within Tampermonkey – Woozie Sep 20 '14 at 15:02
  • The browser does not know whether it is in sandbox in general. You can have sensitive data in userscript as well. For instance if there is a timestamp of login, the owner of another website should not know when you logged in into the other site except he is allowed to. What if you store an offline version in localStorage to enable you to use some of the features even without connection to the internet? Should I be able to read all your offline data (username, private messages and so on)? – Lajos Arpad Sep 20 '14 at 15:20
  • but if localStorage is hopeless, because it’s not secure, it’s unreliable changeable and you don’t want to connect to a server? – Dr.G Sep 25 '20 at 01:04
  • @Dr.G sessionStorage is slightly more secure than localStorage, because you need to have an active session to use it. Otherwise it behaves similarly. – Lajos Arpad Sep 25 '20 at 11:49
  • yes, but both allow editing of data using dev tools, this is dangerous, if any dev is taking values from storage to send to webservices – Dr.G Sep 25 '20 at 16:51
  • @Dr.G that's true. This is why I used the word "slightly" in my previous comment. Having to overcome the creation of a session is a blocker if no such session exists at the hacking attempt. – Lajos Arpad Sep 26 '20 at 11:46