Javascript 使用Tampermonkey关闭延迟弹出窗口(实际上是一个“弹出”模型对话框)
我正在尝试关闭一个模型弹出窗口,如果rockradio.com选项卡在后台,它将在60分钟后出现 我创建了此脚本并将其添加到Tampermonkey:Javascript 使用Tampermonkey关闭延迟弹出窗口(实际上是一个“弹出”模型对话框),javascript,tampermonkey,userscripts,Javascript,Tampermonkey,Userscripts,我正在尝试关闭一个模型弹出窗口,如果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
// ==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);
但是这个弹出窗口:
右键单击时未关闭:页面->篡改猴子->脚本名称。而且,没有错误;所以我不知道出了什么问题。未经测试,因为我不会运行该站点一个小时或更长时间,但是: 有几件事(从大到小):
@run
为此,请参阅.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);
谢谢,我把@run放在上下文菜单中进行测试,我正在使用Opera,但是当粘贴代码时得到“关键字const是保留的”@初学者,奇怪。您使用的是Opera版本36还是更高版本?或者Tampermonkey的版本已经过时(或者Opera上还不支持EC6 lint语法?)无论如何,现在将所有的
const
更改为var
。Opera:63.0,Tampermonkey v4.8.5890,我将您的脚本设置为每2秒运行一次,并且控制台中没有条目,无论如何,它应该是“未找到关闭按钮”,我刚刚开始测试它,但必须等待60分钟,看看它是否有效。