GM.setValue: Difference between revisions

From GreaseSpot Wiki
Jump to navigationJump to search
(Symmetry update with a bug reported)
(replace _ with .)
 
(47 intermediate revisions by 8 users not shown)
Line 1: Line 1:
{{underscore|title=GM_setValue}}
__NOTOC__
== Description ==
== Description ==


This [[API_reference|API]] method allows user script authors to persist simple values locally.
This method allows user script authors to persist simple values across page loads and across origins.
 
Strings, booleans, and integers are currently the only allowed data types. This places limitations on the size of an individual value, and the number of values.<sup>[[#Notes|[1]]]</sup>
 
Values are saved in the [http://developer.mozilla.org/en/docs/Code_snippets:Preferences Firefox preferences] back end and can be manually changed by typing <code>about:config</code> in the address bar and searching for the preference name "greasemonkey.scriptval.''script-namespace''/''script-name''".


[[#Examples|Examples]] | [[#Notes|Notes]] | [[#See_Also|See Also]]
Strings, booleans, and integers are currently the only allowed data types.


== Syntax ==
== Syntax ==


'''GM_setValue(''' ''name'', ''value'' ''')'''
{{Function|GM.setValue|name, value}}
 
:Returns: Nothing<sup>[[#Notes|[2]]]</sup>
:Compatibility: Greasemonkey 0.3b+
 
:{| border="1" cellpadding="5"
|+ Parameters
!|'''Properties'''
|-
| <code>[[#name |name]]</code>
|-
|<code>[[#value |value]]</code>
|}
:* All properties are optional except [[#name|name]] and [[#value|value]].
 
[[#Description|top]]
=== Properties ===
----
==== <code>name</code> ====
:Value: String
:Usage: <code>name = "PropertyName";</code>


:* Property name to retrieve or create.
Compatibility: [[Version_history#4.0_2|Greasemonkey 4.0+]]


[[#Description|top]] | [[#Syntax|back]]''
=== Arguments ===


==== <code>value</code> ====
; <code>name</code>
:Value: String, Integer or Boolean
: <code>String</code> The unique (within this script) name for this value.  Should be restricted to valid Javascript identifier characters.
:Usage: <code>value = 5;</code>
; <code>value</code>
: <code>String</code>, <code>Integer</code> or <code>Boolean</code> Any valid value of these types.  Any other type may cause undefined behavior, including crashes.


[[#Description|top]] | [[#Syntax|back]]
=== Returns ===


= Examples =
A [[Promise]], resolved successfully with no value on success, rejected with no value on failure.
<code><pre>
GM_setValue("foo", "bar");
</pre></code>


[[#Description|top]]
== Examples ==


= Notes =
Set the name foo to hold the value bar:
<sup>[1]</sup>This statement may not be accurate.  Please see [http://groups.google.com/group/greasemonkey-users/t/ca5a0dfac5c5998b this mailing list thread] for details.


<sup>[2]</sup> The previous incarnation of this article had an example of <code>alert(GM_setValue("foo", "bar"));</code>.  Currently miscapis.js and prefmanager.js do not have any <code>return</code> statements.
<pre class='sample'>
GM.setValue("foo", "bar");
</pre>


[[#Description|top]]
== See Also ==


= See Also =
* [[GM.getValue]]
* [[GM_getValue]]
* [[GM.deleteValue]]
* [[GM.listValues]]


[[#Description|top]]
[[Category:API_Reference|S]]
[[Category:API_Reference|G]]

Latest revision as of 00:26, 19 September 2018

Description

This method allows user script authors to persist simple values across page loads and across origins.

Strings, booleans, and integers are currently the only allowed data types.

Syntax

function GM.setValue( name, value )

Compatibility: Greasemonkey 4.0+

Arguments

name
String The unique (within this script) name for this value. Should be restricted to valid Javascript identifier characters.
value
String, Integer or Boolean Any valid value of these types. Any other type may cause undefined behavior, including crashes.

Returns

A Promise, resolved successfully with no value on success, rejected with no value on failure.

Examples

Set the name foo to hold the value bar:

GM.setValue("foo", "bar");

See Also