GM.getValue: Difference between revisions
From GreaseSpot Wiki
Jump to navigationJump to search
m Reverted edits by Fatman12 (Talk) to last revision by 151.20.82.250 |
added descriptions to code examples; added extra usage samples; |
||
Line 30: | Line 30: | ||
== Examples == | == Examples == | ||
Retrieving the value associated with the name 'foo': | |||
<pre class='sample'> | <pre class='sample'> | ||
GM_log(GM_getValue("foo")); | //outputs either the value associated to foo, else undefined | ||
GM_log(GM_getValue("foo")); | |||
</pre> | |||
Retrieving the value associated with the name 'timezoneOffset' with a default value defined: | |||
<pre class='sample'> | |||
//GM_getValue() returns the value -5 (integer) if no value | |||
// associated with the name timezoneOffset is found in storage | |||
GM_log(GM_getValue("timezoneOffset", -5)); | |||
</pre> | |||
A more complex example: | |||
If you have used JSON.stringify to [http://wiki.greasespot.net/Talk:Greasemonkey_Manual:API#GM_getObject.2FGM_setObject place an object into storage], JSON.parse shall be used to convert it back from a string to a [http://www.w3schools.com/js/js_objects.asp Javascript Object] | |||
<pre class='sample'> | |||
//Note that if the value associated with foo is an invalid | |||
// JSON string, JSON.parse will fail. | |||
// Also Note the default value is in quotes (thus a string) rather than an object literal | |||
var storedObject = JSON.parse( GM_getValue("foo", "{}") ); | |||
if(!storedObject) { | |||
//JSON.parse() should never return any value that type-casts to false, assume there is an | |||
// error in the input string | |||
GM_log('Error! JSON.parse failed - The stored value for "foo" is likely to be corrupted.'); | |||
throw; | |||
} | |||
</pre> | </pre> | ||
Revision as of 01:16, 11 May 2011
Description
This method retrieves a value that was set with GM_setValue. See GM_setValue for details on the storage of these values.
Compatibility: Greasemonkey 0.3b+
Syntax
function GM_getValue( name, default )
Arguments
name
String
The property name to get. See GM_setValue for details.default
- Optional. Any value to be returned, when no value has previously been set.
Returns
- When this
name
has been set String
,Integer
orBoolean
as previously set- When this
name
has not been set, anddefault
is provided - The value passed as
default
- When this
name
has not been set, anddefault
is not provided undefined
Examples
Retrieving the value associated with the name 'foo':
//outputs either the value associated to foo, else undefined GM_log(GM_getValue("foo"));
Retrieving the value associated with the name 'timezoneOffset' with a default value defined:
//GM_getValue() returns the value -5 (integer) if no value // associated with the name timezoneOffset is found in storage GM_log(GM_getValue("timezoneOffset", -5));
A more complex example:
If you have used JSON.stringify to place an object into storage, JSON.parse shall be used to convert it back from a string to a Javascript Object
//Note that if the value associated with foo is an invalid // JSON string, JSON.parse will fail. // Also Note the default value is in quotes (thus a string) rather than an object literal var storedObject = JSON.parse( GM_getValue("foo", "{}") ); if(!storedObject) { //JSON.parse() should never return any value that type-casts to false, assume there is an // error in the input string GM_log('Error! JSON.parse failed - The stored value for "foo" is likely to be corrupted.'); throw; }