In userscripts (such as Tampermonkey or Greasemonkey scripts), @include and @match are key directives within the metadata block used to specify on which web pages the script should execute. While their core functionalities are similar, their matching patterns and precision levels differ.
@include
The @include directive allows the use of wildcards to define pages where the script should run. This approach offers greater flexibility but may inadvertently cause the script to execute on unintended pages due to overly broad matching.
Example:
javascript// ==UserScript== // @name Example Script // @include http://*example.com/* // ==/UserScript==
In this example, the script executes on all pages accessing the example.com domain via the http protocol, regardless of the path.
@match
Compared to @include, @match provides more precise URL matching patterns. It does not support wildcards but utilizes specific URL pattern matching syntax to accurately define pages where the script should run.
Example:
javascript// ==UserScript== // @name Example Script // @match http://*example.com/* // ==/UserScript==
Here, @match similarly instructs the script to run on pages accessing example.com, but it employs a standardized pattern matching approach that is easier to control and avoids unintended matches.
Summary
Overall, @include offers greater flexibility and is suitable for scenarios requiring broad matching, while @match provides higher precision and standardization, ideal for environments demanding precise control over script execution. The choice depends on specific application requirements and the level of matching control needed. In practice, developers often combine both directives to leverage their respective advantages.