Javascript 使用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

我正在尝试关闭一个模型弹出窗口,如果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);
但是这个弹出窗口:

右键单击时未关闭:页面->篡改猴子->脚本名称。

而且,没有错误;所以我不知道出了什么问题。

未经测试,因为我不会运行该站点一个小时或更长时间,但是:

有几件事(从大到小):

  • 不要在关联菜单中使用
    @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分钟,看看它是否有效。