Frage

I have a Greasemonkey script with a few @include rules. I'd like to know exactly which one was matched, because the script has to behave slightly differently depending on the URL.

Is there a way to find this out through Greasemonkey's API? I know I can get this information through DOM, but I'm hoping Greasemonkey's API provides something like that already.

War es hilfreich?

Lösung

Greasemonkey does not tell a script which rule it matched. Instead, your script can test properties of the window.location object. EG:

if (/stackoverflow\.com/i.test (location.hostname) ) {
    alert ("Yay!");
}
else if (/quora\.com/i.test (location.hostname) ) {
    alert ("Uh oh!");
}


If you really wanted to, you could loop through the includes and matches arrays of the GM_info object, and try to determine which one(s) would fire on the current location, but that's not foolproof and there's zero need for it (except, maybe, an over-zealous code review of the script).

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top