主機權限和內容指令碼比對是以比對模式定義的一組網址為依據。比對模式基本上就是以許可配置 (http
、https
、file
或 ftp
) 開頭,且可包含「*
」字元的網址。特殊模式 <all_urls>
會比對以許可配置為開頭的任何網址。每個比對模式都包含 3 個部分:
scheme,例如
http
、file
或*
注意:系統不會自動存取file
網址。使用者必須造訪擴充功能管理頁面,並為每一項要求該擴充功能的擴充功能選擇啟用file
存取權。host:例如
www.google.com
或*.google.com
或*
;如果配置是file
,則沒有 host 部分path - 例如
/*
、/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
。如果 host 只有 *
,則會比對任何主機。如果 host 為 *._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/* | 比對所有使用 http 配置且位於主機 127.0.0.1 的網址 | 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 | 主機中的「*」後方只能有「.」或「/」 |
http://foo.*.bar/baz | 如果「*」位於 host 中,則必須是第一個字元 |
http:/bar | 缺少 scheme 分隔符 (「/」應為「//」) |
foo://* | 配置無效 |
而且有些配置可能不適用於某些情況。