GM.getResourceUrl: Difference between revisions

From GreaseSpot Wiki
Jump to navigationJump to search
(→‎Returns: note about double-encoding)
(→‎Returns: greasemonkey-script: protocol)
Line 19: Line 19:


<code>String</code>.
<code>String</code>.
A base64 encoded <code>data:</code> URI.
greasemonkey-script:[''script uuid'']/[''resource name'']
 
i.e.
 
greasemonkey-script:94242686-1400-4dce-982a-090cbfef7ba1/image
 
==== Prior to Greasemonkey 1.0 ====
 
Returns string containing a base64 encoded <code>data:</code> URI.


Note: This can be used anywhere a <code>data:</code> URI will work (E.G. <code><img></code> or <code><script></code>).
Note: This can be used anywhere a <code>data:</code> URI will work (E.G. <code><img></code> or <code><script></code>).

Revision as of 09:40, 30 December 2012


Description

Given a defined @resource, this method returns it as a URL.

Compatibility: Greasemonkey 0.8.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

String.

greasemonkey-script:[script uuid]/[resource name]

i.e.

greasemonkey-script:94242686-1400-4dce-982a-090cbfef7ba1/image

Prior to Greasemonkey 1.0

Returns string containing a base64 encoded data: URI.

Note: This can be used anywhere a data: URI will work (E.G. <img> or <script>). This does not include <object> or <embed>.

The result is a base64 encoded URI, which is then also URI encoded, as suggested by Wikipedia, because of "+" and "/" characters in the base64 alphabet. Thus, for certain usage, this URI encoding may need to be removed. [1]

Raises

Throws an Error when the named resource does not exist.

Examples

// ==UserScript==
// @resource logo http://www.example.com/logo.png
// ==/UserScript==

var img = document.createElement('img');
img.src = GM_getResourceURL("logo");
document.body.appendChild(img);

See Also