GM.getResourceText: Difference between revisions

From GreaseSpot Wiki
Jump to navigationJump to search
m (Text replace - "[[Metadata_block" to "[[Metadata Block")
(simplify)
Line 3: Line 3:
== Description ==
== Description ==


This [[API_reference|API]] method loads an external resource, such as an external JavaScript routine, and returns the string for use in the DOM.
Given a defined [[Metadata Block#.40resource|@resource]], this method returns it as a string.
 
Compatibility: [[Version_history#0.8.20080609.0|Greasemonkey 0.8.0+]]


== Syntax ==
== Syntax ==


'''GM_getResourceText(''' ''resourceName'' ''')'''
{{Function|GM_getResourceText|resourceName}}
 
== Arguments ==
 
; resourceName
: <code>String</code> The name provided when the [[Metadata Block#.40resource|@resource]] was defined, follow that link for valid naming restrictions.


:Value: Function
== Returns ==
:Returns: String
:Compatibility: [[Version_history#0.8.20080609.0|Greasemonkey 0.8.0+]]


:{| cellpadding="5" style="border-style:solid; background:#FFFFE0;"
<code>String</code>
|+ Parameters
!style="background:#CC9900;"|'''Properties'''
|-
| <code><span style="background:#FFFFE0;">[[#resourceName |resourceName]]</span></code>
|}
:* All properties are optional except [[#resourceName|resourceName]].


=== Properties ===
== Raises ==
----
==== <code>resourceName</code> ====
:Value: String
:Usage: <code>'''resourceName''' = "metadata_block_resourceName";</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.
:* This value is used in retrieval of the same-named resource specified in the corresponding metadata block [[Metadata Block#.40resource |@resource]] key. After it is retrieved, no formatting on the resource will occur, and it will be returned upon completion of this API method.


Throws an <code>Error</code> when the named resource does not exist.


== Examples ==
== Examples ==
<pre class='sample'>
<pre class='sample'>
// ==UserScript==
// ==UserScript==
// @name          My Script
// @resource prototype http://www.example.com/prototype.js
// @namespace    <nowiki>http://www.example.com/gmscripts/</nowiki>
// @description  Scripting is fun
// @include      <nowiki>http://www.example.com/*</nowiki>
// @resource     prototype http://www.example.com/prototype.js
// ==/UserScript==
// ==/UserScript==


(function() {
var prototypeSource = GM_getResourceText("prototype");
  // some code
 
  var myPrototype = GM_getResourceText("prototype");
 
  // some code
})();
</pre>
</pre>


[[Category:API_Reference|G]]
[[Category:API_Reference|G]]

Revision as of 21:43, 8 February 2010


Description

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

Compatibility: Greasemonkey 0.8.0+

Syntax

function GM_getResourceText( resourceName )

Arguments

resourceName
String The name provided when the @resource was defined, follow that link for valid naming restrictions.

Returns

String

Raises

Throws an Error when the named resource does not exist.

Examples

// ==UserScript==
// @resource prototype http://www.example.com/prototype.js
// ==/UserScript==

var prototypeSource = GM_getResourceText("prototype");