Last active
October 14, 2016 07:10
-
-
Save jacobmllr95/96df44f7bcec93f97fb3cd8cec70526b to your computer and use it in GitHub Desktop.
Secure Email Adresses
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<meta http-equiv="x-ua-compatible" content="ie=edge"> | |
<title>Secure Email Adresses</title> | |
<meta name="viewport" content="width=device-width, initial-scale=1"> | |
</head> | |
<body> | |
<a id="<?= uniqid('email') ?>" href="mailto:<?= bin2hex('[email protected]') ?>"></a> | |
<script> | |
let emailAnchorEmails = {}; | |
let anchorWithSelectedText = null; | |
function hex2bin(s) { | |
s += ''; | |
let ret = [], i = 0, l; | |
for (l = s.length; i < l; i += 2) { | |
const c = parseInt(s.substr(i, 1), 16); | |
const k = parseInt(s.substr(i + 1, 1), 16); | |
if (isNaN(c) || isNaN(k)) { | |
return false; | |
} | |
ret.push((c << 4) | k); | |
} | |
return String.fromCharCode.apply(String, ret); | |
} | |
function getSelectedText() { | |
let text = ''; | |
if (typeof window.getSelection !== 'undefined') { | |
text = window.getSelection().toString(); | |
} else if (typeof document.selection !== 'undefined' | |
&& document.selection.type === 'Text') { | |
text = document.selection.createRange().text; | |
} | |
return text; | |
} | |
function onEmailAnchorMouseOver(event) { | |
const anchor = event.target; | |
if (anchorWithSelectedText !== null && | |
anchorWithSelectedText.id.replace('_', '') === anchor.id.replace('_', '')) { | |
return; | |
} | |
const email = emailAnchorEmails[anchor.id].decoded; | |
anchor.id = `_${anchor.id}`; | |
anchor.text = email; | |
anchor.setAttribute('href', `mailto:${email}`); | |
} | |
function onEmailAnchorMouseOut(event) { | |
const anchor = event.target; | |
const selectedText = getSelectedText(); | |
if (selectedText && anchor.text.indexOf(selectedText) !== -1) { | |
anchorWithSelectedText = anchor; | |
return; | |
} | |
anchor.id = anchor.id.replace('_', ''); | |
anchor.text = ''; | |
const encodedEmail = emailAnchorEmails[anchor.id].encoded; | |
anchor.setAttribute('href', `mailto:${encodedEmail}`); | |
} | |
function onSelectionChange(event) { | |
if (anchorWithSelectedText !== null) { | |
onEmailAnchorMouseOut({ target: anchorWithSelectedText }); | |
anchorWithSelectedText = null; | |
} | |
} | |
const sheet = (() => { | |
let style = document.createElement('style'); | |
style.appendChild(document.createTextNode('')); | |
document.head.appendChild(style); | |
return style.sheet; | |
})(); | |
[].forEach.call(document.querySelectorAll('a'), (anchor) => { | |
let index = 0; | |
const href = anchor.getAttribute('href'); | |
if (href.indexOf('mailto:') === 0) { | |
const encodedEmail = href.replace('mailto:', ''); | |
const email = hex2bin(encodedEmail); | |
sheet.insertRule(`#${anchor.id}::before { content: "${email}"; }`, index); | |
anchor.addEventListener('mouseover', onEmailAnchorMouseOver, false); | |
anchor.addEventListener('mouseout', onEmailAnchorMouseOut, false); | |
emailAnchorEmails[anchor.id] = { | |
encoded: encodedEmail, | |
decoded: email | |
}; | |
index++; | |
} | |
}); | |
document.addEventListener('selectionchange', onSelectionChange, false); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment