Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Greasemonkey/ Tampermonkey @match for a page with parameters

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?

like image 889
noquierouser Avatar asked Dec 09 '13 03:12

noquierouser


2 Answers

@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.
}
like image 153
Brock Adams Avatar answered Nov 09 '22 03:11

Brock Adams


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

like image 38
Rob M. Avatar answered Nov 09 '22 02:11

Rob M.