Reputation: 19
Currently im new to javascript and i've face some problem when i got an idea for my assignment.
I dont know if we can detect alert with javascript or not?
I searched and i found this topic but its not helped much. Detect if an alert or confirm is displayed on a page
My problem when i try to solve captcha with my script: https://i.sstatic.net/3NbHT.png
I expected : If our script detect "Cannot contact reCAPTCHA. Check your connection and try again."
We will refresh the page and check then try to solve captcha again.
Upvotes: 1
Views: 2338
Reputation: 4248
Interest question, i have some simple solution to prevent alert. First i collect and remember what is origin alert prototype method. That i override alert. Other procedure is simple...
window["cloneOriginAlert"] = alert;
alert = function (msg, prevent) {
if ( typeof prevent !== 'undefined') { return; }
var test = prompt("You wanna see alert ? ", "Alert is comming !!! ");
if (test != null) {
console.warn("Alert is allowed here ! ", msg)
window["cloneOriginAlert"](msg);
} else {
console.log("No alert here")
}
}
alert("Hello bug")
I hope this is what you want!
Upvotes: 0
Reputation: 1325
Try this fiddle. I do a hack on the alert function, then you can check the message to inject your stuff.
(function (window) {
const _alert = window.alert
window.alert = (message) => {
window.onAlert && window.onAlert(message, _alert)
}
})(window)
function createRecaptcha() {
grecaptcha.render("recaptcha", {sitekey: "6LcgSAMTAAAAACc2C7rc6HB9ZmEX4SyB0bbAJvTG", theme: "light"});
}
createRecaptcha();
window.onAlert = (message, alert) => {
if(message === 'Cannot contact reCAPTCHA. Check your connection and try again.') {
// do your stuffs, for example:
document.body.innerHTML = '<h1>The alert is injected</h1>'
return
}
alert(message)
}
alert('application alert')
Upvotes: 0
Reputation: 580
I think you will not be able to know and run any script when alert have been opened. Open your console and run interval to test it:
setInterval(() => console.log('test'), 1000);
And then run:
alert(1);
You will see that interval will not log 'test' until you close the alert.
To do that you want, you may need to create another application that will work in another tab and send to it post messages. If that another application will detect timeout - then you could reload the page.
Update
Or you can override alert and reload page when alert is calling:
window.alert = () => location.reload();
Upvotes: 1