GM.getResourceUrl: Difference between revisions

From GreaseSpot Wiki
Jump to navigationJump to search
m (→‎<code>resourceName</code>: Some redundancy dumped)
m (→‎Examples: Neutralized the example)
Line 38: Line 38:
<code><pre>
<code><pre>
// ==UserScript==
// ==UserScript==
// @name           I CAN HAS MOCHIKIT?
// @name         My Script
// @namespace     http://youngpup.net/userscripts
// @namespace     <nowiki>http://www.example.com/gmscripts/</nowiki>
// @description   Simple library loading test which makes the Google logo pulsate.
// @description   Scripting is fun
// @include       http://www.google.com/
// @include       <nowiki>http://www.example.com/*</nowiki>
// @require        mochikit-packed.js
// @resource     fooLogo http://www.example.com/logo.png
// @resource       better-logo http://wiki.greasespot.net/skins/common/images/gm-wiki-logo.png
// ==/UserScript==
// ==/UserScript==


/* Go to http://www.google.com to see this in action. */
(function()
{
  // some code


var logo = document.evaluate(
  var fooLogo = GM_getResourceURL("fooLogo");
  '/html/body/center/img', document, null, 9, null
).singleNodeValue;


logo.src = GM_getResourceURL("better-logo");
  // some code
pulsate(logo);
 
})();
</pre></code>
</pre></code>



Revision as of 07:49, 12 December 2007

Template:Underscore


Description

This API method loads an external resource, such as an image, and returns the string containing the base64 encoded data: URL scheme for use in the DOM.

Examples | Notes

Syntax

GM_getResourceURL( resourceName )

Value: Function
Returns: String
Compatibility: Greasemonkey 0.8.0+
Parameters
Properties
resourceName

top

Properties


resourceName

Value: String
Usage: resourceName = "metadata-resource-name[1]";
  • This value is used in retrieval of the same named resource specified in the corresponding metadata block @resource key. After it is retrieved, it will be base64 encoded into the data: URL scheme, and returned upon completion of this API method.

top | back

Examples

// ==UserScript==
// @name          My Script
// @namespace     http://www.example.com/gmscripts/
// @description   Scripting is fun
// @include       http://www.example.com/*
// @resource      fooLogo http://www.example.com/logo.png
// ==/UserScript==

(function()
{
  // some code

  var fooLogo = GM_getResourceURL("fooLogo");

  // some code

})();

top

Notes

[1] While the variable names here are non-semantic, it is assumed that it should be compatible with JavaScript variable naming conventions and XML/CSS naming conventions to help keep things consistent.

top