Match patterns in extension manifests

Match patterns are a way to specify groups of URLs: a match pattern matches a specific set of URLs. They are used in WebExtensions APIs in a few places, most notably to specify which documents to load content scripts into, and to specify which URLs to add webRequest listeners to.

APIs that use match patterns usually accept a list of match patterns, and will perform the appropriate action if the URL matches any of the patterns. See, for example, the content_scripts key in manifest.json.

Match pattern structure

注意: Some browsers don’t support certain schemes. 检查 浏览器兼容性表格 了解细节。

All match patterns are specified as strings. Apart from the special <all_urls> pattern, match patterns consist of three parts: scheme , host ,和 path . The scheme and host are separated by :// .

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

scheme

scheme component may take one of two forms:

表单 Matches
* Only "http" and "https" and in some browsers also "ws" and "wss" .
One of http , https , ws , wss , ftp , data , file ,或 (chrome-)extension . Only the given scheme.

host

host component may take one of three forms:

表单 Matches
* Any host.
*. followed by part of the hostname. The given host and any of its subdomains.
A complete hostname, without wildcards. Only the given host.

host must not include a port number.

host is optional only if the scheme is "file".

Note that the wildcard may only appear at the start.

path

path component must begin with a / .

After that, it may subsequently contain any combination of the * wildcard and any of the characters that are allowed in URL paths or query strings. Unlike host path component may contain the * wildcard in the middle or at the end, and the * wildcard may appear more than once.

The value for the path matches against the string which is the URL path plus the URL query string . This includes the ? between the two, if the query string is present in the URL. For example, if you want to match URLs on any domain where the URL path ends with foo.bar , then you need to use an array of Match Patterns like ['*://*/*foo.bar', '*://*/*foo.bar?*'] ?* is needed, rather than just bar* , in order to anchor the ending * as applying to the URL query string and not some portion of the URL path.

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

注意: The path pattern string should not include a port number. Adding a port, as in: http://localhost:1234/* causes the match pattern to be ignored. However, http://localhost:1234 will match with http://localhost/* .

<all_urls>

The special value <all_urls> matches all URLs under any of the supported schemes: that is "http", "https", "ws", "wss", "ftp", "data", and "file".

范例

模式 Example matches Example non-matches
<all_urls>

Match all URLs.

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/
(unsupported scheme)

ftps://files.somewhere.org/
(unsupported scheme)

*://*/*

Match all HTTP, HTTPS and WebSocket URLs.

http://example.org/

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

ws://sockets.somewhere.org/

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

ftp://ftp.example.org/
(unmatched scheme)

file:///a/
(unmatched scheme)

*://*.mozilla.org/*

Match all HTTP, HTTPS and WebSocket URLs that are hosted at "mozilla.org" or one of its subdomains.

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/
(unmatched scheme)

http://mozilla.com/
(unmatched host)

http://firefox.org/
(unmatched host)

*://mozilla.org/

Match all HTTP, HTTPS and WebSocket URLs that are hosted at exactly "mozilla.org/".

http://mozilla.org/

https://mozilla.org/

ws://mozilla.org/

wss://mozilla.org/

ftp://mozilla.org/
(unmatched scheme)

http://a.mozilla.org/
(unmatched host)

http://mozilla.org/a
(unmatched path)

ftp://mozilla.org/

Match only "ftp://mozilla.org/".

ftp://mozilla.org http://mozilla.org/
(unmatched scheme)

ftp://sub.mozilla.org/
(unmatched host)

ftp://mozilla.org/path
(unmatched path)

https://*/path

Match HTTPS URLs on any host, whose path is "path".

https://mozilla.org/path

https://a.mozilla.org/path

https://something.com/path

http://mozilla.org/path
(unmatched scheme)

https://mozilla.org/path/
(unmatched path)

https://mozilla.org/a
(unmatched path)

https://mozilla.org/
(unmatched path)

https://mozilla.org/path?foo=1
(unmatched path due to URL query string)

https://*/path/

Match HTTPS URLs on any host, whose path is "path/" and which has no URL query string.

https://mozilla.org/path/

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

https://something.com/path /

http://mozilla.org/path/
(unmatched scheme)

https://mozilla.org/path
(unmatched path)

https://mozilla.org/a
(unmatched path)

https://mozilla.org/
(unmatched path)

https://mozilla.org/path/ ?foo=1
(unmatched path due to URL query string)

https://mozilla.org/*

Match HTTPS URLs only at "mozilla.org", with any URL path and URL query string.

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
(unmatched scheme)

https://mozilla.com/path
(unmatched host)

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

Match only this URL, or this URL with any URL fragment.

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

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

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

Match HTTPS URLs hosted on "mozilla.org", whose path contains a component "b" somewhere in the middle. Will match URLs with query strings, if the string ends in a / .

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/*/
(unmatched path)

https://mozilla.org/a/b/
(unmatched path)

https://mozilla.org/a/b/c/d/?foo=bar
(unmatched path due to URL query string)

file:///blah/*

Match any FILE URL whose path begins with "blah".

file:///blah/

file:///blah/bleh

file:///bleh/
(unmatched path)

Invalid match patterns

Invalid pattern Reason
resource://path/ Unsupported scheme.
https://mozilla.org No path.
https://mozilla.*.org/ "*" in host must be at the start.
https://*zilla.org/ "*" in host must be the only character or be followed by ".".
http*://mozilla.org/ "*" in scheme must be the only character.
https://mozilla.org:80/ Host must not include a port number.
*://* Empty path: this should be " *://*/* ".
file://* Empty path: this should be " file:///* ".

浏览器兼容性

scheme

BCD tables only load in the browser

Found a problem with this page?

最后修改: , 由 MDN 贡献者

  1. 浏览器扩展名
  2. 快速入门
    1. What are extensions?
    2. Your first extension
    3. Your second extension
    4. Anatomy of an extension
    5. Example extensions
    6. What next?
  3. 概念
    1. Using the JavaScript APIs
    2. Content scripts
    3. Match patterns
    4. Working with files
    5. 国际化
    6. Content Security Policy
    7. Native messaging
    8. Differences between API implementations
    9. Chrome incompatibilities
  4. 用户界面
    1. 用户界面
    2. Toolbar button
    3. Address bar button
    4. Sidebars
    5. Context menu items
    6. Options page
    7. Extension pages
    8. Notifications
    9. Address bar suggestions
    10. Developer tools panels
  5. 如何
    1. Intercept HTTP requests
    2. Modify a web page
    3. Insert external content
    4. Share objects with page scripts
    5. Add a button to the toolbar
    6. Implement a settings page
    7. Work with the Tabs API
    8. Work with the Bookmarks API
    9. Work with the Cookies API
    10. Work with contextual identities
    11. Interact with the clipboard
    12. Build a cross-browser extension
  6. Firefox differentiators
  7. JavaScript API
    1. Browser support for JavaScript APIs
    2. alarms
    3. bookmarks
    4. browserAction
    5. browserSettings
    6. browsingData
    7. captivePortal
    8. clipboard
    9. 命令
    10. contentScripts
    11. contextualIdentities
    12. Cookie
    13. devtools
    14. dns
    15. downloads
    16. events
    17. extension
    18. extensionTypes
    19. find
    20. history
    21. i18n
    22. identity
    23. idle
    24. management
    25. menus
    26. notifications
    27. omnibox
    28. pageAction
    29. permissions
    30. pkcs11
    31. privacy
    32. proxy
    33. runtime
    34. search
    35. sessions
    36. sidebarAction
    37. storage
    38. tabs
    39. theme
    40. topSites
    41. 类型
    42. userScripts
    43. webNavigation
    44. webRequest
    45. windows
  8. Manifest keys
    1. 介绍
    1. 作者
    2. background
    3. browser_action
    4. browser_specific_settings
    5. chrome_settings_overrides
    6. chrome_url_overrides
    7. 命令
    8. content_scripts
    9. content_security_policy
    10. default_locale
    11. description
    12. developer
    13. devtools_page
    14. dictionaries
    15. externally_connectable
    16. homepage_url
    17. icons
    18. incognito
    19. manifest_version
    20. name
    21. offline_enabled
    22. omnibox
    23. optional_permissions
    24. options_page
    25. options_ui
    26. page_action
    27. permissions
    28. protocol_handlers
    29. short_name
    30. sidebar_action
    31. storage
    32. theme
    33. theme_experiment
    34. user_scripts
    35. version
    36. version_name
    37. web_accessible_resources
  9. Extension Workshop
    1. Develop
    2. Publish
    3. Manage
    4. Enterprise
  10. Contact us
  11. Channels
    1. Add-ons blog
    2. Add-ons forum
    3. Add-ons chat