主機權限和內容指令碼比對的依據是一組由比對模式定義的網址,比對模式基本上是開頭為允許的配置 (http、https、file 或 ftp) 的網址,且可包含「*」字元。特殊模式 <all_urls> 會比對以允許的架構開頭的任何網址。每個比對模式都包含 3 個部分:
scheme:例如
http、file或*注意:您不會自動取得file網址的存取權。使用者必須前往擴充功能管理頁面,並為每個要求file存取權的擴充功能選擇啟用。主機 - 例如
www.google.com、*.google.com或*;如果通訊協定為file,則沒有主機部分路徑,例如
/*、/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/* | 比對使用 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 | 缺少通訊協定分隔符 (應為「//」,而非「/」) |
foo://* | 無效的配置 |
部分配置無法在所有情境中使用。