Greasemonkey / Tampermonkey @match用于包含参数的页面

时间:2013-12-09 03:19:25

标签: javascript regex greasemonkey tampermonkey

我正在处理必须在某个页面中执行的脚本,具体取决于它具有的参数。网址如下:

http://example.com/page.php?key1=value1&key2=value2&...

page.php的参数中包含key1=value1时,我需要匹配它。

现在我正在使用

@match http://example.com/page.php?key1=value1&*

但如果page.php没有其他参数,则不匹配。如果key1不是第一个参数,它也将不匹配。

有没有办法根据参数匹配页面?

2 个答案:

答案 0 :(得分:34)

@match only works on the protocol/scheme, host, and pathname of a URL.

要触发查询参数,您可以使用@include或使用@match并自行测试网址。
同时,@match方法执行得更快。虽然没有经过一段时间的测试 使用@include,您可以使用正则表达式语法。

另见Include and exclude rules

在这种情况下,请使用:

...
// @include  /^http://example\.com/page\.php*key1=value1*/
// ==/UserScript==


或者:

...
// @match http://example.com/page.php*
// ==/UserScript==

if (/\bkey1=value1\b/.test (location.search) ) {
    // DO YOUR STUFF HERE.
}

答案 1 :(得分:2)

根据@match的文档,看起来查询字符串参数不是Greasemonkey引擎匹配的东西:

https://developer.chrome.com/extensions/match_patterns.html