How/Where to store data in a Chrome Tampermonkey script other than localStorage?

33,139

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.)

Share:
33,139

Related videos on Youtube

Rakesh Juyal
Author by

Rakesh Juyal

Actively seeking new opportunities.

Updated on July 09, 2022

Comments

  • Rakesh Juyal
    Rakesh Juyal almost 2 years

    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?

    • smitrp
      smitrp about 11 years
      It depends on type of data. For raw binary data, you can use data Blob API . For caching more data, you can use localStorage API too. And it's compatible with most of the browsers.
    • Rakesh Juyal
      Rakesh Juyal about 11 years
      As I mention I am using localStorage, but this is getting cleared after every few hours.
    • Brock Adams
      Brock Adams about 11 years
      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).
    • Jigar Patel
      Jigar Patel about 11 years
      I don't know how useful it will be but you can have a look at this. goo.gl/CQA1h
  • Mithril
    Mithril over 7 years
    Note 1: GM_SuperValue not exist on Tampermonkey (Chrome), at least on 2017.01.24 chrome 55.0.2883.87 m (64-bit).
  • Mithril
    Mithril over 7 years
    Note 2: Must add // @grant GM_setValue on userscript header to use the magic.
  • Brock Adams
    Brock Adams over 7 years
    @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.
  • kvn
    kvn about 7 years
    @BrockAdams The script doesn't work cross origin. Not sure of the reason, though.
  • kvn
    kvn about 7 years
    I'm assuming that TamperMonkey/GreaseMonkey would retains the memory even if the page is redirected to a different domain/page. Am I right?
  • Brock Adams
    Brock Adams about 7 years
    @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 minimal reproducible example. And, it probably needs to be in a new question.
  • kvn
    kvn about 7 years
    Thanks. But is there any way to have global storage space which can be accessed across multiple scripts?
  • Brock Adams
    Brock Adams about 7 years
    @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).