A Useful Trick: Copy Emails on the Screen

Cover Image for A Useful Trick: Copy Emails on the Screen

I was working on something, and I have a website that has multiple emails with anchor tag and mailto action. I need to copy all those mails for sending an email. The idea of one by one copying all those emails terrified me, So I produced a Pure JavaScript solution to copy all those email/phone numbers within a few clicks. Copy the below code in the console of whichever website you want emails to be copied:

var links = document.getElementsByTagName("a"),
  hrefs = [],
  sendIt = "";
for (var i = 0; i < links.length; i++) {
  if (links[i].href.includes("mailto")) {
    var email = links[i].href.replace("mailto:", "");
    //If you want to use this as array
    hrefs.push(email);

    //If you want to copy all email ids in one string (usually to send mail to multiple ids at once)
    sendIt += email + ", ";
  }
}

// Auto-Copy e-mail from string
var el = document.createElement("textarea");
el.value = sendIt;
el.setAttribute("readonly", "");
el.style = { display: "none" };
document.body.appendChild(el);
el.select();
document.execCommand("copy");
document.body.removeChild(el);

Now, just hit Enter in the console and all emails will be copied to your clipboard.

I am also working on a Chrome extension for this, so it can be handy for non-technical users too. I'll update this post when it's done - and I might write another post on how to build Chrome extensions as well.

Back to all writingAbout Maulik