This question is closely related to: Greasemonkey/Tampermonkey @match for a page with parameters.
@match
only works on the protocol/scheme, host, and pathname of a URL.
To trigger off the hash value (officially called the "fragment"), you can either use @include
note-1 or use @match
and also test the URL yourself.
So use code like:
...
// @match *://YOUR_SERVER.COM/YOUR_PATH/
// ==/UserScript==
if ( ! /#unicorn.*/.test(location.hash) ) return;
// REST OF YOUR CODE HERE.
For this example, that code:
- Runs :
https://YOUR_SERVER.COM/YOUR_PATH/#unicorn
- Runs :
https://YOUR_SERVER.COM/YOUR_PATH/#unicorn-mode
, etc.
- Ignores:
https://YOUR_SERVER.COM/YOUR_PATH/#unicom
- Ignores:
https://YOUR_SERVER.COM/YOUR_PATH/?foo=bar#unicorn
(Add *
at end of @match
if desired.)
IMPORTANT: This is for initial page loads only. The hash
can change after a page loads, and that's more complicated to deal with and not specified in this question.
So, for that kind of scenario, open a new question and refer to this one.
Notes:
- Unfortunately, Tampermonkey has yet to implement
@include
the way Greasemonkey did, so the fragment/hash is ignored even in @include
and seems likely to remain that way for the near future.