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 1 yearI'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.phphas thekey1=value1among its parameters.Now I'm using
@match http://example.com/page.php?key1=value1&*But it doesn't match if
page.phphas no other parameters. It also won't match ifkey1is not the first parameter either.Is there any way to match a page according to a parameter?
-
noquierouser almost 9 yearsThis worked, except for a couple things. I used the@matchexample because@includedidn'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 almost 6 yearsIn latest version,@matchdoes match the entire URI including query string. For example,@match https://www.example.comwill not matchhttps://www.example.com?foo=bar. -
Pysis about 5 yearsMultiple match statements in my TamperMonkey script may work for the first site listed, but not the others. I went to usingincludewith a regex, making sure not to forget the delimiters, and it seems I cannot use my custom usual delimiters instead. -
blizzrdof77 almost 5 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 over 3 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 over 3 years@Kamafeather, thes?is a good update; thanks. The\/s are superfluous. Which enabled check did you mean? -
Kamafeather over 3 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 6 monthsI believe the URL for this is now developer.chrome.com/docs/extensions/mv3/match_patterns