Greasemonkey/Tampermonkey @match 用于带参数的页面 [英] 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&...
当 page.php
在其参数中包含 key1=value1
时,我需要匹配它.
And I need to match it when page.php
has the key1=value1
among its parameters.
现在我正在使用
@match http://example.com/page.php?key1=value1&*
但如果 page.php
没有其他参数,则不匹配.如果 key1
也不是第一个参数,它也不会匹配.
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?
推荐答案
@match
仅适用于 URL 的协议/方案、主机和路径名.
@match
only works on the protocol/scheme, host, and pathname of a URL.
要触发查询参数,您可以使用 @include
或使用 @match
并自己测试 URL.
请注意,@match
方法执行得更快.
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.
通过@include
,您可以使用正则表达式语法.另请参阅包括和排除规则.
With @include
, you can use a regex syntax. See, also Include and exclude rules.
在这种情况下,请使用:
...
// @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.
}
这篇关于Greasemonkey/Tampermonkey @match 用于带参数的页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!