Advertisement
Guest User

Untitled

a guest
Jan 19th, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var hadnleEmailAddressToLink = (text) => {
  2.     const regexp = /([a-zA-Z0-9._-]+)(@[a-zA-Z0-9._-]+\.[a-zA-Z0-9._-]{2,})/gi;
  3.     const div = document.createElement('div');
  4.     div.innerHTML = text;
  5.     const links = div.getElementsByTagName('a');
  6.     for (let i = 0; i < links.length; i += 1) {
  7.       const link = links[i];
  8.       const emailsInLink = link.innerHTML.match(
  9.         /([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9._-]{2,})/gi
  10.       );
  11.       const href = link.getAttribute('href');
  12.       if (href && href.split(/mailto/gi).length > 1) {
  13.         const temp = href.split('mailto:')[1].replace(regexp, '$1[[[email]]]$2');
  14.         link.setAttribute('data-email', temp);
  15.         link.setAttribute('href', temp);
  16.         link.classList.add('ssd-email-link');
  17.         if (emailsInLink) {
  18.           let linkText = link.innerText;
  19.           linkText = linkText.replace(regexp, '$1[[[email]]]$2');
  20.           link.innerText = linkText;
  21.         }
  22.       }
  23.     }
  24.     let innerhtml = div.innerHTML;
  25.     innerhtml = innerhtml.replace(
  26.       /([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9._-]{2,})/gi,
  27.       '<a href="mailto:$&" data-email="$&" class="ssd-email-link">$&</a>'
  28.     );
  29.  
  30.     innerhtml = innerhtml.replace(
  31.       /([a-zA-Z0-9._-]+)(\[\[\[email\]\]\])(@[a-zA-Z0-9._-]+\.[a-zA-Z0-9._-]{2,})/gi,
  32.       '$1$3'
  33.     );
  34.     div.innerHTML = innerhtml;
  35.     document.body.innerHTML = div.innerHTML;
  36.   };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement