Search code examples
javascripttampermonkeyuserscripts

Close delayed pop-up (really a "pop-over" model dialog) using Tampermonkey


I'm trying to close a model pop-up which appears after 60 minutes if the rockradio.com tab is in the background.

I created this script and added it to 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);

But this pop-up:

bad pop

is not closed when I right-click: page -> Tampermonkey -> script name.
Also, there are no errors; so no idea what's wrong.


Solution

  • Untested since I'm not going to run that site for an hour or more, but:

    Several things (big to small):

    1. Don't use @run-at context-menu for this, see the doc.
    2. That button selector is problematic and may not be fetching what you want.
    3. A simple .click() call might not be enough. You may need more or different events per this answer.
    4. You didn't say what browser you were using, but different browsers (and versions of those browsers) handle background tabs differently.
    5. jshint no longer applies to Tampermonkey. You would use ESLint directives, if needed.

    So, try the following. If it doesn't work, check the logs and adjust how the mouse events are delivered, per the linked answer. :

    // ==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);