12

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. :(

Brock Adams
  • 90,639
  • 22
  • 233
  • 295
Krzysztof
  • 189
  • 1
  • 10

1 Answers1

11

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 @includenote-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:

  1. 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.
Brock Adams
  • 90,639
  • 22
  • 233
  • 295
  • 1
    thank you, that doesn't fully solve my problem, but i guess from your post - it is not possible right now. Two notes regarding this workaround: 1) it will add to "active scripts count" +1 on everypage - despite them not using this particular script 2) it should be if ( ! /#\/unicorn.*/.test(location.hash) ) return; for my case :) Thank you very much again:) – Krzysztof Sep 21 '18 at 20:24
  • @Krzysztof, You're welcome. Alas, the script count thing is not solvable until Tampermonkey's developer addresses it. The `@include` method would solve that if it worked, and it might on Violentmonkey (haven't checked yet). – Brock Adams Sep 21 '18 at 20:41
  • @Krzysztof, if this doesn't fully solve your problem, open a new question with the additional issues. Include an [mcve] and/or a link to the target page(s). – Brock Adams Sep 21 '18 at 20:43