How can one use @match
and @exclude
rules in a userscript to match URLs that have an arbitrary path at a certain level (a "level" being each new /
in the path, not sure if there's a more technical name for it), but not at sublevels (i.e. having subsequent forward-slashes /
)?
Example
A userscript is intended to match the homepage of any subreddit on Reddit.com, but not any sub-pages of that subreddit. This is what I've tried:
@match https://*.reddit.com/r/*/
@exclude https://*.reddit.com/r/*/*/
My understanding is that this SHOULD match, for example, https://reddit.com/r/funny
but NOT match https://reddit.com/r/funny/submit
, https://reddit.com/r/funny/new
, https://reddit.com/r/funny/comments/blahblahblah
, etc. However, my userscript seems to be firing on those sub-pages anyway.
I'm basing this construction off the Mozilla documentation for Match Patterns, which states:
Pattern
Match HTTPS URLs hosted on "mozilla.org", whose path contains a component "b" somewhere in the middle. Will match URLs with query strings, if the string ends in a /.
Example matches
https://mozilla.org/a/b/c/d/#section1
https://mozilla.org/a/b/c/d/?foo=/
https://mozilla.org/a?foo=21314&bar=/b/&extra=c/
Example non-matches
(unmatched path)
(unmatched path)
https://mozilla.org/a/b/c/d/?foo=bar
(unmatched path due to URL query string)
I know I could solve this using the deprecated @include
with RegEx (or RegEx in @exclude
, for that matter, as Regex in userscript rules generally reduces performance). I know I could add logic to my userscript that validates the pathname before executing the rest of the script, but I'd like an elegant solution using only the basic @match
and @exclude
if possible.
Is there a better way to form these rules to make that happen?