60

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 the key1=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 if key1 is not the first parameter either.

Is there any way to match a page according to a parameter?

zcoop98
  • 2,590
  • 1
  • 18
  • 31
noquierouser
  • 963
  • 2
  • 11
  • 25

2 Answers2

86

@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.
}
Pieter De Bie
  • 1,074
  • 13
  • 30
Brock Adams
  • 90,639
  • 22
  • 233
  • 295
  • 1
    This 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*` – noquierouser Dec 09 '13 at 16:13
  • 1
    In latest version, `@match` does match the entire URI including query string. For example, `@match https://www.example.com` will not match `https://www.example.com?foo=bar`. – Franklin Yu Dec 07 '16 at 04:57
  • Multiple 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. – Pysis Sep 27 '17 at 23:55
  • 2
    The second example (using `@match`) worked for me! Just appending the `*` to the URL path works if it is placed before any query strings. – blizzrdof77 Jan 17 '18 at 22:10
  • 1
    **Example** 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"). – Kamafeather Jul 11 '19 at 17:30
  • @Kamafeather, the `s?` is a good update; thanks. The `\/`s are superfluous. Which enabled check did you mean? – Brock Adams Jul 11 '19 at 17:54
  • @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 ) – Kamafeather Jul 14 '19 at 14:02
8

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

Rob M.
  • 35,491
  • 6
  • 51
  • 50
  • 3
    I believe the URL for this is now https://developer.chrome.com/docs/extensions/mv3/match_patterns/ – Mr_Dave May 20 '22 at 19:39