I was wondering if there is any way to communicate with another tab or window that runs the same userscript (on the same domain).

window.postMessage() seems to be an option, but I'm not sure if/how it works with tabs that the page did not create.

At the moment I use GM_setValue/GM_getValue and setInterval to regularly check for a changed value. It works but it looks hackish.

Re: @cuzi:

If you are running the script within the Sandbox (e.g. @grant something other than none) then they are isolated scopes unless you use unsafeWindow with a page scope variable (implementation just changed on this btw). If you run @grant none you could use a page scope variable directly but risk detection again. Messaging can be detected by the host site too. So it's really up to you what you want to do and risk. I've used GMs storage to transfer data between windows/tabs before just to maintain non-detection.

Re: @Marti:
Thanks for your reply. Could you explain what you mean by page scope variable or how you would actually do it with unsafeWindow?

Re: @cuzi:
Thanks for asking this. I want to propagate some preference changes across open tabs of a web app (no userscript) without the user having to reload from the server, and needed to look into this.

In modern browsers (unfortunately, I also need to support IE8), I should in theory be able to use an event listener on local storage to share data between tabs/windows. Because it's my site, I don't have to worry about private information leaking to the site, but if that is a concern for what you are passing, then local storage would not be the ideal approach. I suppose in that situation you could increment a counter (or otherwise signal the other pages) to trigger a GM_getValue().

Here are some discussions of using local storage in this manner (I have not tried to implement it):

Re: @cuzi:

At the moment I use GM_setValue/GM_getValue and setInterval to regularly check for a changed value.

if you don't like timers, you may track the changes of GM_values inside window.onfocus event handler.