使用 Tampermonkey 关闭延迟弹出窗口(实际上是 "pop-over" 模型对话框)
Close delayed pop-up (really a "pop-over" model dialog) using Tampermonkey
如果 rockradio.com 选项卡在后台,我正在尝试关闭 60 分钟后出现的模型弹出窗口。
我创建了这个脚本并将其添加到 Tampermonkey:
// ==UserScript==
// @name Don't bug me, Rockradio
// @namespace http://www.rockradio.com
// @description Closes the "Are you still there?" dialog box
// @include https://www.rockradio.com/*
// @exclude https://www.rockradio.com/login
// @grant none
// @run-at context-menu
// @version 1.0
// ==/UserScript==
/* jshint -W097 */
'use strict';
setInterval(function() {
var modal = document.getElementById('modal-region');
if (typeof(modal) !== 'undefined' && modal !== null && modal.children.length !== 0) {
document.querySelectorAll("button[type='button']")[1].click();
}
}, 1000);
但是这个弹出窗口:
右击页面->Tampermonkey->脚本名称时没有关闭
此外,没有错误;所以不知道出了什么问题。
未经测试,因为我不会运行那个网站一个小时或更长时间,但是:
几件事(从大到小):
- 不要为此使用
@run-at context-menu
,请参阅 the doc。
- 该按钮选择器有问题,可能无法获取您想要的内容。
- 一个简单的
.click()
调用可能还不够。每个 this answer. 您可能需要更多或不同的事件
- 您没有说您使用的是什么浏览器,但不同的浏览器(以及这些浏览器的版本)处理背景标签的方式不同。
jshint
不再适用于 Tampermonkey。如果需要,您可以使用 ESLint 指令。
所以,请尝试以下操作。如果它不起作用,请根据链接的答案检查日志并调整鼠标事件的传递方式。 :
// ==UserScript==
// @name Rockradio, Don't bug me
// @description Closes the "Are you still there?" dialog box
// @include https://www.rockradio.com/*
// @exclude https://www.rockradio.com/login
// @grant none
// @noframes
// @version 1.1
// ==/UserScript==
/* eslint-disable no-multi-spaces */
'use strict';
setInterval (function () {
const modal = document.getElementById ('modal-region');
if (modal && modal.children.length !== 0) {
console.log ("Model found.");
const closeBtn = modal.querySelector ("button.close");
if (closeBtn) {
closeBtn.click (); // May need dispatch and/or other events.
console.log ('Close button "clicked".');
}
else {
console.log ("Close button not found.");
}
}
}, 1000);
如果 rockradio.com 选项卡在后台,我正在尝试关闭 60 分钟后出现的模型弹出窗口。
我创建了这个脚本并将其添加到 Tampermonkey:
// ==UserScript==
// @name Don't bug me, Rockradio
// @namespace http://www.rockradio.com
// @description Closes the "Are you still there?" dialog box
// @include https://www.rockradio.com/*
// @exclude https://www.rockradio.com/login
// @grant none
// @run-at context-menu
// @version 1.0
// ==/UserScript==
/* jshint -W097 */
'use strict';
setInterval(function() {
var modal = document.getElementById('modal-region');
if (typeof(modal) !== 'undefined' && modal !== null && modal.children.length !== 0) {
document.querySelectorAll("button[type='button']")[1].click();
}
}, 1000);
但是这个弹出窗口:
右击页面->Tampermonkey->脚本名称时没有关闭
此外,没有错误;所以不知道出了什么问题。
未经测试,因为我不会运行那个网站一个小时或更长时间,但是:
几件事(从大到小):
- 不要为此使用
@run-at context-menu
,请参阅 the doc。 - 该按钮选择器有问题,可能无法获取您想要的内容。
- 一个简单的
.click()
调用可能还不够。每个 this answer. 您可能需要更多或不同的事件
- 您没有说您使用的是什么浏览器,但不同的浏览器(以及这些浏览器的版本)处理背景标签的方式不同。
jshint
不再适用于 Tampermonkey。如果需要,您可以使用 ESLint 指令。
所以,请尝试以下操作。如果它不起作用,请根据链接的答案检查日志并调整鼠标事件的传递方式。 :
// ==UserScript==
// @name Rockradio, Don't bug me
// @description Closes the "Are you still there?" dialog box
// @include https://www.rockradio.com/*
// @exclude https://www.rockradio.com/login
// @grant none
// @noframes
// @version 1.1
// ==/UserScript==
/* eslint-disable no-multi-spaces */
'use strict';
setInterval (function () {
const modal = document.getElementById ('modal-region');
if (modal && modal.children.length !== 0) {
console.log ("Model found.");
const closeBtn = modal.querySelector ("button.close");
if (closeBtn) {
closeBtn.click (); // May need dispatch and/or other events.
console.log ('Close button "clicked".');
}
else {
console.log ("Close button not found.");
}
}
}, 1000);