Search code examples
javascriptgreasemonkeyuserscriptstampermonkey

userscript is blocking itself when loaded on more than one tab


I have this problem with greasemonkey 3.17 on FF56 as well as in tampermonkey on Vivaldi, and I cannot find anything about this problem via google.

Example script:

// ==UserScript==
// @name        stackoverflow.com
// @namespace   siod87gbnwf87rnsfdkn
// @include     https://stackoverflow.com/*
// @version     1
// ==/UserScript==

alert(1);
alert(2);

As you can see it's super simple and should give one message box, and after clicking OK the second one. This basically works fine, but now for the problem:

  • Go to stackoverflow.com

  • Use middle mouse button two open a question in a new tab twice directly after another, so that the second tab opens while the other still loads

What happens is, that both tabs will show popupbox "1" as expected, but when you click OK on the box from the first tab the script will pause until you click OK on the popupbox in the second tab. After you clicked OK twice on the second tab, then the alert on the first tab will trigger.

Why do the scripts not run independently from each other? This has nothing to do with alert, I had this problem with a more complicated script, but for showing my problem, alert works the same.


Solution

  • Why do the scripts not run independently from each other?

    They are run "independently" in the sense that they do not share the global space. However, blocking calls such as alert are handled by the browser itself, and the ordering is not specified in the ECMAScript specification.

    One "solution" would be to not use blocking calls such as alert. Instead, if you need to print out values, use console.log or console.info.