Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 953899 of Match patterns

  • Revision slug: Mozilla/Add-ons/WebExtensions/Match_patterns
  • Revision title: Match patterns
  • Revision id: 953899
  • Created:
  • Creator: wbamberg
  • Is current revision? No
  • Comment

Revision Content

Match patterns are a way to specify groups of URLs: a match pattern matches a specific set of URLs. They are used by WebExtensions 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

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

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

scheme

The scheme component may take one of two forms:

Form Matches
"*" Only "http" and "https".
One of "http", "https", "file", "ftp", "app". Only the given scheme.

host

The host component may take one of three forms:

Form 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 is optional only if the scheme is "file".

Note that the wildcard may only appear at the start.

path

The 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. Unlike host, the path component may contain the "*" wildcard in the middle or at the end, and the "*" wildcard may appear more than once.

<all_urls>

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

Examples

Pattern Example matches Example non-matches

<all_urls>

Match all URLs.

https://example.org/

ftp://files.somewhere.org/

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

resource://a/b/c/
(unsupported scheme)

*://*.mozilla.org/*

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

https://mozilla.org/

https://mozilla.org/

https://a.mozilla.org/

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

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

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

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

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

*://mozilla.org/

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

https://mozilla.org/

https://mozilla.org/

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

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

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

ftp://mozilla.org/

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

ftp://mozilla.org

https://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

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

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

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

https://mozilla.org/*

Match HTTPS URLs only at "mozilla.org", with any path.

https://mozilla.org/

https://mozilla.org/path

https://mozilla.org/another

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

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

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

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

Match only this URL.

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

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

Match HTTPS URLs hosted on "mozilla.org", whose path contains a component "b" somewhere in the middle.

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

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

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

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

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

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 by the only character or be followed by ".".
http*://mozilla.org/ "*" in scheme must be the only character.

Testing match patterns

When writing WebExtensions you don't generally work with match patterns directly: usually you pass a match pattern string into an API, and the API constructs a match pattern and uses it to test URLs. However, if you're trying to work out which match pattern to use, or debugging a problem with one, it can be useful to be able to create and test match patterns directly. This section explains how to do this.

First, open the developer tool settings and check the setting marked "Enable browser chrome and add-on debugging toolboxes":

{{EmbedYouTube("JDEe2fyFpHE")}}

Next, open the "Browser Console":

{{EmbedYouTube("mfuBMje6dA4")}}

This gives you a command line that you can use to execute privileged JavaScript in Firefox.

Because code running in the Browser Console has system privileges, any time you use it to run code, you need to understand exactly what the code is doing. That includes the code samples in this article.

Now paste this code into the command line and press enter:

Cu.import("resource://gre/modules/MatchPattern.jsm");
Cu.import("resource://gre/modules/BrowserUtils.jsm");

This does two things:

  • imports "MatchPattern.jsm": this is the system module that implements match patterns. Specifically, the module contains a constructor for MatchPattern objects. MatchPattern objects define a function called matches(), that take a URI and returns true or false.
  • defines a new function that converts a string into an nsIURI object: this is the object that matches() expects to receive.

Now you can construct MatchPattern objects, construct URIs, and check whether the URIs match:

var match = new MatchPattern("*://mozilla.org/");

var uri = BrowserUtils.makeURI("https://mozilla.org/");
match.matches(uri); //        < true

uri = BrowserUtils.makeURI("https://mozilla.org/path");
match.matches(uri); //        < false

 

Revision Source

<p>Match patterns are a way to specify groups of URLs: a match pattern matches a specific set of URLs. They are used by WebExtensions in a few places, most notably to specify which documents to load <a href="/en-US/docs/Mozilla/Add-ons/WebExtensions/Content_scripts">content scripts</a> into, and to specify which URLs to add <code><a href="/en-US/docs/Mozilla/Add-ons/WebExtensions/API/webRequest">webRequest</a></code> listeners to.</p>

<p>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 <code><a href="/en-US/docs/Mozilla/Add-ons/WebExtensions/manifest.json/content_scripts">content_scripts</a></code> key in manifest.json.</p>

<h2 id="Match_pattern_structure">Match pattern structure</h2>

<p>All match patterns are specified as strings. Apart from the special <a href="/en-US/Add-ons/WebExtensions/Match_patterns#%3Call_urls%3E">"&lt;all_urls&gt;"</a> pattern, match patterns consist of three parts: <em>scheme</em>, <em>host</em>, and <em>path</em>. The scheme and host are separated by "://".</p>

<pre>
&lt;scheme&gt;://&lt;host&gt;&lt;path&gt;</pre>

<h3 id="scheme">scheme</h3>

<p>The <em>scheme</em> component may take one of two forms:</p>

<table class="fullwidth-table standard-table">
 <thead>
  <tr>
   <th scope="col" style="width: 50%;">Form</th>
   <th scope="col">Matches</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td>"*"</td>
   <td>Only "http" and "https".</td>
  </tr>
  <tr>
   <td>One of "http", "https", "file", "ftp", "app".</td>
   <td>Only the given scheme.</td>
  </tr>
 </tbody>
</table>

<h3 id="host">host</h3>

<p>The <em>host</em> component may take one of three forms:</p>

<table class="fullwidth-table standard-table">
 <thead>
  <tr>
   <th scope="col" style="width: 50%;">Form</th>
   <th scope="col">Matches</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td>"*"</td>
   <td>Any host.</td>
  </tr>
  <tr>
   <td>"*." followed by part of the hostname.</td>
   <td>The given host and any of its subdomains.</td>
  </tr>
  <tr>
   <td>A complete hostname, without wildcards.</td>
   <td>Only the given host.</td>
  </tr>
 </tbody>
</table>

<p><em>host</em> is optional only if the <em>scheme</em> is "file".</p>

<p>Note that the wildcard may only appear at the start.</p>

<h3 id="path">path</h3>

<p>The path component must begin with a "/".</p>

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

<h3 id="&lt;all_urls&gt;">&lt;all_urls&gt;</h3>

<p>The special value "&lt;all_urls&gt;" matches all URLs under any of the supported schemes: that is, "http", "https", "file", "ftp", "app".</p>

<h2 id="Examples">Examples</h2>

<table class="fullwidth-table standard-table">
 <thead>
  <tr>
   <th scope="col" style="width: 33%;">Pattern</th>
   <th scope="col" style="width: 33%;">Example matches</th>
   <th scope="col" style="width: 33%;">Example non-matches</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td>
    <p><code>&lt;all_urls&gt;</code></p>

    <p>Match all URLs.</p>
   </td>
   <td>
    <p><code>https://example.org/</code></p>

    <p><code>ftp://files.somewhere.org/</code></p>

    <p><code>https://a.org/some/path/</code></p>
   </td>
   <td>
    <p><code>resource://a/b/c/</code><br />
     (unsupported scheme)</p>
   </td>
  </tr>
  <tr>
   <td>
    <p><code>*://*.mozilla.org/*</code></p>

    <p>Match all HTTP and HTTPS URLs that are hosted at "mozilla.org" or one of its subdomains.</p>
   </td>
   <td>
    <p><code>https://mozilla.org/</code></p>

    <p><code>https://mozilla.org/</code></p>

    <p><code>https://a.mozilla.org/</code></p>

    <p><code>https://a.b.mozilla.org/</code></p>

    <p><code>https://b.mozilla.org/path/</code></p>
   </td>
   <td>
    <p><code>ftp://mozilla.org/</code><br />
     (unmatched scheme)</p>

    <p><code>https://mozilla.com/</code><br />
     (unmatched host)</p>

    <p><code>https://firefox.org/</code><br />
     (unmatched host)</p>
   </td>
  </tr>
  <tr>
   <td>
    <p><code>*://mozilla.org/</code></p>

    <p>Match all HTTP and HTTPS URLs that are hosted at exactly "mozilla.org/".</p>
   </td>
   <td>
    <p><code>https://mozilla.org/</code></p>

    <p><code>https://mozilla.org/</code></p>
   </td>
   <td>
    <p><code>ftp://mozilla.org/</code><br />
     (unmatched scheme)</p>

    <p><code>https://a.mozilla.org/</code><br />
     (unmatched host)</p>

    <p><code>https://mozilla.org/a</code><br />
     (unmatched path)</p>
   </td>
  </tr>
  <tr>
   <td>
    <p><code>ftp://mozilla.org/</code></p>

    <p>Match only "ftp://mozilla.org/".</p>
   </td>
   <td><code>ftp://mozilla.org</code></td>
   <td>
    <p><code>https://mozilla.org/</code><br />
     (unmatched scheme)</p>

    <p><code>ftp://sub.mozilla.org/</code><br />
     (unmatched host)</p>

    <p><code>ftp://mozilla.org/path</code><br />
     (unmatched path)</p>
   </td>
  </tr>
  <tr>
   <td>
    <p><code>https://*/path</code></p>

    <p>Match HTTPS URLs on any host, whose path is "path".</p>
   </td>
   <td>
    <p><code>https://mozilla.org/path</code></p>

    <p><code>https://a.mozilla.org/path</code></p>

    <p><code>https://something.com/path</code></p>
   </td>
   <td>
    <p><code>https://mozilla.org/path</code><br />
     (unmatched scheme)</p>

    <p><code>https://mozilla.org/a</code><br />
     (unmatched path)</p>

    <p><code>https://mozilla.org/</code><br />
     (unmatched path)</p>
   </td>
  </tr>
  <tr>
   <td>
    <p><code>https://mozilla.org/*</code></p>

    <p>Match HTTPS URLs only at "mozilla.org", with any path.</p>
   </td>
   <td>
    <p><code>https://mozilla.org/</code></p>

    <p><code>https://mozilla.org/path</code></p>

    <p><code>https://mozilla.org/another</code></p>

    <p><code>https://mozilla.org/path/to/doc</code></p>
   </td>
   <td>
    <p><code>https://mozilla.org/path</code><br />
     (unmatched scheme)</p>

    <p><code>https://mozilla.com/path</code><br />
     (unmatched host)</p>
   </td>
  </tr>
  <tr>
   <td>
    <p><code>https://mozilla.org/a/b/c/</code></p>

    <p>Match only this URL.</p>
   </td>
   <td><code>https://mozilla.org/a/b/c/</code></td>
   <td>Anything else.</td>
  </tr>
  <tr>
   <td>
    <p><code>https://mozilla.org/*/b/*/</code></p>

    <p>Match HTTPS URLs hosted on "mozilla.org", whose path contains a component "b" somewhere in the middle.</p>
   </td>
   <td>
    <p><code>https://mozilla.org/a/b/c/</code></p>

    <p><code>https://mozilla.org/d/b/f/</code></p>

    <p><code>https://mozilla.org/a/b/c/d/</code></p>
   </td>
   <td>
    <p><code>https://mozilla.org/b/*/</code><br />
     (unmatched path)</p>

    <p><code>https://mozilla.org/a/b/</code><br />
     (unmatched path)</p>
   </td>
  </tr>
  <tr>
   <td>
    <p><code>file:///blah/*</code></p>

    <p>Match any FILE URL whose path begins with "blah".</p>
   </td>
   <td>
    <p><code>file:///blah/</code></p>

    <p><code>file://blah/bleh</code></p>
   </td>
   <td><code>file:///bleh/</code><br />
    (unmatched path)</td>
  </tr>
 </tbody>
</table>

<h3 id="Invalid_match_patterns">Invalid match patterns</h3>

<table class="fullwidth-table standard-table">
 <thead>
  <tr>
   <th scope="col">Invalid pattern</th>
   <th scope="col">Reason</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td><code>resource://path/</code></td>
   <td>Unsupported scheme.</td>
  </tr>
  <tr>
   <td><code>https://mozilla.org</code></td>
   <td>No path.</td>
  </tr>
  <tr>
   <td><code>https://mozilla.*.org/</code></td>
   <td>"*" in host must be at the start.</td>
  </tr>
  <tr>
   <td><code>https://*zilla.org/</code></td>
   <td>"*" in host must by the only character or be followed by ".".</td>
  </tr>
  <tr>
   <td><code>http*://mozilla.org/</code></td>
   <td>"*" in scheme must be the only character.</td>
  </tr>
 </tbody>
</table>

<h2 id="Testing_match_patterns">Testing match patterns</h2>

<p>When writing WebExtensions you don't generally work with match patterns directly: usually you pass a match pattern string into an API, and the API constructs a match pattern and uses it to test URLs. However, if you're trying to work out which match pattern to use, or debugging a problem with one, it can be useful to be able to create and test match patterns directly. This section explains how to do this.</p>

<p>First, open the developer tool settings and check the setting marked "Enable browser chrome and add-on debugging toolboxes":</p>

<p>{{EmbedYouTube("JDEe2fyFpHE")}}</p>

<p>Next, open the "Browser Console":</p>

<p>{{EmbedYouTube("mfuBMje6dA4")}}</p>

<p>This gives you a command line that you can use to execute privileged JavaScript in Firefox.</p>

<div class="warning">
<p>Because code running in the Browser Console has system privileges, any time you use it to run code, you need to understand exactly what the code is doing. That includes the code samples in this article.</p>
</div>

<p>Now paste this code into the command line and press <kbd>enter</kbd>:</p>

<pre class="brush: js">
Cu.import("resource://gre/modules/MatchPattern.jsm");
Cu.import("resource://gre/modules/BrowserUtils.jsm");</pre>

<p>This does two things:</p>

<ul>
 <li>imports "MatchPattern.jsm": this is the system module that implements match patterns. Specifically, the module contains a constructor for <code>MatchPattern</code> objects.&nbsp;<code>MatchPattern</code> objects define a function called <code>matches()</code>, that take a URI and returns <code>true</code> or <code>false</code>.</li>
 <li>defines a new function that converts a string into an <code><a href="/en-US/docs/Mozilla/Tech/XPCOM/Reference/Interface/nsIURI">nsIURI</a></code> object: this is the object that <code>matches()</code> expects to receive.</li>
</ul>

<p>Now you can construct <code>MatchPattern</code> objects, construct URIs, and check whether the URIs match:</p>

<pre class="brush: js">
var match = new MatchPattern("*://mozilla.org/");

var uri = BrowserUtils.makeURI("https://mozilla.org/");
match.matches(uri); //        &lt; true

uri = BrowserUtils.makeURI("https://mozilla.org/path");
match.matches(uri); //        &lt; false</pre>

<p>&nbsp;</p>
Revert to this revision