48

I wrote one Greasemonkey/Tampermonkey script for Facebook . I needed to store data to retrieve it later. For that I used localStorage.

That was working fine. But I noticed that after few hours all data which I stored was removed automagically. Probably Facebook itself deletes all localStorage data.

I searched for alternatives.

  1. Cookies : No this will be removed when user clears history.
  2. Web SQL : Apparently it is dropped by w3.org. So in near future I assume chrome might not be using web sql too.

I want to store the data in the client system. What options do I have? Should I use FileSystem to store data?

ggorlen
  • 44,755
  • 7
  • 76
  • 106
Rakesh Juyal
  • 35,919
  • 68
  • 173
  • 214
  • It depends on type of data. For raw binary data, you can use data [Blob API](http://www.nczonline.net/blog/2012/06/05/working-with-files-in-javascript-part-5-blobs/) . For caching more data, you can use localStorage API too. And it's [compatible](http://caniuse.com/namevalue-storage) with most of the browsers. – smitrp Mar 31 '13 at 16:00
  • As I mention I am using localStorage, but this is getting cleared after every few hours. – Rakesh Juyal Apr 01 '13 at 06:00
  • Tampermonkey, a Chrome userscript and Greasemonkey are related but not the same thing. This question was a bit ambiguous about what you were using. I'm assuming that you are using the Tampermonkey extension on Chrome (you should be, it's way better than a straight Chrome userscript). – Brock Adams Apr 03 '13 at 10:26
  • I don't know how useful it will be but you can have a look at this. http://goo.gl/CQA1h – Jigar Patel Apr 09 '13 at 22:58

1 Answers1

78

Since you are using Tampermonkey (Chrome) and Greasemonkey (Firefox). Go ahead and use GM_setValue(). It cannot be cleared by Facebook or by any other website.

It has the advantage of storing values cross-domain, as well.

~~~
Beware that the bog-standard GM_setValue() is somewhat problematic on Firefox. It can cause a script instance to crash on invalid values -- So it's best to use a serializer, such as GM_SuperValue, to store anything but strings. Even innocent-looking integers can cause the default GM_setValue() to crash.

Currently, only GM_setValue(), cookies, localStorage, and IndexedDB are available for persistent data on both browsers.

IndexedDB would also probably do what you want, but it is nowhere as easy to use as GM_setValue().


Update:
Nowadays, don't forget to use:

  • // @grant GM_setValue
    and
  • // @grant GM_getValue

Also, if you do use the GM_SuperValue library, you would now add it with:

// @require http://userscripts-mirror.org/scripts/source/107941.user.js 

in the metadata block. (Since userscripts.org is long dead.)

Community
  • 1
  • 1
Brock Adams
  • 90,639
  • 22
  • 233
  • 295
  • 2
    Note 1: `GM_SuperValue` not exist on Tampermonkey (Chrome), at least on 2017.01.24 chrome 55.0.2883.87 m (64-bit). – Mithril Jan 24 '17 at 02:13
  • 1
    Note 2: Must add `// @grant GM_setValue` on userscript header to use the magic. – Mithril Jan 24 '17 at 02:15
  • @Mithril, In order to use `GM_SuperValue` in either Tampermonkey or any other compatible script engine, you must `@require` it as shown. Also you need *two* `@grants`. One for `GM_setValue` and one for `GM_getValue`. – Brock Adams Jan 24 '17 at 02:27
  • @BrockAdams The script doesn't work cross origin. Not sure of the reason, though. – kvn Mar 06 '17 at 06:41
  • I'm assuming that TamperMonkey/GreaseMonkey would retains the memory even if the page is redirected to a different domain/page. Am I right? – kvn Mar 06 '17 at 06:47
  • @SGSVenkatesh, this script and GM_setValue store in a space that is unique to each `@name` and `@namespace` combination (except Tampermonkey may still only key off the `@name`). So, everything is cross domain, if the userscript is. And yes the values are retained across redirects as long as the same script fires across the redirects (as set by the match/include directives). ...If something is not working for you, post an [MCVE]. And, it probably needs to be in a new question. – Brock Adams Mar 06 '17 at 06:55
  • 1
    Thanks. But is there any way to have global storage space which can be accessed across multiple scripts? – kvn Mar 06 '17 at 07:13
  • 1
    @SGSVenkatesh, the only way to do that is to either host your own data app or use something with an API like Google docs. That is, to store the data externally in something that the multiple scripts can read/write over http(s). – Brock Adams Mar 06 '17 at 08:21