javascript 如何/在哪里在 Chrome Tampermonkey 脚本中存储数据?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15730216/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 01:52:29  来源:igfitidea点击:

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

javascriptgoogle-chromelocal-storagegreasemonkeytampermonkey

提问by Rakesh Juyal

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.

我为 Facebook 编写了一个 Greasemonkey/Tampermonkey 脚本。我需要存储数据以便以后检索。为此,我使用了localStorage.
那工作得很好。但是我注意到几个小时后我存储的所有数据都被自动删除了。可能 Facebook 本身会删除所有 localStorage 数据。

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.
  1. Cookies: 不,当用户清除历史记录时,这将被删除。
  2. Web SQL:显然它被 w3.org 删除了。所以在不久的将来,我认为 chrome 可能也不会使用 web sql。

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

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

回答by Brock Adams

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.

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

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.

~~~
请注意,沼泽标准GM_setValue()Firefox上有些问题。它可能导致脚本实例因无效值而崩溃——因此最好使用序列化程序(例如GM_SuperValue)来存储除字符串之外的任何内容。即使是看似无辜的整数也会导致默认GM_setValue()崩溃。

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

目前,只有GM_setValue()、cookies、localStorage 和IndexedDB可用于两种浏览器上的持久数据。

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

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



Update:
Nowadays, don't forget to use:

更新:
如今,不要忘记使用:

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

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

此外,如果您确实使用了 GM_SuperValue 库,您现在可以添加:

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

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

在元数据块中。(因为 userscripts.org 早已死了。)