Skip to content

Instantly share code, notes, and snippets.

@jacobmllr95
Last active October 14, 2016 07:10
Show Gist options
  • Save jacobmllr95/96df44f7bcec93f97fb3cd8cec70526b to your computer and use it in GitHub Desktop.
Save jacobmllr95/96df44f7bcec93f97fb3cd8cec70526b to your computer and use it in GitHub Desktop.
Secure Email Adresses
<!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('jacob.mueller.elz@gmail.com') ?>"></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