2

I'm planning to write a Greasemonkey script that will run on a particular URL (@include or @match), but, I'd like to have it so that if I open up one or more additional tabs with the same URL, the Greasemonkey script will NOT run on the second (and further) duplicate pages.

So, for example, say I have a Greasemonkey script that makes "style" changes to a webpage (or in some other way "modifies" it) when I open "google.com". I want to be able to open another tab to "google.com" where that Greasemonkey script will not run. Or, more correctly, will run but will detect that it is already running and "modifying" the page in the first tab and then "exit" in the second tab. The second tab will remain open but is just "unaffected" by the script.

I don't know if a Greasemonkey script can "see" other opened tabs, but if it can, my first thought was to check if another tab was already loaded to that page, and then to exit if it found that it was.

But it would have to be a bit more involved than that because:

  1. If the first tab was closed and a new ("third") tab was opened to that URL, I would want the Greasemonkey script to run and "modify" that page as it would have on the first page.
  2. Similarly, if I "refresh" the first tab (say after editing the Greasemonkey script, or for another reason), I would want the Greasemonkey script to run and "modify" the reloaded page.

So, it seems the Greasemonkey script would have to either:

  1. Check the URL of each other tab to find a "match", and then "Peek into that tab" to see if the Greasemonkey script was "actively" modifying that page (perhaps look for a hidden, empty, named "DIV" or other element).
  2. Use some sort of "local storage" or "cross-tab global (superglobal ?) variables" to keep track of the tabs as they open and close.

I'm not looking for someone to write this script for me, I feel I'm capable of writing the script if I knew how to do a few things. If a script that does all or most of this already exists then that would be great too.

To get started, I need to know if (and how) a Greasemonkey script:

  1. Can "see" other tabs.
  2. Can read the URLs (and perhaps the "title" text) of the other tabs.
  3. Check for existence of (and perhaps read but not write) elements in the other tab.
  4. Manage "local (computer) storage" or manage "cross-tab global variables" (if this can even be done).

1 Answer 1

6

Greasemonkey cannot:

  1. See other tabs.
  2. Check the URLs of other tabs.
  3. Check for DOM nodes/elements in other tabs.

The reason is that Greasemonkey, Tampermonkey, etc. do not expose these APIs to/for userscripts.
(Full-fledged browser extensions can do all of those things.)


Greasemonkey can:

  1. Cross communicate between script instances using GM_setValue()Doc and GM_getValue()Doc.
  2. Sometimes, you can also use .postMessage().   (Example)
    This will probably only work for you if you use window.open to create the new tab.
  3. Since you are on the same domain, you could also use localStorage or cookies to cross communicate.
  4. Finally, if the new tabs are opened from the master tab, you van communicate to the new tab via URL parameters (or the hash). See this userscript for an example of that. (Note how the SEMC links are handled.)

I'm not looking for someone to write this script for me, I feel I'm capable of writing the script if I knew how to do a few things. If a script that does all or most of this already exists then that would be great too.

All the bits and pieces have been addressed on Stack Overflow, but not in any one question that I recall.

If you use variables to communicate (methods 1 or 3), be sure to have something like a beforeunload event handler to clear the variables before the master page closes.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .