匹配模式

匹配模式是一种指定 URL 组的方法:匹配模式可以匹配一组特定的 URL。它们在 WebExtensions API 的几个地方使用,最值得注意的是指定要将内容脚本加载到哪些文档中,以及指定要添加webRequest 监听器的哪些 URL。

使用匹配模式的 API 通常接受一个匹配模式列表,如果 URL 匹配任何模式,它们将执行相应的操作。例如,请参阅 manifest.json 中的content_scripts 键。

匹配模式结构

注意:某些浏览器不支持某些方案。请查看浏览器兼容性表格了解详情。

所有匹配模式都指定为字符串。除了特殊的<all_urls> 模式外,匹配模式由三个部分组成:方案主机路径。方案和主机之间用 :// 分隔。

<scheme>://<host><path>

scheme

方案组件可以采取以下两种形式之一

表单 匹配
* 仅支持 "http" 和 "https",在某些浏览器中还支持"ws" 和 "wss"
httphttpswswssftpdatafile(chrome-)extension 中的一个。 仅匹配给定的方案。

主机

主机组件可以采取以下三种形式之一

表单 匹配
* 任何主机。
*. 后面跟着主机名的一部分。 给定的主机及其任何子域。
一个完整的主机名,没有通配符。 仅匹配给定的主机。

主机不得包含端口号。

仅当方案为 "file" 时,主机才是可选的。

请注意,通配符只能出现在开头。

路径

路径组件必须以 / 开头。

在此之后,它可以包含任意组合的 * 通配符以及 URL 路径或查询字符串中允许的任何字符。与主机不同,路径组件可以在中间或末尾包含 * 通配符,并且 * 通配符可以出现多次。

路径的值将与 URL 路径加上URL 查询字符串的字符串进行匹配。如果 URL 中存在查询字符串,这将包括两者之间的 ?。例如,如果您想匹配任何域上 URL 路径以 foo.bar 结尾的 URL,则需要使用匹配模式数组,例如 ["*://*/*foo.bar", "*://*/*foo.bar?*"]。这里需要 ?* 而不是仅 bar*,是为了将结尾的 * 锚定到 URL 查询字符串,而不是 URL 路径的某一部分。

Neither the URL fragment identifier, nor the # which precedes it, are considered as part of the path

注意:路径模式字符串不应包含端口号。添加端口,例如:https://:1234/* 会导致匹配模式被忽略。但是,https://:1234 会与 https:///* 匹配。

<all_urls>

特殊值 <all_urls> 匹配所有受支持方案下的所有 URL:即 "http"、"https"、"ws"、"wss"、"ftp"、"data" 和 "file"。

示例

模式 示例匹配 示例不匹配

<all_urls>

匹配所有 URL。

http://example.org/

https://a.org/some/path/

ws://sockets.somewhere.org/

wss://ws.example.com/stuff/

ftp://files.somewhere.org/

resource://a/b/c/
(不受支持的方案)

ftps://files.somewhere.org/
(不受支持的方案)

*://*/*

匹配所有 HTTP、HTTPS 和 WebSocket URL。

http://example.org/

https://a.org/some/path/

ws://sockets.somewhere.org/

wss://ws.example.com/stuff/

ftp://ftp.example.org/
(不匹配的方案)

file:///a/
(不匹配的方案)

*://*.mozilla.org/*

匹配 "mozilla.org" 或其任何子域上的所有 HTTP、HTTPS 和 WebSocket URL。

http://mozilla.org/

https://mozilla.org/

http://a.mozilla.org/

http://a.b.mozilla.org/

https://b.mozilla.org/path/

ws://ws.mozilla.org/

wss://secure.mozilla.org/something

ftp://mozilla.org/
(不匹配的方案)

http://mozilla.com/
(不匹配的主机)

http://firefox.org/
(不匹配的主机)

*://mozilla.org/

匹配 exactly "mozilla.org/" 上的所有 HTTP、HTTPS 和 WebSocket URL。

http://mozilla.org/

https://mozilla.org/

ws://mozilla.org/

wss://mozilla.org/

ftp://mozilla.org/
(不匹配的方案)

http://a.mozilla.org/
(不匹配的主机)

http://mozilla.org/a
(不匹配的路径)

ftp://mozilla.org/

仅匹配 "ftp://mozilla.org/"。

ftp://mozilla.org

http://mozilla.org/
(不匹配的方案)

ftp://sub.mozilla.org/
(不匹配的主机)

ftp://mozilla.org/path
(不匹配的路径)

https://*/path

匹配任何主机上的 HTTPS URL,其路径为 "path"。

https://mozilla.org/path

https://a.mozilla.org/path

https://something.com/path

http://mozilla.org/path
(不匹配的方案)

https://mozilla.org/path/
(不匹配的路径)

https://mozilla.org/a
(不匹配的路径)

https://mozilla.org/
(不匹配的路径)

https://mozilla.org/path?foo=1
(由于 URL 查询字符串而不匹配路径)

https://*/path/

匹配任何主机上的 HTTPS URL,其路径为 "path/" 且没有 URL 查询字符串。

https://mozilla.org/path/

https://a.mozilla.org/path/

https://something.com/path/

http://mozilla.org/path/
(不匹配的方案)

https://mozilla.org/path
(不匹配的路径)

https://mozilla.org/a
(不匹配的路径)

https://mozilla.org/
(不匹配的路径)

https://mozilla.org/path/?foo=1
(由于 URL 查询字符串而不匹配路径)

https://mozilla.org/*

仅在 "mozilla.org" 匹配 HTTPS URL,具有任何 URL 路径和 URL 查询字符串。

https://mozilla.org/

https://mozilla.org/path

https://mozilla.org/another

https://mozilla.org/path/to/doc

https://mozilla.org/path/to/doc?foo=1

http://mozilla.org/path
(不匹配的方案)

https://mozilla.com/path
(不匹配的主机)

https://mozilla.org/a/b/c/

仅匹配此 URL,或此 URL 带有任何 URL 片段。

https://mozilla.org/a/b/c/

https://mozilla.org/a/b/c/#section1

其他任何内容。

https://mozilla.org/*/b/*/

匹配在 "mozilla.org" 上托管的 HTTPS URL,其路径在中间的某个地方包含组件 "b"。如果字符串以 / 结尾,则会匹配带有查询字符串的 URL。

https://mozilla.org/a/b/c/

https://mozilla.org/d/b/f/

https://mozilla.org/a/b/c/d/

https://mozilla.org/a/b/c/d/#section1

https://mozilla.org/a/b/c/d/?foo=/

https://mozilla.org/a?foo=21314&bar=/b/&extra=c/

https://mozilla.org/b/*/
(不匹配的路径)

https://mozilla.org/a/b/
(不匹配的路径)

https://mozilla.org/a/b/c/d/?foo=bar
(由于 URL 查询字符串而不匹配路径)

file:///blah/*

匹配路径以 "blah" 开头的任何 FILE URL。

file:///blah/

file:///blah/bleh

file:///bleh/
(不匹配的路径)

无效的匹配模式

无效的模式 原因
resource://path/ 不受支持的方案。
https://mozilla.org 无路径。
https://mozilla.*.org/ 主机中的 "*" 必须在开头。
https://*zilla.org/ 主机中的 "*" 必须是唯一字符,或者后面跟着 "."。
http*://mozilla.org/ 方案中的 "*" 必须是唯一字符。
https://mozilla.org:80/ 主机不得包含端口号。
*://* 空路径:这应该是 *://*/*
file://* 空路径:这应该是 file:///*

浏览器兼容性