To me it seems like this would be easy to Google for, but I'm not finding anything. I've searched:
And so on. I fully expect for this to be closed as a duplicate, but I'm listing my searches out so that it can help the next person: maybe it will lead to the answer I couldn't find on my own and save them some time.
That aside, here is the context. I am writing a greasemonkey/tapermonkey userscript that clicks on a button and that button has an event on it that brings up a confirm()
. How do I click on the "OK" or "Cancel" button of that confirm popup?
If the site uses window.confirm
(regardless of whether it also uses jQuery or not), you can overwrite it and implement your own functionality. For example:
// userscript code
window.confirm = () => true;
// site code
button.addEventListener('click', () => {
const result = confirm('did you really mean to click the button?');
console.log(result);
});
<button id="button">click</button>
You cannot use a userscript to interact with any of the window.alert
, window.confirm
, or window.prompt
modals, but