处理后台脚本API
This commit is contained in:
18
example/gm_value/gm_value_1.js
Normal file
18
example/gm_value/gm_value_1.js
Normal file
@ -0,0 +1,18 @@
|
||||
// ==UserScript==
|
||||
// @name gm value storage 设置方
|
||||
// @namespace https://bbs.tampermonkey.net.cn/
|
||||
// @version 0.1.0
|
||||
// @description 多个脚本之间共享数据 设置方
|
||||
// @author You
|
||||
// @match https://bbs.tampermonkey.net.cn/
|
||||
// @run-at document-start
|
||||
// @grant GM_setValue
|
||||
// @grant GM_deleteValue
|
||||
// @storageName example
|
||||
// ==/UserScript==
|
||||
|
||||
setTimeout(() => {
|
||||
GM_deleteValue("test_set");
|
||||
}, 3000);
|
||||
|
||||
GM_setValue("test_set", new Date().getTime());
|
17
example/gm_value/gm_value_1_bg.js
Normal file
17
example/gm_value/gm_value_1_bg.js
Normal file
@ -0,0 +1,17 @@
|
||||
// ==UserScript==
|
||||
// @name gm value storage 设置方 - 定时脚本
|
||||
// @namespace https://bbs.tampermonkey.net.cn/
|
||||
// @version 0.1.0
|
||||
// @description 多个脚本之间共享数据 设置方 - 定时脚本
|
||||
// @author You
|
||||
// @run-at document-start
|
||||
// @grant GM_setValue
|
||||
// @grant GM_deleteValue
|
||||
// @storageName example
|
||||
// @crontab */5 * * * * *
|
||||
// ==/UserScript==
|
||||
|
||||
return new Promise((resolve) => {
|
||||
GM_setValue("test_set", new Date().getTime());
|
||||
resolve();
|
||||
});
|
28
example/gm_value/gm_value_2.js
Normal file
28
example/gm_value/gm_value_2.js
Normal file
@ -0,0 +1,28 @@
|
||||
// ==UserScript==
|
||||
// @name gm value storage 读取与监听方
|
||||
// @namespace https://bbs.tampermonkey.net.cn/
|
||||
// @version 0.1.0
|
||||
// @description 多个脚本之间共享数据 读取与监听方
|
||||
// @author You
|
||||
// @match https://bbs.tampermonkey.net.cn/
|
||||
// @run-at document-start
|
||||
// @grant GM_getValue
|
||||
// @grant GM_addValueChangeListener
|
||||
// @grant GM_listValues
|
||||
// @grant GM_cookie
|
||||
// @storageName example
|
||||
// ==/UserScript==
|
||||
|
||||
GM_addValueChangeListener("test_set", function (name, oldval, newval, remote, tabid) {
|
||||
console.log("test_set change", name, oldval, newval, remote, tabid);
|
||||
// 可以通过tabid获取到触发变化的tab
|
||||
// GM_cookie.store可以获取到对应的cookie storeId
|
||||
GM_cookie("store", tabid, (storeId) => {
|
||||
console.log("store", storeId);
|
||||
});
|
||||
});
|
||||
|
||||
setInterval(() => {
|
||||
console.log("test_set: ", GM_getValue("test_set"));
|
||||
console.log("value list:", GM_listValues());
|
||||
}, 2000);
|
32
example/gm_value/gm_value_2_bg.js
Normal file
32
example/gm_value/gm_value_2_bg.js
Normal file
@ -0,0 +1,32 @@
|
||||
// ==UserScript==
|
||||
// @name gm value storage 读取与监听方 - 后台脚本
|
||||
// @namespace https://bbs.tampermonkey.net.cn/
|
||||
// @version 0.1.0
|
||||
// @description 多个脚本之间共享数据 读取与监听方 - 后台脚本
|
||||
// @author You
|
||||
// @run-at document-start
|
||||
// @grant GM_getValue
|
||||
// @grant GM_addValueChangeListener
|
||||
// @grant GM_listValues
|
||||
// @grant GM_cookie
|
||||
// @storageName example
|
||||
// @background
|
||||
// ==/UserScript==
|
||||
|
||||
return new Promise((resolve) => {
|
||||
GM_addValueChangeListener("test_set", function (name, oldval, newval, remote, tabid) {
|
||||
console.log("value change", name, oldval, newval, remote, tabid);
|
||||
// 可以通过tabid获取到触发变化的tab
|
||||
// GM_cookie.store可以获取到对应的cookie storeId
|
||||
GM_cookie("store", tabid, (storeId) => {
|
||||
console.log("store", storeId);
|
||||
});
|
||||
});
|
||||
|
||||
setInterval(() => {
|
||||
console.log("test_set: ", GM_getValue("test_set"));
|
||||
console.log("value list:", GM_listValues());
|
||||
}, 2000);
|
||||
// 永不返回resolve表示永不结束
|
||||
// resolve()
|
||||
});
|
Reference in New Issue
Block a user