欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

Tampermonkey 的存储 GM_setValue、GM_getValue 等。- GM_getValue(名称、默认值)

最编程 2024-05-11 21:15:20
...
Get the value of 'name' from the storage.

也简单,
GM_getValue('myName', 'woonigh')
就是把名字是 myName 的存储的值读取出来,
如果找不到myName的值呢?
那就用默认的值 'woonigh'

GM_listValues

官网:

GM_listValues()
List all names of the storage.

这个没有参数,就是把所有存储的名字罗列出来。
返回结果是 Array
这个一般也比较少用到。
一般的小脚本,都只有几个存储数据,谁心里还没点数~
甚至不用Tampermonkey的存储功能也是很正常的事。
需要用到这个功能的,只有2中情况

  • 你的应用很大,需要做存储数据管理
  • 你的应用数据很乱

我猜测,更多的情况是第二种。这个时候,你就要思考下怎样优化自己的应用了。
这个函数给我的感觉是,聊胜于无

GM_deleteValue

官网:

GM_deleteValue(name)
Deletes 'name' from storage.

望文生义,
假设代码 GM_deleteValue('myName')
就是删除名字叫做 myName 的存储。
和上面 GM_listValues 一样,多用于数据管理。两者是一对。
需要用到的情况也和上面的一样。需要想想是否需要优化自己的应用了。

值变动相关的功能

GM_addValueChangeListener

官网:

GM_addValueChangeListener(name, function(name, old_value, new_value, remote)
 Adds a change listener to the storage and returns the listener ID.
 'name' is the name of the observed variable.
 The 'remote' argument of the callback function shows whether this value was modified from the instance of another tab (true) or within this script instance (false).
 Therefore this functionality can be used by scripts of different browser tabs to
 communicate with each other.

翻译:
GM_addValueChangeListener(name, function(name, old_value, new_value, remote)
 对指定名字的存储值进行变动监听,并且返回监听器的ID
 回调函数的