主机权限和内容脚本匹配基于由匹配格式定义的一组网址。匹配模式实质上是以允许的架构(http
、https
、file
或 ftp
)开头的网址,可以包含“*
”字符。特殊格式 <all_urls>
与以允许的协议开头的任何网址匹配。每种匹配模式都包含 3 个部分:
架构 - 例如
http
、file
或*
注意:系统不会自动对file
网址进行访问。该用户必须访问扩展程序管理页面,并为提出请求的每个扩展程序选择启用file
访问权限。host - 例如
www.google.com
、*.google.com
或*
;如果架构为file
,则没有 host 部分路径 - 例如
/*
、/foo*
或/foo/bar
。路径必须包含在主机权限中,但始终会被视为/*
。
以下是基本语法:
<url-pattern> := <scheme>://<host><path>
<scheme> := '*' | 'http' | 'https' | 'file' | 'ftp' | 'urn'
<host> := '*' | '*.' <any char except '/' and '*'>+
<path> := '/' <any chars>
“*
”的含义取决于它位于“协议”部分、“主机”还是“路径”部分。如果架构为 *
,则它匹配 http
或 https
,而不匹配 file
、ftp
或 urn
。如果主机只是 *
,则它匹配任何主机。如果主机为 *._hostname_
,则它匹配指定主机或其任何子网域。在 path 部分中,每个“*
”匹配 0 个或多个字符。下表显示了一些有效的格式。
模式 | 作用 | 匹配网址示例 |
---|---|---|
http://*/* | 匹配使用 http 架构的所有网址 | http://www.google.com/ http://example.org/foo/bar.html |
http://*/foo* | 匹配任何主机上使用 http 架构的网址,只要路径以 /foo 开头即可 | http://example.com/foo/bar.html http://www.google.com/foo |
https://*.google.com/foo*bar | 匹配使用 https 架构且位于 google.com 主机(如 www.google.com、docs.google.com 或 google.com)上的任何网址,只要路径以 /foo 开头并以 bar 结尾即可 | https://www.google.com/foo/baz/bar https://docs.google.com/foobar |
http://example.org/foo/bar.html | 与指定的网址匹配 | http://example.org/foo/bar.html |
file:///foo* | 匹配路径以 /foo 开头的所有本地文件 | file:///foo/bar.html file:///foo |
http://127.0.0.1/* | 匹配主机 127.0.0.1 上使用 http 架构的任何网址 | http://127.0.0.1/ http://127.0.0.1/foo/bar.html |
*://mail.google.com/* | 匹配以 http://mail.google.com 或 https://mail.google.com 开头的所有网址。 | http://mail.google.com/foo/baz/bar https://mail.google.com/foobar |
urn:* | 匹配以 urn: 开头的所有网址。 | urn:uuid:54723bea-c94e-480e-80c8-a69846c3f582 urn:uuid:cfa40aff-07df-45b2-9f95-e023bcf4a6da |
<all_urls> | 匹配使用允许的架构的所有网址。(请参阅本节开头部分,查看允许的协议列表。) | http://example.org/foo/bar.html file:///bar/baz.html |
以下是无效模式匹配的一些示例:
图案无效 | 差评原因 |
---|---|
http://www.google.com | 无路径 |
http://*foo/bar | host 中的“*”后面只能跟“.”或“/” |
http://foo.*.bar/baz | 如果 host 中包含“*”,它必须是第一个字符 |
http:/bar | 缺少架构分隔符(“/”应为“//”) |
foo://* | scheme 无效 |
有些架构并非在所有上下文中都受支持。