简体   繁体   English

在Greasemonkey脚本中存储数据

[英]Storing data in Greasemonkey scripts

Does GreaseMonkey have something built in so you can store data per-site or per-page? GreaseMonkey是否内置了一些内容,因此您可以按站点或每页存储数据? For example, say you wanted to tweak StackOverflow.com so you can add a note to each of the questions in your favorites list and sort on that note. 例如,假设您想要调整StackOverflow.com,以便您可以为收藏夹列表中的每个问题添加注释,并对该注释进行排序。 Does GreaseMonkey have something built-in to store those notes? GreaseMonkey是否有内置的东西来存储这些笔记? Or perhaps can the scripts self-modify, such that you just define an array or object and keep the data there? 或者脚本可以自我修改,这样您只需定义一个数组或对象并将数据保存在那里?

Yes - GM_setValue . 是的 - GM_setValue

This method allows user script authors to persist simple values across page-loads. 此方法允许用户脚本作者在页面加载中保留简单值。 Strings, booleans, and integers are currently the only allowed data types 字符串,布尔值和整数是目前唯一允许的数据类型

The values are restricted to the simple datatypes: string, boolean and integer. 值仅限于简单数据类型:string,boolean和integer。 The values will be stored in Firefox preferences (located in about:config) which is not designed for storing huge amounts of data. 这些值将存储在Firefox首选项(位于about:config中)中,该首选项不是为存储大量数据而设计的。

http://wiki.greasespot.net/GM_setValue http://wiki.greasespot.net/GM_setValue

If GM_setValue doesn't cut it the linked question/answers shows other great possibilities: alternatives to GM_setValue 如果GM_setValue没有删除它,链接的问题/答案显示了其他很大的可能性: GM_setValue的替代品

It is really waranted to add that since this question was asked, new APIs were developed for persistent data storage. 真的有必要补充说,自从提出这个问题以来,为持久性数据存储开发了新的API。

Local storage 本地存储

Holds string values only, non string values will be converted to string. 仅保留字符串值,非字符串值将转换为字符串。 You can use JSON or your own format to store objects. 您可以使用JSON或您自己的格式来存储对象。

Example: 例:

localStorage.my_script_value = JSON.stringify([1,2,3,4]);

var my_parsed_value = JSON.parse(localStorage.my_script_value);

IndexedDB IndexedDB的

More complex, but can hold more data - including binary blobs. 更复杂,但可以容纳更多数据 - 包括二进制blob。 Check the MDN article for details. 有关详细信息,请查看MDN文章。

Example: Check this on MDN . 示例: 在MDN上检查此项

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM