GM.getResourceUrl: Difference between revisions
From GreaseSpot Wiki
Jump to navigationJump to search
m →<code>resourceName</code>: Link to IETF |
→Syntax: use GM.* not GM_* now |
||
(25 intermediate revisions by 5 users not shown) | |||
Line 1: | Line 1: | ||
== Description == | == Description == | ||
Given a defined [[Metadata Block#.40resource|@resource]], this method returns it as a URL. | |||
[[# | Compatibility: [[Version_history#4.0_2|Greasemonkey 4.0+]] | ||
== Syntax == | == Syntax == | ||
{{Function|GM.getResourceUrl|resourceName}} | |||
=== Arguments === | |||
; <code>resourceName</code> | |||
: <code>String</code> The name provided when the [[Metadata Block#.40resource|@resource]] was defined, follow that link for valid naming restrictions. | |||
=== Returns === | |||
=== | |||
A [[Promise]], rejected on failure and resolved with a <code>String</code> URL on success. | |||
Treat the result as opaque string. | |||
It will work where you need a URL (for a <code><link></code> or <code><style></code> for CSS, for an <code><img></code> tag, or similar). | |||
== Examples == | |||
<pre class='sample'> | |||
// ==UserScript== | // ==UserScript== | ||
// @name | // @name Image resource example | ||
// @ | // @resource logo ../icons/laptop.png | ||
// @grant GM.getResourceUrl | |||
// @ | |||
// ==/UserScript== | // ==/UserScript== | ||
(function() | (async function() { | ||
{ | let img = document.createElement("img"); | ||
img.src = await GM.getResourceUrl("logo"); | |||
document.body.appendChild(img); | |||
})(); | })(); | ||
</pre> | </pre> | ||
<!-- Not in 4.0! | |||
== See Also == | |||
* [[GM_getResourceText]] | |||
--> | |||
[[Category:API_Reference|G]] | [[Category:API_Reference|G]] |
Latest revision as of 05:10, 12 December 2019
Description
Given a defined @resource, this method returns it as a URL.
Compatibility: Greasemonkey 4.0+
Syntax
function GM.getResourceUrl( resourceName )
Arguments
resourceName
String
The name provided when the @resource was defined, follow that link for valid naming restrictions.
Returns
A Promise, rejected on failure and resolved with a String
URL on success.
Treat the result as opaque string.
It will work where you need a URL (for a <link>
or <style>
for CSS, for an <img>
tag, or similar).
Examples
// ==UserScript== // @name Image resource example // @resource logo ../icons/laptop.png // @grant GM.getResourceUrl // ==/UserScript== (async function() { let img = document.createElement("img"); img.src = await GM.getResourceUrl("logo"); document.body.appendChild(img); })();