首页 > 解决方案 > 使用 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-> 脚本名称。
此外,没有错误;所以不知道出了什么问题。

标签: javascripttampermonkeyuserscripts

解决方案


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

几件事(从大到小):

  1. 不要@run-at context-menu用于此,请参阅文档
  2. 该按钮选择器有问题,可能无法获取您想要的内容。
  3. 一个简单的.click()调用可能还不够。根据此答案,您可能需要更多或不同的事件。
  4. 您没有说您使用的是什么浏览器,但不同的浏览器(以及这些浏览器的版本)处理背景选项卡的方式不同。
  5. 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);

推荐阅读