|
|
(130 intermediate revisions by 33 users not shown) |
Line 1: |
Line 1: |
| __NOTOC__
| |
| {{Greasemonkey Manual TOC}}
| |
|
| |
| == Description == | | == Description == |
| The '''metadata block''' is a section of a [[user script]] that does not execute any code, but describes the script. It contains the script name, namespace, description, and [[include and exclude rules]].
| |
|
| |
| The metadata block appears in JavaScript comments and may appear anywhere in the top level Greasemonkey code scope of the script, but is usually near the top of the file. It begins with the line
| |
|
| |
| {{Core samp |1=<pre style="border: none; margin: inherit;">// ==UserScript==</pre>}}
| |
|
| |
| and ends with
| |
|
| |
| {{Core samp |1=<pre style="border: none; margin: inherit;">// ==/UserScript==</pre>}}
| |
|
| |
| Everything between those lines is in the format
| |
|
| |
| {{Core samp |1=<pre style="border: none; margin: inherit;">// @key value</pre>}}
| |
|
| |
|
| If the metadata block includes a key that Greasemonkey does not understand, it will simply be ignored. Key names are typically constructed from alphanumeric characters. User script hosting sites may utilize a XML styled name prefix to identify the origin of the key. An example of this is ''prefix:key''.
| | 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. |
|
| |
|
| [[#Other_Keys|Other Keys]] | [[#Examples|Examples]] | [[#Caveats|Caveats]] | [[#See_Also|See Also]] | [[#Notes|Notes]]
| | If the metadata block includes a key that Greasemonkey does not understand, it will be ignored. |
|
| |
|
| == Syntax == | | == Syntax == |
| '''// ==UserScript=='''
| |
|
| |
|
| '''// == @'''''key'' '''value'''
| | The metadata block must follow the format: |
|
| |
|
| '''// ==/UserScript==''' | | <pre class='sample'> |
| | | // ==UserScript== |
| :Value: Object
| | // @key value |
| :Compatibility: [[Version_history#0.2.5|Greasemonkey 0.2.5+]]
| | // ==/UserScript== |
| | | </pre> |
| :{| cellpadding="5" style="border-style:solid; background:#FFFFE0;"
| |
| |+ Keys
| |
| ! colspan="4" style="background:#CC9900;"|'''Properties'''
| |
| |-
| |
| | <code><span style="background:#FFFFE0;">[[#.40name |@name]]</span></code> || <code><span style="background:#FFFFE0;">[[#.40include |@include]]</span></code> || <code><span style="background:#FFFFE0;">[[#.40resource |@resource]]</span></code> || <code><span style="background:#FFFFE0;">[[#.40unwrap |@unwrap]]</span></code>
| |
| |-
| |
| |<code><span style="background:#FFFFE0;">[[#.40namespace |@namespace]]</span></code> || <code><span style="background:#FFFFE0;">[[#.40exclude |@exclude]]</span></code> || <code><span style="background:#FFFFE0;">[[#.40require |@require]]</span></code>
| |
| |-
| |
| |<code><span style="background:#FFFFE0;">[[#.40description |@description]]</span></code>
| |
| |}
| |
| :* All properties are optional
| |
| | |
| [[#top|top]]
| |
| === Properties ===
| |
| ----
| |
| | |
| ==== @name ====
| |
| | |
| :Value: String
| |
| :Usage: <code>// @'''name''' My Script</code>
| |
| ::* 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 used to determine whether to overwrite an old version of a script or to install it separately. If no name is provided, it will be inferred from the file name.
| |
| ::* If the file name or the @name key exceeds 24 characters the file name will be truncated during installation not including spaces and other special characters. As of [[Version_history#0.8.20080609.0|Greasemonkey 0.8.0]], new scripts will be stored under their own folder name in the [[Scripts directory|gm_scripts folder]] and also include spaces to underscores conversion and other special characters. The [[Scripts directory|scripts directory]] is also backed up into a folder called gm_scripts_08bak when migrating to version [[Version_history#0.8.20080609.0|0.8.0]].
| |
| [[#top|top]] | [[#Syntax|back]]
| |
|
| |
|
| ==== @namespace ==== | | It must use line comments (<code>//</code>) like above, not block comments (<code>/* */</code>). |
| | Note that the opening <code>// ==UserScript==</code> and closing <code>// ==/UserScript==</code> 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 <code>==/UserScript==</code> line should be at the end of the metadata block (not at the end of the script). |
|
| |
|
| :Value: String
| | === @description === |
| :Usage: <code>// @'''namespace''' <nowiki>http://www.example.com/gmscripts/</nowiki></code>
| |
| ::* The namespace, along with the name, is used to determine whether to overwrite an old version of a script or to install it separately. A script author will usually put all of their scripts under one common namespace, and then assign each script a unique name. If two scripts have the same name, but a different namespace, they can co-exist. However, two scripts of the same name in the same namespace are assumed to be replacements for one another.
| |
| ::* While the namespace is '''non-semantic''', it should be your prefered internet homepage [http://labs.apache.org/webarch/uri/rfc/rfc3986.html URI] according to the [http://www.w3.org/Addressing W3C standards specification]. If no namespace is provided, it is assumed to be the domain from which the script is installed. Since a script can live on various servers or on a local file system, authors may choose to omit this key when publishing on [http://userscript.org userscripts.org] and let Greasemonkey supply one automatically. If you are creating one locally, authors may choose the [http://labs.apache.org/webarch/uri/rfc/rfc3986.html URI specification] standard of <code>http://localhost</code> for the URI value as being an anonymous local script, but runs the risk of another script with the same name and namespace overwriting it.
| |
|
| |
|
| ::* Common String Values | | Example: |
| :::* '''Greasemonkey addon supplied (e.g. @namespace omitted in source script file)'''
| | <pre class='sample'>// @description This script even does the laundry!</pre> |
| ::::Examples:
| |
| ::::{| cellpadding="5" style="border-style:solid; background:#FFFFE0;"
| |
| |+ ''Automatic host to namespace mapping''
| |
| !style="background:#CC9900;"|'''Script Host''' || style="background:#CC9900;"|'''Chosen Namespace'''
| |
| |-
| |
| |<code><span style="background:#FFFFE0;">http://userscripts.org/scripts/source/''scriptid''.user.js</span></code> || <code><span style="background:#FFFFE0;">userscripts.org</span></code>
| |
| |-
| |
| |<code><span style="background:#FFFFE0;">http://example.org/scripts/filename.user.js</span></code> || <code><span style="background:#FFFFE0;">example.org</span></code>
| |
| |-
| |
| |<code><span style="background:#FFFFE0;">http://localhost/scripts/filename.user.js</span></code> || <code><span style="background:#FFFFE0;">localhost</span></code>
| |
| |-
| |
| |}
| |
|
| |
|
| :::* '''tag URI'''
| | Just a brief summary of what the script does, presented to the user as the script is installed, and in the manage dialog. |
| ::::Usage: "tag:" taggingEntity ":" specific [ "#" fragment ]
| |
| ::::Examples:
| |
| :::::<code>'''@namespace''' <nowiki>tag:johndoe@example.com,2009:johndoe</nowiki></code>
| |
| :::::<code>'''@namespace''' <nowiki>tag:johndoe@example.net,2010-05:johndoe/pathto</nowiki></code>
| |
| :::::<code>'''@namespace''' tag:userscripts.org,2005-06-19T23:33:49Z:JohnDoe:DescriptionWithNoSpaces:etc:etc:etc</code>
| |
| ::::See Also: http://www.faqs.org/rfcs/rfc4151.html
| |
|
| |
|
| :::* '''userscripts.org''' ''(User contributed)''
| | ''As of Greasemonkey 2.2'': can be localized for multiple languages; see the [[#@name]] documentation for more detail. |
|
| |
|
| ::::Examples:
| | <!-- 4.x does not use this |
| :::::<code>'''@namespace''' userid</code>
| | === @downloadURL === |
| :::::<code>'''@namespace''' userid + userid</code>
| |
| :::::<code>'''@namespace''' vanityid + attribution1vanityid + contributor1vanityid</code>
| |
|
| |
|
| :::* '''XML namespace'''
| | The URL to download this script from, when installing updates. |
| ::::Usage: <nowiki>http://domain.tld/pathto/</nowiki>
| |
| ::::Examples:
| |
| :::::<code>'''@namespace''' http://localhost</code>
| |
| :::::<code>'''@namespace''' http://localhost.localdomain/</code>
| |
| :::::<code>'''@namespace''' http://userscripts.org/users/''userid''</code>
| |
| :::::<code>'''@namespace''' http://userscripts.org/scripts/show/''scriptid''</code>
| |
| :::::<code>'''@namespace''' http://example.org/pathto/script</code>
| |
| ::::See Also: https://developer.mozilla.org/en/Namespaces
| |
|
| |
|
| ::* The [https://addons.mozilla.org/en-US/firefox/addon/722 NoScript] add-on currently utilizes namespace filtering, and may sanitize user scripts that are not listed in the XSS white-list section of the options dialog.
| | <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. |
|
| |
|
| [[#top|top]] | [[#Syntax|back]]
| | It is '''unusual''' to specify this value. |
| | Most scripts should omit it. |
|
| |
|
| ==== @description ====
| | --> |
| :Value: String
| |
| :Usage: <code>// @'''description''' This script even does the laundry!</code>
| |
| ::* Just a brief summary of what the script does, to present to the user who is installing it.
| |
|
| |
|
| [[#top|top]] | [[#Syntax|back]]
| | === @exclude === |
|
| |
|
| ==== @include ====
| | Examples: see [[#@include]] |
| :Value: String
| |
| :Usage: <code>// @'''include''' <nowiki>http://www.example.com/*</nowiki></code> | |
| ::* Refer to ''[[Include and exclude rules]]''. There can be any number of @include rules in a script.
| |
| ::* You cannot edit a live scripts @include and have it take effect.
| |
| [[#top|top]] | [[#Syntax|back]]
| |
|
| |
|
| ==== @exclude ====
| | See [[Include and exclude rules]]. |
| :Value: String
| | There can be any number of @exclude rules in a script. |
| :Usage: <code>// @'''exclude''' <nowiki>http://www.example.com/foo/*</nowiki></code>
| |
| ::* Refer to ''[[Include and exclude rules]]''. There can be any number of @exclude rules in a script.
| |
| ::* You cannot edit a live scripts @exclude and have it take effect.
| |
| [[#top|top]] | [[#Syntax|back]]
| |
|
| |
|
| ==== @resource ====
| | === @grant === |
| :Value: String
| |
| :Compatibility: [[Version_history#0.8.20080609.0|Greasemonkey 0.8.0+]]
| |
| :Usage: <code>// @'''resource''' resourceName <nowiki>http://www.example.com/resource.png</nowiki></code>
| |
| ::* While the resourceName is non-semantic, it is suggested that it should be compatible with JavaScript variable naming conventions and XML/CSS naming conventions to help keep things consistent.
| |
| ::* Each resourceName must have a unique name.
| |
| ::* This metadata block key property may be used to include local and remote resources into the current script at first install but not successive installs. A user is required to uninstall the current user script before installing a newer version if new resource keys or require keys are present.
| |
| ::* Resources may include the scheme of file, http, https or ftp. If no scheme is provided, it is assumed to be the domain from which the script is installed including the full path. They may be accessed through [[GM_getResourceText]] and [[GM_getResourceURL]] respectively.
| |
|
| |
|
| [[#top|top]] | [[#Syntax|back]] | | See dedicated [[@grant]] page. |
|
| |
|
| ==== @require ====
| | === @icon === |
|
| |
|
| :Value: String | | Example: |
| :Compatibility: [[Version_history#0.8.20080609.0|Greasemonkey 0.8.0+]]
| | <pre class='sample'>// @icon http://www.example.org/icon.png</pre> |
| :Usage: <code>// @'''require''' foo.js</code>
| |
| ::* This metadata block key property may be used to include local or remote scripts into the current script at first install, but not successive installs.
| |
| ::* Once a script is installed, changing the @require or @resource values will have no effect. The script must be uninstalled and reinstalled.
| |
| :::* Similarly, if a user is updating their script and it contains new values, they must uninstall the current user script before installing a newer version.
| |
| ::* Script sources may include the scheme of file, http, https or ftp. If no scheme is provided, it is assumed to be the domain from which the script is installed including the full path.
| |
| :::* Some common JavaScript libraries may be obtained from the [http://code.google.com/apis/ajaxlibs/documentation/ Google AJAX Libraries API] or [http://developer.yahoo.com/yui/articles/hosting/ YUI]).
| |
|
| |
|
| [[#top|top]] | [[#Syntax|back]]
| | 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. |
|
| |
|
| ==== @unwrap ====
| | === @include === |
|
| |
|
| :Value: undefined | | Examples: |
| :Compatibility: [[Version_history#0.8.20090123.1|Greasemonkey 0.8.1+]]
| | <pre class='sample'>// @include http://www.example.com/* |
| :Usage: <code>// @'''unwrap'''</code>
| | // @include http://* |
| ::* Also referenced as a metadata imperative. This metadata block key property must not include any value after it or it may be ignored altogether.
| | // @include *</pre> |
| ::* Removes the anonymous function wrapper around scripts. | |
| :::* '''This key is strongly recommended to only be used for [[Global_object#Debugging|debugging]] purposes.'''
| |
| :::* By default most versions of Greasemonkey automatically encapsulate each script in an anonymous function wrapper. In other words:
| |
| ::::* <code>(function(){ /* script source */ })();</code> | |
| :::* Encapsulation of the script prevents name collisions of variables and objects used by [[XPCNativeWrapper]] and other objects typically inserted by the browser. If an unwrapped user script uses a reserved object name incorrectly, it will throw an error and script execution will most likely fail. One of the simplest ways to confirm this is to include '''@unwrap''' and have a script with a simple variable declaration of <code>var sidebar = "foo";</code>. This will return a script error of Illegal value, and script execution will cease. See also [http://greasemonkey.devjavu.com/ticket/108 #108].
| |
|
| |
|
| [[#top|top]] | [[#Syntax|back]] | | See [[Include and exclude rules]]. |
| | There can be any number of @include rules in a script. |
|
| |
|
| == Other Keys == | | === @match === |
|
| |
|
| Some [[user script]]s contain other special keys in the metadata block.
| | Examples: |
| | <pre class='sample'>// @match https://www.example.com/* |
| | // @match http://*.example.com/*</pre> |
|
| |
|
| '''These unsupported metadata keys serve no technical purpose and are ignored by the [[Greasemonkey|Greasemonkey extension]], but can be read by human beings or utilized by other code.'''
| | 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. |
|
| |
|
| :{| cellpadding="5" style="border-style:solid; background-color:#FFFFFF;"
| | For details, see the documentation on [https://developer.chrome.com/extensions/match_patterns Match Patterns] for Google Chrome. |
| |+ Keys
| | Chrome implemented <code>@match</code> first, and Greasemonkey has been designed to be compatible. |
| ! colspan="4" style="color: #FFFFFF; background-color:#000000;"|'''Properties'''
| |
| |-
| |
| | colspan="2" style="text-align: center; color: #FFFFFF; background-color:#FF8800;"|'''userscripts.org'''
| |
| | colspan="2" style="text-align: center; color: #FFFFFF; background-color:#FF8800;"|'''user contributed'''
| |
| |-
| |
| | <code><span style="background-color:#FFFFFF;">[[#.40copyright |@copyright]]</span></code> || <code><span style="background-color:#FFFFFF;">[[#.40uso:script |@uso:script]]</span></code> || <code><span style="background-color:#FFFFFF;">[[#.40attribution |@attribution]]</span></code> || <code><span style="background-color:#FFFFFF;">[[#.40author |@author]]</span></code>
| |
| |-
| |
| |<code><span style="background-color:#FFFFFF;">[[#.40license |@license]]</span></code> || <code><span style="background-color:#FFFFFF;">[[#.40uso:version |@uso:version]]</span></code> || <code><span style="background-color:#FFFFFF;">[[#.40contributor |@contributor]]</span></code> || <code><span style="background-color:#FFFFFF;">[[#.40homepage |@homepage]]</span></code>
| |
| |-
| |
| |<code><span style="background-color:#FFFFFF;">[[#.40version |@version]]</span></code>
| |
| |}
| |
| :* This is by no means an exhaustive list, nor is it meant to be.
| |
| [[#top|top]]
| |
|
| |
|
| ===== @copyright =====
| | === @name === |
|
| |
|
| :Value: String | | Example: |
| :Usage: <code>// @'''copyright''' Year, Author (Author Homepage)</code>
| | <pre class='sample'>// @name Example Script</pre> |
| ::* Example:
| |
| ::: <code>// @copyright 2009+, John Doe (<nowiki>http://www.example.com/projecthomepage</nowiki>)</code>
| |
|
| |
|
| [[#top|top]] | [[#Other Keys|back]] | | The name of the script. |
| | 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. |
|
| |
|
| ===== @license =====
| | ''As of Greasemonkey 2.2'': can be localized for multiple languages, for example: |
| ===== @licence =====
| |
|
| |
|
| :Value: String
| | <pre class='sample'> |
| :Usage: <code>// @'''license''' License Type; License Homepage</code>
| | // @name Example Script |
| ::* There can be any number of @license keys in a script for multiple licensing however userscripts.org will only display the first occurrence as the primary.
| | // @name:cs Uživatelské skripty |
| ::* Example:
| | // @name:es-MX Ejemplo Script |
| ::: <code>// @license GPL version 3 or any later version; <nowiki>http://www.gnu.org/copyleft/gpl.html</nowiki></code>
| | // @name:ru Пользовательские скрипты |
| ::: <code>// @license (CC) Attribution Non-Commercial Share Alike; <nowiki>http://creativecommons.org/licenses/by-nc-sa/3.0/</nowiki></code>
| | </pre> |
|
| |
|
| [[#top|top]] | [[#Other Keys|back]] | | 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. |
|
| |
|
| ===== @version =====
| | === @namespace === |
|
| |
|
| :Value: String or Number | | Example: |
| :Usage: <code>// @'''version''' 0.0.1</code>
| |
| ::* Typically this is in the industry standard form of major dot minor dot build
| |
| ::* This key can also be used in some scripts that are self-updating. Usually, this will include a time stamp indicating the number of milliseconds that have expired since 1 January 1970 00:00:00 UCT, which can be generated by calling <code>new Date().getTime()</code>.
| |
|
| |
|
| [[#top|top]] | [[#Other Keys|back]]
| | <pre class='sample'>// @namespace http://www.example.com/gmscripts</pre> |
|
| |
|
| ===== @uso:script =====
| | 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. |
|
| |
|
| :Value: String or Number
| | While the namespace is non-semantic, a URL is often used. |
| :Usage: <code>// @'''uso:script''' ''scriptid'' </code>
| | Some authors use the common home page for the collection of scripts they have written. |
| ::* userscripts.org will dynamically create an ascending numerical value based on the next available script id in the repository.
| | But remember, the namespace can be any unique value. |
| :: '''NOTE:''' It is not necessary to define this key in your script as their meta.js routine will automatically return this key/value for you via a http/https request to <nowiki>http://userscripts.org/scripts/source/</nowiki>''scriptid''.meta.js
| |
|
| |
|
| [[#top|top]] | [[#Other Keys|back]]
| | === @noframes === |
|
| |
|
| ===== @uso:version ===== | | Example: |
| | <pre class='sample'>// @noframes</pre> |
|
| |
|
| :Value: String or Number
| | When present, this imperative restricts the execution of the script. |
| :Usage: <code>// @'''uso:version''' ''versionid'' </code>
| | The script will run only in the top-level document, never in nested frames. |
| ::* userscripts.org will dynamically create an ascending numerical value based on the next available version id in the repository.
| | It takes no arguments, it is either present or not present. |
| :: '''NOTE:''' It is not necessary to define this key in your script as their meta.js routine will automatically return this key/value for you via a http/https request to <nowiki>http://userscripts.org/scripts/source/</nowiki>''scriptid''.meta.js
| | This is off (scripts run in frames) by default. |
|
| |
|
| [[#top|top]] | [[#Other Keys|back]]
| | === @require === |
|
| |
|
| ===== @attribution ===== | | Example: |
| | <pre class='sample'>// @require http://www.example.com/example.js</pre> |
|
| |
|
| :Value: String
| | There can be any number of @require keys in a script. |
| :Usage: <code>// @'''attribution''' Attribution Name (Attribution Script Homepage)</code>
| | Each @require is downloaded once, when the script is installed, and stored on the user's hard drive alongside the script. |
| ::* There can be any number of @attribution keys in a script for multiple attribution support.
| | The URL specified may be relative to the URL the script is being installed from. |
| ::* Attribution may only be used if source [[#.40license|@license]] type is compatible.
| |
| ::* Example:
| |
| ::: <code>// @attribution Jane Doe (<nowiki>http://www.example.com/janedoe/scriptid</nowiki>)</code>
| |
| ::: <code>// @attribution Jack Doe (<nowiki>http://www.example.com/jackdoe/scriptid</nowiki>)</code>
| |
| ::: <code>// @attribution Jill Doe (<nowiki>http://www.example.com/jilldoe/scriptid</nowiki>)</code>
| |
|
| |
|
| [[#top|top]] | [[#Other Keys|back]] | | Note that since [[Version_history#0.9.0|Greasemonkey 0.9.0]], if Greasemonkey detects that the <code>@require</code> value(s) have been altered, these new values will be used (thus each <code>@require</code> shall be re-downloaded). |
|
| |
|
| ===== @author =====
| | See also: |
|
| |
|
| :Value: String | | * [[:Category:@require Library]] |
| :Usage: <code>// @'''author''' John Doe</code> | | * [[#Adding Resources]] |
| ::* ''(Suggest not using this key in favor of [[#.40copyright |@copyright]])''
| |
|
| |
|
| [[#top|top]] | [[#Other Keys|back]]
| | === @resource === |
|
| |
|
| ===== @contributor ===== | | Example: |
| | <pre class='sample'>// @resource resourceName http://www.example.com/example.png</pre> |
|
| |
|
| :Value: String
| | There can be any number of @resource keys in a script. |
| :Usage: <code>// @'''contributor''' Contributor Name (Contributor Homepage)</code>
| |
| ::* There can be any number of @contributor keys in a script for multiple contributor support.
| |
| ::* Example:
| |
| ::: <code>// @contributor Jane Doe (<nowiki>http://www.example.com/janedoe</nowiki>)</code>
| |
| ::: <code>// @contributor Jack Doe (<nowiki>http://www.example.com/jackdoe</nowiki>)</code>
| |
| ::: <code>// @contributor Jill Doe (<nowiki>http://www.example.com/jilldoe</nowiki>)</code>
| |
|
| |
|
| [[#top|top]] | [[#Other Keys|back]]
| | While the resourceName is non-semantic, it should comply with JavaScript identifier restrictions. |
| | Each @resource must have a unique name. |
|
| |
|
| ===== @homepage =====
| | 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. |
|
| |
|
| :Value: URI
| | These named resources may be accessed through [[GM_getResourceURL|GM.getResourceURL]]. |
| :Usage: <code>// @'''homepage''' <nowiki>http://www.example.com/myhomepage</nowiki></code>
| |
| ::* ''(Suggest not using this key in favor of [[#.40copyright |@copyright]])''
| |
|
| |
|
| [[#top|top]] | [[#Other Keys|back]]
| | See also: |
|
| |
|
| == Examples ==
| | * [[#Adding Resources]] |
| [[#Core |Core]] | [[#Knowing Your Own Metadata |Knowing Your Own Metadata]] | [[#jQuery require |jQuery require]] | |
|
| |
|
| === Core === | | === @run-at === |
| {{Core samp |1=<pre style="border: none; margin: inherit;">
| |
| // ==UserScript==
| |
| // @name My Script
| |
| // @namespace <nowiki>http://www.example.com/gmscripts</nowiki>
| |
| // @description Scripting is fun
| |
| // @include <nowiki>http://www.example.com/*</nowiki>
| |
| // @include <nowiki>http://www.example.org/*</nowiki>
| |
| // @exclude <nowiki>http://www.example.org/foo</nowiki>
| |
| // @require foo.js
| |
| // @resource resourceName1 resource1.png
| |
| // @resource resourceName2 <nowiki>http://www.example.com/resource2.png</nowiki>
| |
| // ==/UserScript==
| |
| </pre>}}
| |
|
| |
|
| [[#top|top]] | [[#Examples|back]] | | :Compatibility: Limited support in [[Version_history#4.0_2|Greasemonkey 4.0]]. |
|
| |
|
| === Knowing Your Own Metadata === | | Example: |
| Routine utilizing E4X (ECMAScript for XML) XMLList to retrieve a copy of the initial file metadata block entries and https metadata block entries.
| | <pre class='sample'>// @run-at document-end</pre> |
|
| |
|
| :* If a user changes the include/exclude rules via Manage User Scripts, they will not be reflected in this copy.
| | This key supports three values: |
| :* If a user omits [[#.40namespace |@namespace]], and allows Greasemonkey to supply one, it will not be reflected in this copy.
| |
| :* Remember to change the [[#.40uso:script |@uso:script]] key to match the ''scriptid'' hosted on userscripts.org
| |
| {{Good samp |1=<pre style="border: none; margin: inherit;">
| |
| function parseHeaders(metadataBlock) {
| |
| var headers = {};
| |
| var line, name, prefix, header, key, value;
| |
|
| |
|
| var lines = metadataBlock.split(/\n/).filter(/\/\/ @/);
| | ; document-end |
| for each (line in lines) {
| | : 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. |
| [, name, value] = line.match(/\/\/ @(\S+)\s*(.*)/);
| | ; 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. |
|
| |
|
| switch (name) {
| | 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>. |
| case "licence":
| | For example: |
| name = "license";
| |
| break;
| |
| }
| |
|
| |
|
| [key, prefix] = name.split(/:/).reverse();
| | <pre class="sample"> |
| | if ('loading' == document.readyState) { |
| | alert("This script is running at document-start time."); |
| | } else { |
| | alert("This script is running with document.readyState: " + document.readyState); |
| | } |
| | </pre> |
|
| |
|
| if (prefix) {
| | Scripts running at <code>document-end</code> will have the value <code>interactive</code> here. |
| if (!headers[prefix])
| |
| headers[prefix] = new Object;
| |
| header = headers[prefix];
| |
| } else
| |
| header = headers;
| |
|
| |
|
| if (header[key] && !(header[key] instanceof Array))
| | === @version === |
| header[key] = new Array(header[key]);
| |
|
| |
|
| if (header[key] instanceof Array)
| | Example: |
| header[key].push(value);
| | <pre class='sample'>// @version 1</pre> |
| else
| |
| header[key] = value;
| |
| }
| |
|
| |
|
| headers["licence"] = headers["license"];
| | 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 [https://github.com/omichelsen/compare-versions#compare-versions compare-versions] library to determine when a version is higher/greater/newer. |
|
| |
|
| return headers;
| | == Examples == |
| }
| |
|
| |
|
| var fileMETA = parseHeaders(<><![CDATA[
| | <pre class='sample'> |
| // ==UserScript== | | // ==UserScript== |
| // @name My Script | | // @name My Script |
| // @namespace http://www.example.com/gmscripts | | // @namespace http://www.example.com/gmscripts |
| // @description Scripting is fun | | // @description Scripting is fun |
| // @copyright 2009+, John Doe (http://www.example.com/~jdoe)
| |
| // @license GPL version 3 or any later version; http://www.gnu.org/copyleft/gpl.html
| |
| // @version 0.0.1
| |
| // @include http://www.example.com/* | | // @include http://www.example.com/* |
| // @include http://www.example.org/* | | // @include http://www.example.org/* |
Line 354: |
Line 222: |
| // @resource resourceName1 resource1.png | | // @resource resourceName1 resource1.png |
| // @resource resourceName2 http://www.example.com/resource2.png | | // @resource resourceName2 http://www.example.com/resource2.png |
| // @uso:script scriptid | | // @version 1.0 |
| // ==/UserScript==
| | // @icon http://www.example.net/icon.png |
| ]]></>.toString());
| |
| | |
| GM_xmlhttpRequest({
| |
| method:"GET",
| |
| url:"https://userscripts.org/scripts/source/" + fileMETA["uso"]["script"] + ".meta.js",
| |
| headers:{
| |
| "Accept":"text/javascript; charset=UTF-8"
| |
| },
| |
| overrideMimeType:"application/javascript; charset=UTF-8",
| |
| onload:function(response) {
| |
| var httpsMETA = parseHeaders(response.responseText);
| |
| | |
| GM_log([
| |
| "\n---------- Local ----------",
| |
| fileMETA["name"] + " version " + fileMETA["version"],
| |
| fileMETA["copyright"], | |
| fileMETA["license"],
| |
| fileMETA["description"],
| |
| fileMETA["include"],
| |
| fileMETA["exclude"],
| |
| "\n---------- Remote ----------",
| |
| httpsMETA["name"] + " version " + httpsMETA["version"],
| |
| httpsMETA["copyright"],
| |
| httpsMETA["license"],
| |
| httpsMETA["description"],
| |
| httpsMETA["include"],
| |
| httpsMETA["exclude"],
| |
| httpsMETA["uso"]["script"],
| |
| httpsMETA["uso"]["version"]
| |
| ].join("\n"));
| |
| }
| |
| });
| |
| </pre>}}
| |
| | |
| [[#top|top]] | [[#Examples|back]]
| |
| | |
| === jQuery require ===
| |
| {{Good samp |1=<pre style="border: none; margin: inherit;">
| |
| // ==UserScript==
| |
| // @name Hello jQuery | |
| // @namespace <nowiki>http://www.example.com/examples</nowiki>
| |
| // @description jQuery test script
| |
| // @include *
| |
| // @require <nowiki>http://code.jquery.com/jquery-latest.js</nowiki>
| |
| // ==/UserScript== | | // ==/UserScript== |
| | </pre> |
|
| |
|
| $(document).ready(function() {
| | == Adding Resources == |
| $("a").click(function() {
| |
| alert('Hello world!');
| |
| });
| |
| });
| |
| </pre>}}
| |
| | |
| [[#top|top]] | [[#Examples|back]]
| |
| | |
| == Caveats == | |
|
| |
|
| Changing the metadata of an installed script does not do anything, as this data is only accessed during installation. The script must be re-installed for these changes to take. Alternatively, [[config.xml]] can be modified manually.
| | 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. |
|
| |
|
| [[#top|top]]
| | 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]]
| |
|
| |
|
| [[#top|top]] | | * [[API reference]] |
| | * [[Third-Party Libraries]] |
|
| |
|
| == Notes == | | == Notes == |
|
| |
|
| [[#top|top]]
| | [[Category:API Reference]] |
| [[Category:API Reference|*{{FULLPAGENAME}}]] | |
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