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:
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.
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);
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.”