15

Does GreaseMonkey have something built in so you can store data per-site or per-page? For example, say you wanted to tweak StackOverflow.com so you can add a note to each of the questions in your favorites list and sort on that note. Does GreaseMonkey have something built-in to store those notes? Or perhaps can the scripts self-modify, such that you just define an array or object and keep the data there?

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794

3 Answers3

13

Yes - GM_setValue.

This method allows user script authors to persist simple values across page-loads. Strings, booleans, and integers are currently the only allowed data types

Sam Hasler
  • 12,344
  • 10
  • 72
  • 106
bdonlan
  • 224,562
  • 31
  • 268
  • 324
  • There's also a library called super_gm_setvalue that allows all types of JavaScript objects to be stored across domains. http://userscripts.org/scripts/show/107941 – Anderson Green Dec 20 '12 at 03:50
3

The values are restricted to the simple datatypes: string, boolean and integer. The values will be stored in Firefox preferences (located in about:config) which is not designed for storing huge amounts of data.

http://wiki.greasespot.net/GM_setValue

If GM_setValue doesn't cut it the linked question/answers shows other great possibilities: alternatives to GM_setValue

Community
  • 1
  • 1
Mads Hoel
  • 113
  • 5
0

It is really waranted to add that since this question was asked, new APIs were developed for persistent data storage.

Local storage

Holds string values only, non string values will be converted to string. You can use JSON or your own format to store objects.

Example:

localStorage.my_script_value = JSON.stringify([1,2,3,4]);

var my_parsed_value = JSON.parse(localStorage.my_script_value);

IndexedDB

More complex, but can hold more data - including binary blobs. Check the MDN article for details.

Example: Check this on MDN.

Tomáš Zato
  • 50,171
  • 52
  • 268
  • 778