Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
843 views
in Technique[技术] by (71.8m points)

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...