本帖最后由 wilsons 于 2025-8-25 15:48 编辑
本帖最后由 wilsons 于 2025-8-18 04:48 编辑
本帖最后由 wilsons 于 2025-8-18 04:24 编辑
lulu0601 发表于 2025-8-17 21:17
可以帮忙给外完整的代码块吗?我初学者,代码我实在是写不出来。
测试页面 https://wilson.lovestoblog.com/demo/A.html
油猴demo脚本
// ==UserScript==
// @name GM_addValueChangeListener demo
// @namespace https://bbs.tampermonkey.net.cn/
// @version 0.1.0
// @description GM_addValueChangeListener demo. see https://bbs.tampermonkey.net.cn/thread-9358-1-1.html
// @author Wilsons
// @match https://wilson.lovestoblog.com/demo/A.html
// @match https://wilson.lovestoblog.com/demo/B.html
// @match https://www.baidu.com/*
// @grant GM_addValueChangeListener
// @grant GM_setValue
// ==/UserScript==
(function() {
'use strict';
const closedPage = '__closed_page';
if(location.href.includes('demo/A.html')) {
GM_setValue(closedPage, '');
GM_addValueChangeListener(closedPage, (name, old_value, new_value, remote)=>{
GM_setValue(closedPage, '');
if(new_value === 'B.html') alert('B closed');
else if(new_value === 'baidu.com') alert('baidu.com closed');
});
}
if(location.href.includes('demo/B.html')) {
setTimeout(()=>{
GM_setValue(closedPage, 'B.html');
setTimeout(()=>window.close(), 50); // 延迟更保险
}, 5000);
}
if(location.href.includes('baidu.com')) {
setTimeout(()=>{
GM_setValue(closedPage, 'baidu.com');
setTimeout(()=>window.close(), 50); // 延迟更保险
}, 5000);
}
})();
操作步骤:
第一步,写油猴脚本
第二步,打开测试页面测试
注意: 如果scriptcat测试有问题,可以用tampermonkey测试,我测试下来,发现scriptcat有bug,偶有监听不到的情况。