Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var hadnleEmailAddressToLink = (text) => {
- const regexp = /([a-zA-Z0-9._-]+)(@[a-zA-Z0-9._-]+\.[a-zA-Z0-9._-]{2,})/gi;
- const div = document.createElement('div');
- div.innerHTML = text;
- const links = div.getElementsByTagName('a');
- for (let i = 0; i < links.length; i += 1) {
- const link = links[i];
- const emailsInLink = link.innerHTML.match(
- /([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9._-]{2,})/gi
- );
- const href = link.getAttribute('href');
- if (href && href.split(/mailto:/gi).length > 1) {
- const temp = href.split(/mailto:/gi)[1].replace(regexp, '$1[[[email]]]$2');
- link.setAttribute('data-email', temp);
- link.setAttribute('href', temp);
- link.classList.add('ssd-email-link');
- if (emailsInLink) {
- let linkText = link.innerText;
- linkText = linkText.replace(regexp, '$1[[[email]]]$2');
- link.innerText = linkText;
- }
- }
- }
- let innerhtml = div.innerHTML;
- innerhtml = innerhtml.replace(
- /([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9._-]{2,})/gi,
- '<a href="mailto:$&" data-email="$&" class="ssd-email-link">$&</a>'
- );
- innerhtml = innerhtml.replace(
- /([a-zA-Z0-9._-]+)(\[\[\[email\]\]\])(@[a-zA-Z0-9._-]+\.[a-zA-Z0-9._-]{2,})/gi,
- '$1$3'
- );
- div.innerHTML = innerhtml;
- document.body.innerHTML = div.innerHTML;
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement