Include and exclude rules: Difference between revisions
→Greaseable schemes: Added file: scheme and →Notes |
m Reverted edits by Marcasmar5 (talk) to last revision by Arantius Tags: content model change Rollback |
||
(9 intermediate revisions by 5 users not shown) | |||
Line 3: | Line 3: | ||
'''The script will execute if it matches any include rule, as long as it does not match an exclude rule.''' | '''The script will execute if it matches any include rule, as long as it does not match an exclude rule.''' | ||
The rules | The [[Metadata_Block#@match|<code>@match</code>]] rule is similar to and possibly preferable over <code>@include</code>. | ||
== Globs == | |||
Include and exclude rules support the <code>*</code> or [[wikipedia:Glob (programming)|globbing operator]]. | |||
The <code>*</code> serves as a wildcard that matches one or more of any character. | |||
A rule can have several wildcards or none, in which case the rule must match the entire URL exactly. | |||
Exclude rules look the same, and prevent the script from being executed. | |||
For example: <code><nowiki>http://www.example.com/foo/*</nowiki></code> will match: | |||
* <code><nowiki>http://www.example.com/foo/bar</nowiki></code> and, | * <code><nowiki>http://www.example.com/foo/bar</nowiki></code> and, | ||
Line 12: | Line 21: | ||
* <code><nowiki>http://www.example.com/baz/</nowiki></code>. | * <code><nowiki>http://www.example.com/baz/</nowiki></code>. | ||
Further examples: | |||
<pre class="sample"> | <pre class="sample"> | ||
Line 29: | Line 32: | ||
If no include rule is provided, <code>@include *</code> is assumed. | If no include rule is provided, <code>@include *</code> is assumed. | ||
That is: every URL will be matched, within the allowed [[#Greaseable_schemes |Greaseable schemes]]. | That is: every URL will be matched, within the allowed [[#Greaseable_schemes|Greaseable schemes]]. | ||
== Regular Expressions == | == Regular Expressions == | ||
Support for full regular expressions in include and exclude rules is also available. | |||
If the rule both starts and ends with a forward-slash (<code>/</code>) character, the contents inside those slashes are interpreted as | If the rule both starts and ends with a forward-slash (<code>/</code>) character, the contents inside those slashes are interpreted as a regular expression. | ||
For example: | For example: | ||
Line 51: | Line 54: | ||
== Greaseable schemes == | == Greaseable schemes == | ||
Greasemonkey will run scripts only on documents loaded from particular [http:// | Greasemonkey will run scripts only on documents loaded from particular [http://tools.ietf.org/html/rfc3986#section-3.1 schemes]. | ||
By default, those are: | By default, those are: | ||
* http | * http | ||
* https | * https | ||
* | * about:blank | ||
User scripts will not run on documents from any other scheme (ftp, file, etc.) or any other part of about. | |||
<!-- Not in 4.0 | |||
== Magic TLD == | == Magic TLD == | ||
The only special syntax besides the wildcard is .tld. An include such as <code><nowiki>http://www.example.tld/*</nowiki></code> will match any top level domain, such as <code>www.example.com</code>, <code>www.example.org</code>, <code>www.example.co.uk</code>, and so on. One must be careful with this, to not accidentally leak data to a site that they did not mean to match. This list of TLDs includes myriad dual-segment TLDs (such as ca.us, aeroport.fr and kyoto.jp), beside the plain country or category codes (com, jp, se). For a full list see the [[Magic TLD]] page. | The only special syntax besides the wildcard is .tld. An include such as <code><nowiki>http://www.example.tld/*</nowiki></code> will match any top level domain, such as <code>www.example.com</code>, <code>www.example.org</code>, <code>www.example.co.uk</code>, and so on. One must be careful with this, to not accidentally leak data to a site that they did not mean to match. This list of TLDs includes myriad dual-segment TLDs (such as ca.us, aeroport.fr and kyoto.jp), beside the plain country or category codes (com, jp, se). For a full list see the [[Magic TLD]] page. | ||
--> | |||
<!-- Not in 4.0 | |||
== User Specified Rules == | == User Specified Rules == | ||
Line 111: | Line 80: | ||
If a script include matches, but a user exclude also matches, the user exclude will take precedence over the script, and it will not run. | If a script include matches, but a user exclude also matches, the user exclude will take precedence over the script, and it will not run. | ||
If a script exclude matches, but a user include also matches, the user include will take precedence over the script, and it will run. | If a script exclude matches, but a user include also matches, the user include will take precedence over the script, and it will run. | ||
--> |
Latest revision as of 21:30, 4 January 2024
User scripts specify include and exclude rules in the metadata block.
The script will execute if it matches any include rule, as long as it does not match an exclude rule.
The @match
rule is similar to and possibly preferable over @include
.
Globs
Include and exclude rules support the *
or globbing operator.
The *
serves as a wildcard that matches one or more of any character.
A rule can have several wildcards or none, in which case the rule must match the entire URL exactly.
Exclude rules look the same, and prevent the script from being executed.
For example: http://www.example.com/foo/*
will match:
http://www.example.com/foo/bar
and,http://www.example.com/foo/
but not:
http://www.example.com/baz/
.
Further examples:
// ==UserScript== // @include http://www.example.com/foo/* // @include http://www.example.org/*.bar // @exclude http://www.example.com/foo/baz // ==/UserScript==
If no include rule is provided, @include *
is assumed.
That is: every URL will be matched, within the allowed Greaseable schemes.
Regular Expressions
Support for full regular expressions in include and exclude rules is also available.
If the rule both starts and ends with a forward-slash (/
) character, the contents inside those slashes are interpreted as a regular expression.
For example:
// ==UserScript== // @include /^https?://www\.example\.com/.*$/ // @include /^http://www\.example\.(org|net)// // ==/UserScript==
Note:
- The rule is parsed as a standard javascript
new RegExp()
, so you do not need to escape forward slashes inside the rule. Special regex characters (like.
) should still be escaped, as in the above examples; otherwise they have their normal regex meaning (like.
matching any non-newline character). - The rule is always treated as case insensitive.
- Anchors (
^
,$
) are not supplied for you. If desired, they should be used as in the above example.
Greaseable schemes
Greasemonkey will run scripts only on documents loaded from particular schemes. By default, those are:
- http
- https
- about:blank
User scripts will not run on documents from any other scheme (ftp, file, etc.) or any other part of about.