3

How can I copy to the clipboard the content of an alert message in Google Chrome?

https://code2care.org/2015/how-to-copy-chrome-alert-popup-text-to-clipboard/ (mirror) says one could select the text and use CTRL + C but that doesn't work for me: I cannot select the text. I use Version 83.0.4103.116 (Official Build) (64-bit) on Windows 7 SP1 x64 Ultimate.

2 Answers 2

1

An alert in HTML webpage comes from windows.alert() javascript code. So you can attach a debugger which triggers a break point when a alert pops up. You get the access to the statements, and you can copy the alert text from there. Open developer tools, and add this code found from here https://stackoverflow.com/questions/14159283/is-there-any-way-to-get-the-origin-of-an-alert-box:

window.alert = function() {
  debugger;
}
1
  • 1
    Probably it isn't possible without having access to the source code of the page (or an extension that replaces the native alert function). I have the impression OP asked as a normal user, rather than as a developer. – Although it is pretty silly that Chrome doesn't provide any way to access useful information in JS alerts... Commented Aug 9, 2022 at 14:07
1

I found a useful solution here.

Hit F12 to open chrome's developer tools, paste this code snippet into the console, and hit enter:

alert = ( () => {
  const oldAlert = alert;
  var inAlert = false;
  return (x) => {
    if (!inAlert) {
      console.log(x);
      inAlert = true;
      return oldAlert(x)
      inAlert=false;
    };
  }
} )()

Now, text from the alert will be duplicated in the console, where it can be selected and copied. You need to run this bit of code before the alert appears for it to work, so if the alert is already showing, close it and re-do whatever caused it to appear.

(Thanks to Stuart Schechter for this code)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .