Include and exclude rules: Difference between revisions

From GreaseSpot Wiki
Jump to navigationJump to search
(Undo revision 7239 by LouCypher (talk) // Oops! Didn't see it)
(→‎Greaseable schemes: Notes for 'data' scheme)
Line 57: Line 57:
* https
* https
* ftp
* ftp
* data
* data (see Notes)


(<b>Note:</b>  What is officially called a 'scheme' in a URL is also found in Javascript as the <code>.protocol</code> property of any abstract <code>link</code> element such as <code>&lt;a&gt;</code>, <code>&lt;link&gt;</code>, or a DOM object such as <code>document.location</code>.)
=== Notes ===
* What is officially called a 'scheme' in a URL is also found in Javascript as the <code>.protocol</code> property of any abstract <code>link</code> element such as <code>&lt;a&gt;</code>, <code>&lt;link&gt;</code>, or a DOM object such as <code>document.location</code>.
* Supports for 'data' scheme has been removed since version 1.11 due to security issue ({{GitTicket|1767}}).


== Extra schemes ==
== Extra schemes ==

Revision as of 14:51, 3 August 2013

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 rules are URLs, which can have a "wildcard" asterisk (*), which matches any string including the empty string. 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/.

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:

// ==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

As of Greasemonkey 0.9.8, support for full regular expressions in include and exclude rules was added. If the rule both starts and ends with a forward-slash (/) character, the contents inside those slashes are interpreted as a 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
  • ftp
  • data (see Notes)

Notes

  • What is officially called a 'scheme' in a URL is also found in Javascript as the .protocol property of any abstract link element such as <a>, <link>, or a DOM object such as document.location.
  • Supports for 'data' scheme has been removed since version 1.11 due to security issue (#1767).

Extra schemes

Greasemonkey will also run scripts on:

file
Only if greasemonkey.fileIsGreaseable is set to true in about:config.
about
Prior to 0.9.8, scripts would only run if greasemonkey.aboutIsGreaseable is set to true in about:config. (But about:blank is always allowed.).
Since 0.9.8, only about:blank is allowed and only when explicitly included. (#1375)

In both cases this restriction is intended to prevent security/privacy vulnerabilities.

Magic TLD

The only special syntax besides the wildcard is .tld. An include such as http://www.example.tld/* will match any top level domain, such as www.example.com, www.example.org, www.example.co.uk, 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.

Data scheme user scripts

Browsers can open windows in which all of the page top content is contained in a data scheme URI. For example, the below URI will display a HTML page that indirectly includes an image from google.com as its sole content:

data:text/html;charset=utf-8,<html><head><title>data: test</title></head><body><img src='http://www.google.com/intl/en_ALL/images/logo.gif'></body></html></pre>

This link points to the above data URI and can be clicked to see it in action.

Firefox ignores unknown semicolon separated parameters in the header of a data URI (and the standards seem to leave this possibility open) which means if one adds say the string MyScript; in the header of the above URI, giving:

data:text/html;MyScript;charset=utf-8,<html><head><title>data: test</title>....

one can then use Include and exclude rules such as

@include data:text/html;MyScript;*

to trigger user scripts to run on a subtype of data URIs.

This ability can be useful if a user script creates one or more data URIs and then opens them. Augmenting the URIs with some extra marking can cause specific user scripts to run in their windows. For example, a user script can create a data URI that contains a HTML table and trigger a user script for it that allows the user sort it.

User Specified Rules

Since Greasemonkey 0.9.9, users have been able to specify their own exclude and include values through the script options dialog in the Add Ons Manager. Thus, each script has its own rules plus optionally the user's rules.

The user's rules are checked first, then the script's rules are checked. If any exclude matches the page, the script does not run. If any include matches the page, the script will 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.