Why would a link to close an overlay "takeover" popup would be clicked only from console but not from userscript?

I try to emulate a click on a button that closes a “takeover” popup (a popup that takes over the screen of a website and won’t go away until you close it).

This button is the third link in a row.

This doesn’t work in console:

document.querySelector(.x).remove();

But, this does work in console:

document.querySelectorAll('a')[2].click();

Anyway, my actual problem is that both codes don’t work from a userscript.

  • I didn’t find an element containing a shadowRoot property so I don’t think that this concept is related here.
  • After the button is clicked (manually or emulated) and the tab webpage is refreshed, the popup will no longer appear; the popup appears only after a new tab with the same webpage is opened, so this may have to do with sessionStorage.

So the code that works in console but fails in a userscript is the following:

// ==UserScript==
// @name         x
// @match        https://example.com/
// @run-at       document-start

// ==/UserScript==

window.setInterval ( ()=>{
    document.querySelectorAll('a')[2].click();
}, 100);

I am not sure that JavaScript is the best way to go here, I might need some “Meta JavaScript” approach such as some browser extension to record a macro action (Selenium, Ui.Vision RPA, etc.) or, given that I operate my computer with Windows 11 Home, utilizing some macro action from AutoHotkey.

Anyway, how would you deal with such a problem?

Well for starters, you’ve forgotten to put quotes around the selector string.

Second of all, that will select the first member of the class.

1 Like

Thanks :slight_smile:

The selector I tried to select had a component that had to be double backlashed, so I’ve changed:

document.querySelector('#example-:1:').click();

To:

document.querySelector('#example-\\:1\\:').click();

What also confused me is that in each opening of a tab with that webpage the selector slightly changes so I had to match the slight changes.

Anyway, now, when the following code pattern works from console but not from a userscript, I don’t know what I miss:

// ==UserScript==
// @name         x
// @match        https://example.com
// @run-at       document-start

// ==/UserScript==

window.setInterval( ()=>{
    const mySelectors = [
        '#example-\\:a\\: > div > div > a',
        '#example-\\:b\\: > div > div > a',
        '#example-\\:c\\: > div > div > a',
        '#example-\\:d\\: > div > div > a',
        '#example-\\:e\\: > div > div > a',
    ];

    let matchedElement = null;

    for (const selector of mySelectors) {
        matchedElement = document.querySelector(mySelectors);
        if (matchedElement) {
            break; // If an element is found, stop the loop
        }
    }

    if (matchedElement) {
        matchedElement.click();
    };
}, 100);

This also works from console but fails from userscript:

// ==UserScript==
// @name         x
// @match        https://example.com
// @run-at       document-start

// ==/UserScript==

window.setInterval( ()=>{
    document.querySelector('[id^="example-\\:r"] > div > div > a').click();
}, 100);

It’s as if the owners of the website I run it on have disabled JavaScript almost completely for the user itself…

what…exactly are you trying to match?

I am not sure if it’s legal to mention a website name here but just a popup in the homepage of a quite famous AI chatbot…

Can you give an example of the HTML block you’re trying to match, is more what i meant than which site.

You can take for granted the inevitable “Obey the ToS of the sites you are trying to interact with. Yes, they can apply to scripts you run on your local computer.”

Pasting the entire output of the following command will be quite long and I am not sure that it would be legal.

copy(document.documentElement.outerHTML);

Anyway after saving this output to an HTML file and running it locally, it was redirected to a 404 page.

I don’t know how to isolate the exact relevant HTML from the complex DOM as appears in the dev tool.

I should probably take some time and try to create a macro operation to emulate a click on this button…

Well, i cant advise you in the dark. So until you can show me what element youre trying to select, and when it exists in the DOM…

does it work if you jump the timer up to like…5000?

Changing the timer to 5000 doesn’t help; I’ll send a link in private.