如何/在哪里存储数据在Chrome Tampermonkey脚本中? [英] How/Where to store data in a Chrome Tampermonkey script?

查看:961
本文介绍了如何/在哪里存储数据在Chrome Tampermonkey脚本中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为Facebook写了一个Greasemonkey / Tampermonkey脚本。我需要存储数据以便稍后检索它。为此,我使用了 localStorage

这工作正常。但我注意到,几个小时后,我存储的所有数据都被删除了automagicllay。 Facebook本身可能会删除所有localStorage数据。



现在,我搜索了替代品。


  1. Cookie :否用户清除历史记录后将删除。

  2. 网络SQL :显然它被w3.org丢弃了。所以在不久的将来,我认为chrome可能不会使用web sql。

我想将数据存储在客户端系统中。我有什么选择?我应该使用FileSystem来存储数据吗?

解决方案

由于您使用的是 Tampermonkey (Chrome)和Greasemonkey(Firefox)。继续使用 GM_setValue() 。它不能被Facebook或任何其他网站清除。



它具有跨域存储值的优势。



~~~

请注意, Firefox 中的标准 GM_setValue() 。它可能导致脚本实例在无效值上崩溃 - 因此最好使用序列化程序,例如 GM_SuperValue ,用于存储除字符串以外的任何内容。即使天真无邪的整数可能导致默认的 GM_setValue()崩溃。



目前,只有 GM_setValue(),Cookie,localStorage和 IndexedDB 可用于持久数据浏览器。

IndexedDB也可能做你想做的事,但它不如 GM_setValue()




更新:

如今,别忘了使用:




  • // @grant GM_setValue


  • // @grant GM_getValue



另外,如果你请使用GM_SuperValue库,现在将它添加到:

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

在元数据块中。 (因为userscripts.org已经很长时间了。)


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 automagicllay. Probably Facebook itself deletes all localStorage data.

Now, 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 client system. What option do I have? Should I use FileSystem to store data?

解决方案

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

这篇关于如何/在哪里存储数据在Chrome Tampermonkey脚本中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆