Difference between revisions of "GM.setValue"

From GreaseSpot Wiki
Jump to navigationJump to search
(Updated information about where data is stored.)
(replace _ with .)
 
(5 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{{DISPLAYTITLE:GM_setValue}}
== Description ==
== Description ==


This method allows user script authors to persist simple values across page-loads.
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.  
Strings, booleans, and integers are currently the only allowed data types.  


Values are saved per userscript in a [https://developer.mozilla.org/en-US/docs/Storage SQLite] database. You can find the corresponding database by the userscript <code>basedir</code> as defined in the <code>config.xml</code>. The values are only accessible by opening the database is an SQLite reader, like add-on [https://addons.mozilla.org/en-US/firefox/addon/sqlite-manager/ SQLite Manager]. This is only available from [[Version_history#1.13|version 1.13+]] available.
== Syntax ==


In older Greasemonkey version, values are saved in the [https://developer.mozilla.org/en/Code_snippets/Preferences Firefox preferences] back end and can be manually inspected or changed by typing [[mozillazine:About:config|about:config]] in the address bar and searching for the preference name "<code>greasemonkey.scriptvals.[[Metadata Block#.40namespace|namespace]]/[[Metadata Block#.40name|script_name]].value_name</code>".
{{Function|GM.setValue|name, value}}
(For appropriate values of <code>namespace</code>, <code>script_name</code> and <code>value_name</code>.)


The Firefox preference store is not designed for storing large amounts of data. There are no hard limits, but very large amounts of data may cause Firefox to consume more memory and/or run more slowly. <ref>Please see [http://groups.google.com/group/greasemonkey-users/t/ca5a0dfac5c5998b this mailing list thread] for details.</ref>
Compatibility: [[Version_history#4.0_2|Greasemonkey 4.0+]]
Integer preferences must be in the range -2<sup>31</sup>-1 to 2<sup>31</sup>-1. Numbers outside this range can be stored by using <code>.toString()</code> before <code>GM_setValue</code>. <ref>"Note: Preferences cannot store non-integer numbers or numbers outside the signed 32-bit range -(2^31-1) to 2^31-1, If you have such a number, store it as a string by calling toString() on the number before passing it to this method" [https://github.com/mozilla/mozilla-central/blob/master/services/common/preferences.js#L86|Firefox's source code on Github]</ref>
 
Compatibility: [[Version_history#0.3_beta|Greasemonkey 0.3b+]]
 
== Syntax ==
 
{{Function|GM_setValue|name, value}}


=== Arguments ===
=== Arguments ===
Line 30: Line 20:
=== Returns ===
=== Returns ===


<code>undefined</code>
A [[Promise]], resolved successfully with no value on success, rejected with no value on failure.


== Examples ==
== Examples ==


Set the name foo to hold the value bar:
Set the name foo to hold the value bar:
<pre class='sample'>
<pre class='sample'>
GM_setValue("foo", "bar");
GM.setValue("foo", "bar");
</pre>
</pre>


== See Also ==
== See Also ==


* [[GM_getValue]]
* [[GM.getValue]]
* [[GM_deleteValue]]
* [[GM.deleteValue]]
* [[GM_listValues]]
* [[GM.listValues]]
* [[:Category:Coding Tips:Persistence]]
 
== References ==
 
<references/>


[[Category:API_Reference|S]]
[[Category:API_Reference|S]]

Latest revision as of 19:26, 18 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