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.
How can I go about detecting whether the active userscript manager is Greasemonkey or not?
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}`);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With