I have a lot open tabs of a website - and I want to limit it to only one URL.
The problem is that the URL has #
in it and I can't seem to @match it.
The URL looks like: https://xxx.xxxxxx.xxx/#/xxxxxx
I tried:
// @match https://xxx.xxxxxx.xxx/#/xxxxxx - doesnt work
// @match https://xxx.xxxxxx.xxx/* - works - but works on all tabs
// @match https://xxx.xxxxxx.xxx/*/xxxxxx -doesnt work;/
// @match https://xxx.xxxxxx.xxx/\#/xxxxxx -doesnt work;/
Any combination of * and anything after it doesn't work. :(
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 note-1 or use @include
@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:
https://YOUR_SERVER.COM/YOUR_PATH/#unicorn
https://YOUR_SERVER.COM/YOUR_PATH/#unicorn-mode
, etc.https://YOUR_SERVER.COM/YOUR_PATH/#unicom
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:
@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.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