ContentScripts需要使用Match Pattern来确定对哪些页面起作用。
一个合法的match pattern可能是all_urls(代表所有url),或者是一个带有*号的url。这个url的生成规则如下:
- <url-pattern> := <scheme>://<host><path>
- <scheme> := '*' | 'http' | 'https'
- <host> := '*' | '*.' <any char except '/' and '*'>+
- <path> := '/' <any chars>
需要注意的地方是scheme中只能是或者http、https,而不能是tps这类的,而且在这里只能代替http和https,对ftp或者其他scheme无效。host中也是一样,用到的话,只能是,或者.xxx.com这类的,而不能是test..com这样的。只能出现在host的最前面。
例子下面是一些正确的Pattern和一些错误的,取自chrome的extension文档,针对我们的差异稍作修改。
Pattern | What it does | Examples of matching URLs |
---|---|---|
http:/// | Matches any URL that uses the http scheme | http://www.google.com/http://example.org/foo/bar.html |
http:///foo | Matches any URL that uses the http scheme, on any host, as long as the path starts with /foo | http://example.com/foo/bar.htmlhttp://www.google.com/foo |
https://.google.com/foobar | Matches any URL that uses the https scheme, is on a google.com host (such as www.google.com, docs.google.com, or google.com), as long as the path starts with /foo and ends with bar | http://www.google.com/foo/baz/barhttp://docs.google.com/foobar |
http://example.org/foo/bar.html | row 2, cell 2 | row 2, cell 3 |
http://127.0.0.1/* | row 2, cell 2 | row 2, cell 3 |
://mail.google.com/ | Matches any URL that starts with http://mail.google.com or https://mail.google.com. | http://mail.google.com/foo/baz/barhttps://mail.google.com/foobar |
all_urls | Matches any URL that uses a permitted scheme. (See the beginning of this section for the list of permitted schemes.) | http://example.org/foo/bar.html |
Here are some examples of invalid pattern matches:
Bad pattern | Why it's bad |
---|---|
http://www.google.com | No path |
http://*foo/bar | '' in the host can be followed only by a '.' or '/' |
[http://foo..bar/baz](http://foo.*.bar/baz) | If '' is in the host, it must be the first character |
http:/bar | Missing scheme separator ("/" should be "//") |
foo:// | Invalid scheme |