GM.getResourceUrl: Difference between revisions

From GreaseSpot Wiki
Jump to navigationJump to search
(→‎Returns: note about double-encoding)
(→‎Syntax: use GM.* not GM_* now)
 
(3 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{{DISPLAYTITLE:GM_getResourceURL}}
== Description ==
== Description ==


Given a defined [[Metadata Block#.40resource|@resource]], this method returns it as a URL.
Given a defined [[Metadata Block#.40resource|@resource]], this method returns it as a URL.


Compatibility: [[Version_history#0.8.20080609.0|Greasemonkey 0.8.0+]]
Compatibility: [[Version_history#4.0_2|Greasemonkey 4.0+]]


== Syntax ==
== Syntax ==


{{Function|GM_getResourceURL|resourceName}}
{{Function|GM.getResourceUrl|resourceName}}


=== Arguments ===
=== Arguments ===
Line 18: Line 16:
=== Returns ===
=== Returns ===


<code>String</code>.
A [[Promise]], rejected on failure and resolved with a <code>String</code> URL on success.
A base64 encoded <code>data:</code> URI.
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).
Note: This can be used anywhere a <code>data:</code> URI will work (E.G. <code><img></code> or <code><script></code>).
This does not include <code><object></code> or <code><embed></code>.
 
<!-- A better reference for the reason for the double encoding would be awesome. -->
The result is a base64 encoded URI, which is then also URI encoded, as [http://en.wikipedia.org/wiki/Base64#URL_applications suggested by Wikipedia], because of "+" and "/" characters in the base64 alphabet.
Thus, for certain usage, this URI encoding may need to be removed.
[http://github.com/greasemonkey/greasemonkey/issues/issue/1151]
 
=== Raises ===
 
Throws an <code>Error</code> when the named resource does not exist.


== Examples ==
== Examples ==
Line 37: Line 24:
<pre class='sample'>
<pre class='sample'>
// ==UserScript==
// ==UserScript==
// @resource logo http://www.example.com/logo.png
// @name Image resource example
// @resource logo ../icons/laptop.png
// @grant GM.getResourceUrl
// ==/UserScript==
// ==/UserScript==


var img = document.createElement('img');
(async function() {
img.src = GM_getResourceURL("logo");
let img = document.createElement("img");
img.src = await GM.getResourceUrl("logo");
document.body.appendChild(img);
document.body.appendChild(img);
})();
</pre>
</pre>


<!-- Not in 4.0!
== See Also ==
== See Also ==


* [[GM_getResourceText]]
* [[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);
})();