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?
Checking what manager is running is a poor approach, which will prove to be brittle and high maintenance. The smart thing to do is to check if this mysterious function exists, or works as needed.
This is the same type of problem as "browser sniffing" and the answer is the same: Use feature detection instead.
EG:
if (typeof dicyFunc == "function") {
//-- Use the function
dicyFunc ();
}
else {
console.error ("This userscript engine does not support dicyFunc.");
}
Update for user comment: Sometimes you might also need a try... catch block. EG:
try {
REALLY_dicyFunc ();
}
catch (zError) {
console.error ("REALLY_dicyFunc fail on this engine: ", zError);
}
You need to give a concrete example (Make an MCVE) for more.
If you insist on engine detection, then see this answer to a near duplicate question.
Essentially, you would use GM_info.scriptHandler
property, possibly backed up by the GM_info.version
property.
For best results, make a feature request for Greasemonkey to support the . (Tampermonkey and ViolentMonkey already do.)
There is a recently closed pull request for this for Greasemonkey, so maybe it will be in the next version?scriptHandler
property