7

Let's say a web page did this:

window.alert = console.info;

How can I, through browser console, recover the original alert method to get the modal back?

I tried accessing the window.prototype but it does not exist. I would also like to know if such process exist in general (like, if String.* was erased/redefined by a website or if website made a console.log = window.alert).

3
  • If you can find a Polyfill for your function, you could just plug it back in. In your case, I don't think there's a polyfill for window.alert.
    – Zenoo
    Commented Jul 26, 2018 at 9:59
  • 1
    Possible duplicate of Bringing back an "overridden" window method Commented Jul 26, 2018 at 11:17
  • Since the dupe target that has been pointed to only contains only an outdated answer, I am more tempted to close that one as a dupe of this one...
    – Kaiido
    Commented Jul 26, 2018 at 13:03

1 Answer 1

3

What you can do, is create an iframe attach it to page and then copy alert method from that iframe

//someone did this
window.alert = null;

//you are doing this
var frame = document.createElement('iframe');
document.body.appendChild(frame);
window.alert = frame.contentWindow.alert.bind(window);
document.body.removeChild(frame);
2
  • Would there be a way that doesn't rely on iframe nor on altering the page content?
    – Xenos
    Commented Jul 26, 2018 at 13:39
  • You can create new window and copy alert from that link
    – ned
    Commented Jul 27, 2018 at 11:11

Not the answer you're looking for? Browse other questions tagged or ask your own question.