相關(guān)函數(shù)四個(gè):
GM_setValue
GM_getValue
GM_listValues
GM_deleteValue
當(dāng)然,還有存儲(chǔ)內(nèi)容變動(dòng)時(shí)相關(guān)的函數(shù)
GM_addValueChangeListener
GM_removeValueChangeListener
GM_setValue
官網(wǎng)用法:
GM_setValue(name, value)
Set the value of 'name' to the storage.
翻譯······
其實(shí)也沒(méi)有必要翻譯了,一看就懂。
假設(shè)代碼 GM('myName', 'woonigh')
在Tampermonkey管理的存儲(chǔ)中,把名字是myName的存儲(chǔ),設(shè)置值為 'woonigh'。
GM_getValue
官方用法
GM_getValue(name, defaultValue)
Get the value of 'name' from the storage.
也簡(jiǎn)單,
GM_getValue('myName', 'woonigh')
就是把名字是 myName 的存儲(chǔ)的值讀取出來(lái),
如果找不到myName的值呢?
那就用默認(rèn)的值 'woonigh'
GM_listValues
官網(wǎng):
GM_listValues()
List all names of the storage.
這個(gè)沒(méi)有參數(shù),就是把所有存儲(chǔ)的名字羅列出來(lái)。
返回結(jié)果是 Array
這個(gè)一般也比較少用到。
一般的小腳本,都只有幾個(gè)存儲(chǔ)數(shù)據(jù),誰(shuí)心里還沒(méi)點(diǎn)數(shù)~
甚至不用Tampermonkey的存儲(chǔ)功能也是很正常的事。
需要用到這個(gè)功能的,只有2中情況
- 你的應(yīng)用很大,需要做存儲(chǔ)數(shù)據(jù)管理
- 你的應(yīng)用數(shù)據(jù)很亂
我猜測(cè),更多的情況是第二種。這個(gè)時(shí)候,你就要思考下怎樣優(yōu)化自己的應(yīng)用了。
這個(gè)函數(shù)給我的感覺(jué)是,聊勝于無(wú)
GM_deleteValue
官網(wǎng):
GM_deleteValue(name)
Deletes 'name' from storage.
望文生義,
假設(shè)代碼 GM_deleteValue('myName')
就是刪除名字叫做 myName 的存儲(chǔ)。
和上面 GM_listValues 一樣,多用于數(shù)據(jù)管理。兩者是一對(duì)。
需要用到的情況也和上面的一樣。需要想想是否需要優(yōu)化自己的應(yīng)用了。
值變動(dòng)相關(guān)的功能
GM_addValueChangeListener
官網(wǎng):
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)
對(duì)指定名字的存儲(chǔ)值進(jìn)行變動(dòng)監(jiān)聽(tīng),并且返回監(jiān)聽(tīng)器的ID
回調(diào)函數(shù)的 remote 參數(shù)用于區(qū)分這個(gè)值的改變,是本標(biāo)簽頁(yè)(false)引起的,還是其他標(biāo)簽頁(yè)(true)引起的
因此,本功能可以被同一個(gè)瀏覽器的不同標(biāo)簽頁(yè)之間的腳本使用,進(jìn)行交流
對(duì)于這個(gè)功能,應(yīng)用場(chǎng)景文檔已經(jīng)說(shuō)的非常清楚了。
就是不同標(biāo)簽之間交流溝通的。數(shù)據(jù)和狀態(tài)變動(dòng)都可以。
GM_removeValueChangeListener
官網(wǎng):
GM_removeValueChangeListener(listener_id)
Removes a change listener by its ID.
翻譯
GM_removeValueChangeListener(listener_id)
根據(jù)監(jiān)聽(tīng)器的 ID,把監(jiān)聽(tīng)器移除
如果不熟悉的孩子,可能有些疑惑,這個(gè) listener_id 是什么玩意?哪里來(lái)的?
這就要和前面 GM_addValueChangeListener 功能對(duì)應(yīng)起來(lái)了,它的返回值就是一個(gè)監(jiān)聽(tīng)的的ID。所以 這倆貨是要配對(duì)使用的。
示例代碼
乖o(*^@^*)o,拿著叔叔的代碼,一邊玩去吧~~
// 存儲(chǔ)中 增加一個(gè)存儲(chǔ)量,名字叫 'myName', 值是'woonigh'
let my_name = GM_setValue('myName', 'Woonigh');
// 讀取存儲(chǔ)中的,名字叫 'hisName' 的值, 如果沒(méi)有這個(gè)名字的存儲(chǔ)的話,那返回來(lái)的值就叫 'Tony'
let his_name = GM_getValue('hisName', 'Tony');
// 把存儲(chǔ)中所有的存儲(chǔ)名字羅列出來(lái)
let all_value_names = GM_listValues();
console.log(all_value_names); // 輸出 ["myName"]
// 給名字叫 'myName' 的存儲(chǔ)添加一個(gè)監(jiān)聽(tīng)器
let listener1 = GM_addValueChangeListener('myName', function (name, old_value, new_value, remote) {
console.log(`
發(fā)生變化的存儲(chǔ)名是: ${name},
${name} 原來(lái)的值是 ${old_value},
${name} 新的值是 ${new_value},
這個(gè)值的變動(dòng)是由${remote ? '本標(biāo)簽頁(yè)' : '其他標(biāo)簽頁(yè)'} 引起的。
`);
});
GM_setValue('myName', 'Maxwell');
// 把 id 是listener1 的監(jiān)聽(tīng)器移除
GM_removeValueChangeListener(listener1);
// 把名字叫 'myNamae' 的存儲(chǔ)刪除掉
GM_deleteValue('myName');