Metadata Block: Difference between revisions

From GreaseSpot Wiki
Jump to navigationJump to search
m →‎@run-at: formatting
 
(44 intermediate revisions by 13 users not shown)
Line 5: Line 5:
The metadata block appears in JavaScript line comments and may appear anywhere in the top level Greasemonkey code scope of the script, but is usually near the top of the file.
The metadata block appears in JavaScript line comments and may appear anywhere in the top level Greasemonkey code scope of the script, but is usually near the top of the file.


If the metadata block includes a key that Greasemonkey does not understand, it will simply be ignored.
If the metadata block includes a key that Greasemonkey does not understand, it will be ignored.
Some authors choose to include their own keys, and [http://userscripts.org userscripts.org] defines a set of metadata keys for varying purposes.
 
As of [[Version_history#0.9.0|Greasemonkey 0.9.0]], the metadata block is reloaded any time the file changes, just like the rest of the source in the script.
In any older version, one must re-install the script for metadata changes to be recognized.
In 0.9.0+, editing the script is how (e.g.) <code>@include</code> and <code>@exclude</code> rules are customized.
All of the metadata imperatives can be added, removed, or changed, but be careful with the <code>@name</code> and <code>@namespace</code> imperatives: their combination must remain unique among installed scripts, so changing them can cause problems.


== Syntax ==
== Syntax ==
Line 32: Line 26:


Example:
Example:
// @description This script even does the laundry!
<pre class='sample'>// @description This script even does the laundry!</pre>


Just a brief summary of what the script does, presented to the user as the script is installed, and in the manage dialog.
Just a brief summary of what the script does, presented to the user as the script is installed, and in the manage dialog.
''As of Greasemonkey 2.2'': can be localized for multiple languages; see the [[#@name]] documentation for more detail.
<!-- 4.x does not use this
=== @downloadURL ===
The URL to download this script from, when installing updates.
<pre class='sample'>// @downloadURL https://www.example.com/myscript.user.js</pre>
Greasemonkey's default settings require this value to be secure (<tt><nowiki>https://...</nowiki></tt>) for updates to be applied.
If it is not specified, the URL the script was originally installed from will be used.
It is '''unusual''' to specify this value.
Most scripts should omit it.
-->


=== @exclude ===
=== @exclude ===


Examples: see @include
Examples: see [[#@include]]


See [[Include and exclude rules]].
See [[Include and exclude rules]].
There can be any number of @exclude rules in a script.
There can be any number of @exclude rules in a script.
=== @grant ===
See dedicated [[@grant]] page.


=== @icon ===
=== @icon ===
Compatibility: [[Version_history#0.9.0|Greasemonkey 0.9.0+]]


Example:
Example:
<nowiki>// @icon http://www.example.org/icon.png</nowiki>
<pre class='sample'>// @icon http://www.example.org/icon.png</pre>


The icon is, as of Greasemonkey 0.9.0, displayed in the script management interface.
The icon is, as of Greasemonkey 0.9.0, displayed in the script management interface.
Almost any image will work, but a 32x32 pixel size is best.
Almost any image will work, but a 32x32 pixel size is best.
This value ''may'' be specified relative to the URL the script itself is downloaded from.


=== @include ===
=== @include ===


Examples:
Examples:
<nowiki>// @include http://www.example.com/*</nowiki>
<pre class='sample'>// @include http://www.example.com/*
<nowiki>// @include http://*</nowiki>
// @include http://*
<nowiki>// @include *</nowiki>
// @include *</pre>


See [[Include and exclude rules]].
See [[Include and exclude rules]].
There can be any number of @include rules in a script.
There can be any number of @include rules in a script.
=== @match ===
Examples:
<pre class='sample'>// @match https://www.example.com/*
// @match http://*.example.com/*</pre>
The <code>@match</code> metadata imperative is very similar to <code>@include</code>, however it is safer.
It sets more strict rules on what the <code>*</code> character means.
For details, see the documentation on [https://developer.chrome.com/extensions/match_patterns Match Patterns] for Google Chrome.
Chrome implemented <code>@match</code> first, and Greasemonkey has been designed to be compatible.


=== @name ===
=== @name ===


Example:
Example:
// @name Example Script
<pre class='sample'>// @name Example Script</pre>


The name of the script.
The name of the script.
This appears in the [[Greasemonkey_Manual:Manage_Dialog|script manager]] and [[Greasemonkey_Manual:Monkey_Menu|monkey menu]], and is also the unique identifier of a script (within a [[#@namespace|namespace]]).
This appears in the [[Greasemonkey_Manual:Monkey_Menu|monkey menu]], and is also the unique identifier of a script (within a [[#@namespace|namespace]]).
If no name is provided, it will be derived from the file name.
If no name is provided, it will be derived from the file name.
''As of Greasemonkey 2.2'': can be localized for multiple languages, for example:
<pre class='sample'>
// @name      Example Script
// @name:cs    Uživatelské skripty
// @name:es-MX Ejemplo Script
// @name:ru    Пользовательские скрипты
</pre>
Add a colon and the locale code, which is the [http://www.w3.org/WAI/ER/IG/ert/iso639.htm ISO 639] language code and optionally a hyphen and [http://www.iso.org/iso/country_codes/iso_3166_code_lists/english_country_names_and_code_elements.htm ISO 3166] country code, when disambiguation is necessary.
When the user's browser is configured with the matching primary language, that value will be displayed instead.


=== @namespace ===
=== @namespace ===


Example:
Example:
<nowiki>// @namespace http://www.example.com/gmscripts</nowiki>
 
<pre class='sample'>// @namespace http://www.example.com/gmscripts</pre>


The combination of namespace and name is the unique identifier for a Greasemonkey script.
The combination of namespace and name is the unique identifier for a Greasemonkey script.
Line 85: Line 124:
Some authors use the common home page for the collection of scripts they have written.
Some authors use the common home page for the collection of scripts they have written.
But remember, the namespace can be any unique value.
But remember, the namespace can be any unique value.
=== @noframes ===
Example:
<pre class='sample'>// @noframes</pre>
When present, this imperative restricts the execution of the script.
The script will run only in the top-level document, never in nested frames.
It takes no arguments, it is either present or not present.
This is off (scripts run in frames) by default.


=== @require ===
=== @require ===
Compatibility: [[Version_history#0.8.20080609.0|Greasemonkey 0.8.0+]]


Example:
Example:
<nowiki>// @require http://www.example.com/example.js</nowiki>
<pre class='sample'>// @require http://www.example.com/example.js</pre>


There can be any number of @require keys in a script.
There can be any number of @require keys in a script.
Line 102: Line 149:


* [[:Category:@require Library]]
* [[:Category:@require Library]]
* [[#Adding Resources]]


=== @resource ===
=== @resource ===
:Compatibility: [[Version_history#0.8.20080609.0|Greasemonkey 0.8.0+]]


Example:
Example:
<nowiki>// @resource resourceName http://www.example.com/example.png</nowiki>
<pre class='sample'>// @resource resourceName http://www.example.com/example.png</pre>


There can be any number of @resource keys in a script.
There can be any number of @resource keys in a script.
Line 118: Line 164:
The URL specified may be relative to the URL the script is being installed from.
The URL specified may be relative to the URL the script is being installed from.


These named resources may be accessed through [[GM_getResourceText]] and [[GM_getResourceURL]] respectively.
These named resources may be accessed through [[GM_getResourceURL|GM.getResourceURL]].


Note that since [[Version_history#0.9.0|Greasemonkey 0.9.0]], if Greasemonkey detects that the <code>@resource</code> value(s) have been altered, these new values will be used (thus each <code>@resource</code> shall be re-downloaded).
See also:
 
* [[#Adding Resources]]


=== @run-at ===
=== @run-at ===


:Compatibility: [[Version_history#0.9.8|Greasemonkey 0.9.8+]]
:Compatibility: Limited support in [[Version_history#4.0_2|Greasemonkey 4.0]].


Example:
Example:
<nowiki>// @run-at document-end</nowiki>
<pre class='sample'>// @run-at document-end</pre>


Supports two values: <code>document-end</code> and <code>document-start</code>.
This key supports three values:


End is the standard behavior that Greasemonkey has always had (see [[DOMContentLoaded]]).
; document-end
This is the default if no value is provided.
: The default if no value is provided. The script will run after the main page is loaded, but before other resources (images, style sheets, etc.) have loaded. The only guaranteed working value in Greasemonkey 4.x.
 
; document-start
Start is new as of version 0.9.8.
: The script will run before any document begins loading, thus before any scripts run or images load.
The script will run before any document begins loading, thus before any scripts run or images load.
; document-idle
: The script will run after the page and all resources (images, style sheets, etc.) are loaded and page scripts have run.


To detect if you are running at <code>document-start</code> time, check the value of <code>[https://developer.mozilla.org/en/DOM/document.readyState document.readyState]</code>.
To detect if you are running at <code>document-start</code> time, check the value of <code>[https://developer.mozilla.org/en/DOM/document.readyState document.readyState]</code>.
For example:
For example:


<pre class="sample-good">
<pre class="sample">
if ('loading' == document.readyState) {
if ('loading' == document.readyState) {
   alert("This script is running at document-start time.");
   alert("This script is running at document-start time.");
Line 149: Line 198:


Scripts running at <code>document-end</code> will have the value <code>interactive</code> here.
Scripts running at <code>document-end</code> will have the value <code>interactive</code> here.
'''Note:''' Scripts set to run at <code>document-start</code> time will run  only in top-level documents, not in <code>FRAME</code>s or <code>IFRAME</code>s.
=== @unwrap ===
Compatibility: [[Version_history#0.8.20090123.1|Greasemonkey 0.8.1+]]
Example:
// @unwrap
This declarative key has no associated value.
It changes the interpretation of the script as it executes.
Normally, greasemonkey scripts are wrapped in an anonymous function when they are executed.
This behavior started in very early versions of greasemonkey, which ran scripts directly in the content page, to separate their variables from those in the page.
In later versions, it remains because isolating the script from the [[sandbox]] scope still helps in certain cases.
This key is strongly recommended to only be used for [[Global_object#Debugging|debugging]] purposes.


=== @version ===
=== @version ===
Compatibility: [[Version_history#0.9.0|Greasemonkey 0.9.0+]]


Example:
Example:
// @version 1.0
<pre class='sample'>// @version 1</pre>


This version is, as of Greasemonkey 0.9.0, only used for display in the script management interface.
The version is used by the auto-update feature.
It should be treated like a [https://developer.mozilla.org/en/install_manifests#version firefox extension version], and maintain the [https://developer.mozilla.org/en/Extension_Versioning%2c_Update_and_Compatibility same syntax].
When `@version` is set and if the auto-update feature is enabled, Greasemonkey will check periodically for new versions of the script by downloading it again (from same location where it was originally installed).
If the new downloaded `@version` is higher, the update will be installed.
Greasemonkey uses the [https://github.com/omichelsen/compare-versions#compare-versions compare-versions] library to determine when a version is higher/greater/newer.


== Examples ==
== Examples ==


<pre class='sample'><nowiki>
<pre class='sample'>
// ==UserScript==
// ==UserScript==
// @name          My Script
// @name          My Script
Line 194: Line 225:
// @icon          http://www.example.net/icon.png
// @icon          http://www.example.net/icon.png
// ==/UserScript==
// ==/UserScript==
</nowiki></pre>
</pre>
 
== Adding Resources ==
 
Two metadata imperatives allow downloading files (once, at install time) for efficient reference:
First [[#@require|@require]], which includes a remote code resource.
Second [[#@resource|@resource]], which makes a remote data resource available.
 
It is possible to add a new entry for either of these values, while editing a script that is already installed.
In either case, simply add the appropriate line and the referenced file will be downloaded and made available the next time the script runs.
If you specify a relative URL, then it will be interpreted as relative to the the URL the script was originally downloaded from.
<!--
If there was no original download URL (i.e. a brand new script created from a local file), then this will work if you also create an appropriately named file alongside the script.  (As of Greasemonkey 1.0.)
-->


== See Also ==
== See Also ==


* [[API reference]]
* [[API reference]]
* [[Knowing Your Own Metadata]]
* [[Third-Party Libraries]]
* [[Third-Party Libraries]]



Latest revision as of 01:56, 6 November 2023

Description

The metadata block is a section of a user script that describes the script. It usually contains the script name, namespace, description, and include and exclude rules. The metadata block appears in JavaScript line comments and may appear anywhere in the top level Greasemonkey code scope of the script, but is usually near the top of the file.

If the metadata block includes a key that Greasemonkey does not understand, it will be ignored.

Syntax

The metadata block must follow the format:

// ==UserScript==
// @key value
// ==/UserScript==

It must use line comments (//) like above, not block comments (/* */). Note that the opening // ==UserScript== and closing // ==/UserScript== must be precisely at the beginning of its line. Some keys may have multiple values. In all cases the key and value(s) are separated by whitespace. The closing ==/UserScript== line should be at the end of the metadata block (not at the end of the script).

@description

Example:

// @description This script even does the laundry!

Just a brief summary of what the script does, presented to the user as the script is installed, and in the manage dialog.

As of Greasemonkey 2.2: can be localized for multiple languages; see the #@name documentation for more detail.


@exclude

Examples: see #@include

See Include and exclude rules. There can be any number of @exclude rules in a script.

@grant

See dedicated @grant page.

@icon

Example:

// @icon http://www.example.org/icon.png

The icon is, as of Greasemonkey 0.9.0, displayed in the script management interface. Almost any image will work, but a 32x32 pixel size is best. This value may be specified relative to the URL the script itself is downloaded from.

@include

Examples:

// @include http://www.example.com/*
// @include http://*
// @include *

See Include and exclude rules. There can be any number of @include rules in a script.

@match

Examples:

// @match https://www.example.com/*
// @match http://*.example.com/*

The @match metadata imperative is very similar to @include, however it is safer. It sets more strict rules on what the * character means.

For details, see the documentation on Match Patterns for Google Chrome. Chrome implemented @match first, and Greasemonkey has been designed to be compatible.

@name

Example:

// @name Example Script

The name of the script. This appears in the monkey menu, and is also the unique identifier of a script (within a namespace). If no name is provided, it will be derived from the file name.

As of Greasemonkey 2.2: can be localized for multiple languages, for example:

// @name       Example Script
// @name:cs    Uživatelské skripty
// @name:es-MX Ejemplo Script 
// @name:ru    Пользовательские скрипты

Add a colon and the locale code, which is the ISO 639 language code and optionally a hyphen and ISO 3166 country code, when disambiguation is necessary. When the user's browser is configured with the matching primary language, that value will be displayed instead.

@namespace

Example:

// @namespace http://www.example.com/gmscripts

The combination of namespace and name is the unique identifier for a Greasemonkey script. If a script is being installed, and a script with that same name and namespace already exists, it will be replaced by the new script. Otherwise, the new script is added to the set of installed scripts. A script author will usually put all of their scripts under one common namespace, and then assign each script a unique name.

While the namespace is non-semantic, a URL is often used. Some authors use the common home page for the collection of scripts they have written. But remember, the namespace can be any unique value.

@noframes

Example:

// @noframes

When present, this imperative restricts the execution of the script. The script will run only in the top-level document, never in nested frames. It takes no arguments, it is either present or not present. This is off (scripts run in frames) by default.

@require

Example:

// @require http://www.example.com/example.js

There can be any number of @require keys in a script. Each @require is downloaded once, when the script is installed, and stored on the user's hard drive alongside the script. The URL specified may be relative to the URL the script is being installed from.

Note that since Greasemonkey 0.9.0, if Greasemonkey detects that the @require value(s) have been altered, these new values will be used (thus each @require shall be re-downloaded).

See also:

@resource

Example:

// @resource resourceName http://www.example.com/example.png

There can be any number of @resource keys in a script.

While the resourceName is non-semantic, it should comply with JavaScript identifier restrictions. Each @resource must have a unique name.

Each @resource is downloaded once, when the script is installed, and stored on the user's hard drive alongside the script. The URL specified may be relative to the URL the script is being installed from.

These named resources may be accessed through GM.getResourceURL.

See also:

@run-at

Compatibility: Limited support in Greasemonkey 4.0.

Example:

// @run-at document-end

This key supports three values:

document-end
The default if no value is provided. The script will run after the main page is loaded, but before other resources (images, style sheets, etc.) have loaded. The only guaranteed working value in Greasemonkey 4.x.
document-start
The script will run before any document begins loading, thus before any scripts run or images load.
document-idle
The script will run after the page and all resources (images, style sheets, etc.) are loaded and page scripts have run.

To detect if you are running at document-start time, check the value of document.readyState. For example:

if ('loading' == document.readyState) {
  alert("This script is running at document-start time.");
} else {
  alert("This script is running with document.readyState: " + document.readyState);
}

Scripts running at document-end will have the value interactive here.

@version

Example:

// @version 1

The version is used by the auto-update feature. When `@version` is set and if the auto-update feature is enabled, Greasemonkey will check periodically for new versions of the script by downloading it again (from same location where it was originally installed). If the new downloaded `@version` is higher, the update will be installed. Greasemonkey uses the compare-versions library to determine when a version is higher/greater/newer.

Examples

// ==UserScript==
// @name          My Script
// @namespace     http://www.example.com/gmscripts
// @description   Scripting is fun
// @include       http://www.example.com/*
// @include       http://www.example.org/*
// @exclude       http://www.example.org/foo
// @require       foo.js
// @resource      resourceName1 resource1.png
// @resource      resourceName2 http://www.example.com/resource2.png
// @version       1.0
// @icon          http://www.example.net/icon.png
// ==/UserScript==

Adding Resources

Two metadata imperatives allow downloading files (once, at install time) for efficient reference: First @require, which includes a remote code resource. Second @resource, which makes a remote data resource available.

It is possible to add a new entry for either of these values, while editing a script that is already installed. In either case, simply add the appropriate line and the referenced file will be downloaded and made available the next time the script runs. If you specify a relative URL, then it will be interpreted as relative to the the URL the script was originally downloaded from.

See Also

Notes