Get 2 userscripts to interact with each other?

这一生的挚爱 提交于 2019-11-28 01:21:38

The scripts cannot directly interact with each other and // @namespace is just to resolve script name conflicts. (That is, you can have 2 different scripts named "Link Remover", only if they have different namespaces.)

Separate scripts can swap information using:

  • Cookies -- works same-domain only
  • localStorage -- works same-domain only
  • Sending and receiving values via AJAX to a server that you control -- works cross-domain.

That's it.

Different running instances, of the same script, can swap information using GM_setValue() and GM_getValue(). This technique has the advantage of being cross-domain, easy, and invisible to the target web page(s).

See this working example of cross-tab communication in Tampermonkey.


On Chrome, and only Chrome, you might be able to use the non-standard FileSystem API to store data on a local file. But this would probably require the user to click for every transaction -- if it worked at all.

Another option is to write an extension (add-on) to act as a helper and do the file IO. You would interact with it via postMessage, usually.

In practice, I've never encountered a situation were it wasn't easier and cleaner to just merge any scripts that really need to share data.


Also, scripts cannot share code, but they can inject JS into the target page and both access that.

Finally, AFAICT, scripts always run sequentially, not in parallel. But you can control the execution order from the Manage User Scripts panel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!