javascript - 使用 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
用于此,请参阅文档。 - 该按钮选择器有问题,可能无法获取您想要的内容。
- 一个简单的
.click()
调用可能还不够。根据此答案,您可能需要更多或不同的事件。 - 您没有说您使用的是什么浏览器,但不同的浏览器(以及这些浏览器的版本)处理背景选项卡的方式不同。
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);
推荐阅读
- c# - 如何在 acumatica 上获取图像链接
- eclipse - 如何在 BIRT 中编辑报告单元格的行跨度?
- python - 我正在努力从 CSV 文件创建字典
- docker - Docker网络接口卡带宽限制?
- python - 如何防止精灵相互重叠
- azure-machine-learning-service - 作业提交失败:CondaHTTPError: HTTP 000 CONNECTION FAILED
- php - Symfony 4 TwigFunction 未注册
- r - 如何为 3 个组添加一些列的值?
- docker - 创建 pod 沙箱失败:rpc 错误:code = Unknown desc #198
- mysql - 是否可以将应用程序内的所有数据库交互限制为读取?