Match patterns
主机权限和内容脚本匹配基于一组由匹配模式定义的 URL。匹配模式本质上是以允许的方案开头的 URL(http
、https
、file
或 ftp
,并且可以包含 '*
' 字符。特殊模式 <all_urls>
匹配任何以允许方案开头的 URL
。每个匹配模式有 3 部分:
scheme—例如,
http
orfile
or*
访问
file
URL 不是自动的。用户必须访问扩展管理页面并选择对请求它的每个扩展进行文件访问。host—例如,
www.google.com
或*.google.com
或*
;如果scheme是file
,则没有主机部分path—例如,
/*
,/foo*
, or/foo/bar
.该路径必须存在于主机权限中,但始终被视为/*
。
这是基本语法:
<url-pattern> := <scheme>://<host><path>
<scheme> := '*' | 'http' | 'https' | 'file' | 'ftp' | 'urn'
<host> := '*' | '*.' <any char except '/' and '*'>+
<path> := '/' <any chars>
'*'
的含义取决于它是在方案(scheme)、主机(host)还是路径(path)部分。如果方案是*
,则它匹配 http
或 https
,而不匹配 file
、ftp
或 urn
。如果主机只是 *
,那么它匹配任何主机。如果主机是 *._hostname_
,则它匹配指定的主机或其任何子域。在路径部分,每个'*'
匹配 0 个或多个字符。下表显示了一些有效的模式。
注意:urn
方案自 Chrome 91 起可用。
Pattern | What it does | Examples of matching URLs |
---|---|---|
https://*/* |
匹配任何使用 https 方案的 URL | https://www.google.com/ https://example.org/foo/bar.html |
https://*/foo* |
匹配任何使用 https 方案的 URL,在任何主机上,只要路径以 /foo 开头 | https://example.com/foo/bar.html https://www.google.com/foo |
https://*.google.com/foo*bar |
匹配任何使用 https 方案、位于 google.com 主机(例如 www.google.com、docs.google.com 或 google.com)上的 URL,只要路径以 /foo 开头并以 bar 结尾 | https://www.google.com/foo/baz/bar https://docs.google.com/foobar |
https://example.org/foo/bar.html |
匹配指定的 URL | https://example.org/foo/bar.html |
file:///foo* |
匹配路径以 /foo 开头的任何本地文件 | file:///foo/bar.html file:///foo |
http://127.0.0.1/* |
匹配任何使用 http 方案且位于主机 127.0.0.1 上的 URL | http://127.0.0.1/ http://127.0.0.1/foo/bar.html |
*://mail.google.com/* |
匹配任何以http://mail.google.com or https://mail.google.com . |
http://mail.google.com/foo/baz/bar https://mail.google.com/foobar |
urn:* |
匹配任何以 urn: 开头的 URL。 | urn:uuid:54723bea-c94e-480e-80c8-a69846c3f582 urn:uuid:cfa40aff-07df-45b2-9f95-e023bcf4a6da |
<all_urls> |
匹配任何使用允许方案的 URL。 (有关允许的方案列表,请参阅本节开头。) | http://example.org/foo/bar.html file:///bar/baz.html |
以下是无效模式匹配的一些示例:
Bad pattern | 为什么不好 |
---|---|
https://www.google.com |
没有路径 |
https://*foo/bar |
主机中的“*”后只能跟一个“.”或者 '/' |
https://foo.*.bar/baz |
如果'*'在主机中,它必须是第一个字符 |
http:/bar |
缺少scheme分隔符(“/”应为“//”) |
foo://* |
无效方案 |
并非在所有上下文中都支持某些方案。
By.一粒技术服务