What Does the @match Directive Do in Userscripts?

The @match directive in userscripts specifies the exact web pages where a script is permitted to execute. By utilizing standardized url match patterns, it gives developers precise control over domain, protocol, and path targeting, ensuring that custom scripts only run on intended websites rather than globally across the web.

How the @match Directive Works

When you install a userscript in browser extensions like Tampermonkey, Violentmonkey, or Greasemonkey, the extension inspects the script’s metadata block before loading it. The @match rule acts as a filter. If the URL of the current browser tab matches the pattern defined in the directive, the userscript manager injects and executes your code. If the URL does not match, the script remains inactive.

A standard @match rule follows a strict format consisting of three distinct parts:

// @match <scheme>://<host><path>

Common Examples of @match Patterns

Developers use various pattern combinations depending on how broadly or narrowly they want their script to apply:

Multiple @match lines can be added to the metadata block if a single script needs to target several distinct domain patterns.

@match vs. @include

While both directives define target URLs, modern userscript development heavily favors @match over the older @include directive: