Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect which userscript manager is running the script

I'm using a function in a userscript that I'm writing which does not work in Greasemonkey due to the limitations of Greasemonkey. This function is not necessary for proper operation of the userscript, but it improves user experience, so I don't want to just remove it entirely.

I tried using a try { ... } catch() { ... } block, but unfortunately Greasemonkey ceases execution of the script as soon as it attempts execution of the function instead of throwing an exception. So I instead decided to prevent execution of the function when the script is loaded via Greasemonkey, but I have been unable to find a method of doing that.

  • I've read through the API reference, but was unable to find anything useful.
  • I found this github issue that would allow detection of Greasemonkey but that seems to have been "fixed" now.
  • I looked through this topic on userscripts-mirror.org, but the proposed solution in that thread checks for the existence of GM* functions, which are also available in most other userscript managers (specifically Tampermonkey and Violentmonkey which support the function that Greasemonkey does not).

How can I go about detecting whether the active userscript manager is Greasemonkey or not?

like image 342
Random Logic Avatar asked Jul 04 '26 08:07

Random Logic


1 Answers

In the same vein as the third item in your list, you could choose a function that is not supported by Greasemonkey, but is supported by the userscript managers that support your function.

From this comparison table we can see that the @author meta property is not supported by Greasemonkey, but it is supported by Tampermonkey and Violentmonkey. This means that if you set the @author meta property, you can check if that exists via GM_info.script.author.

// ==UserScript==
// @name         Greasemonkey Check
// @description  Checks if the script is loaded by Greasemonkey or not
// @author       @TinyGiant
// @grant        none
// ==/UserScript==

const isGM = 'undefined' === typeof GM_info.script.author;
console.log(`Is Greasemonkey? ${isGM}`);