Greasemonkey/ Tampermonkey @match for a page with parameters
Solution 1
@match
only works on the protocol/scheme, host, and pathname of a URL.
To trigger off the query parameters, you can either use @include
or use @match
and also test the URL yourself.
Note that the @match
approach performs faster.
With @include
, you can use a regex syntax. See, also Include and exclude rules.
In this case, use either:
...
// @include /^https?://example\.com/page\.php*key1=value1*/
// ==/UserScript==
**Or:**
...
// @match *://example.com/page.php*
// ==/UserScript==
if (/\bkey1=value1\b/.test (location.search) ) {
// DO YOUR STUFF HERE.
}
Solution 2
According to the documentation for @match
, it doesn't appear that query string parameters are something the Greasemonkey engine will match on:
https://developer.chrome.com/extensions/match_patterns.html
noquierouser
Updated on August 12, 2021Comments
-
noquierouser over 2 years
I'm working on a script that must be executed in a certain page, depending on the parameters it has. The URL is like this:
http://example.com/page.php?key1=value1&key2=value2&...
And I need to match it when
page.php
has thekey1=value1
among its parameters.Now I'm using
@match http://example.com/page.php?key1=value1&*
But it doesn't match if
page.php
has no other parameters. It also won't match ifkey1
is not the first parameter either.Is there any way to match a page according to a parameter?
-
noquierouser over 10 yearsThis worked, except for a couple things. I used the
@match
example because@include
didn't work (I will keep on trying, though). Also, I had to do this match to make it work:@match http://example.com/page.php*
-
Franklin Yu over 7 yearsIn latest version,
@match
does match the entire URI including query string. For example,@match https://www.example.com
will not matchhttps://www.example.com?foo=bar
. -
Pysis over 6 yearsMultiple match statements in my TamperMonkey script may work for the first site listed, but not the others. I went to using
include
with a regex, making sure not to forget the delimiters, and it seems I cannot use my custom usual delimiters instead. -
blizzrdof77 over 6 yearsThe second example (using
@match
) worked for me! Just appending the*
to the URL path works if it is placed before any query strings. -
Kamafeather almost 5 yearsExample match any Google subdomain on http&https ...
@include /^https?\:\/\/.*.google\..*\/.*$/
– Might help other dudes getting mad in trying to make it work like I did (also, don't forget to check "✅Enabled"). -
Brock Adams almost 5 years@Kamafeather, the
s?
is a good update; thanks. The\/
s are superfluous. Which enabled check did you mean? -
Kamafeather almost 5 years@BrockAdams I mean the first menu item when clicking on the Chrome extension icon; the first issue I had was that I needed to enable the global scripts execution from there; just a hint that might help some 😉. About the slashes, yes, I thought so, but often I put them anyway to spare me eventual headaches (regex are quite sensitive to [my] mistakes, so I write them defensively 🙃)
-
Mr_Dave almost 2 yearsI believe the URL for this is now developer.chrome.com/docs/extensions/mv3/match_patterns