2

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.

Brock Adams
  • 90,639
  • 22
  • 233
  • 295
beginner
  • 53
  • 7

1 Answers1

2

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);
Brock Adams
  • 90,639
  • 22
  • 233
  • 295
  • Thanks, i put @run-at context-menu for testing, i.m using Opera, but when pasting your code getting "The keyword const is reserved" – beginner Sep 20 '19 at 23:17
  • @beginner, Weird. Are you using Opera version 36 or later? Or maybe the version of Tampermonkey is obsolete (or doesn't yet support EC6 lint syntax on Opera?) Anyway, change all the `const` to `var` for now. – Brock Adams Sep 20 '19 at 23:29
  • opera:63.0, Tampermonkey v4.8.5890 , i set your script to run every 2 seconds, and no entries in console, it should be "Close button not found", anyway, i started testing it just now, but have to wait 60 min to see if it works or not. – beginner Sep 21 '19 at 11:43