Joel Coehoorn
Joel Coehoorn

Reputation: 415725

Storing data in Greasemonkey scripts

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?

Upvotes: 16

Views: 12169

Answers (3)

Tomáš Zato
Tomáš Zato

Reputation: 53129

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.

Upvotes: 1

bdonlan
bdonlan

Reputation: 231113

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

Upvotes: 13

Mads Hoel
Mads Hoel

Reputation: 163

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

Upvotes: 4

Related Questions