Skip to content

Instantly share code, notes, and snippets.

@cflems
Created December 28, 2022 12:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cflems/1edd39f746af59a5820030b36452cae5 to your computer and use it in GitHub Desktop.
Save cflems/1edd39f746af59a5820030b36452cae5 to your computer and use it in GitHub Desktop.
Simple-JS email protection scheme to prevent harvesting (when not running Cloudflare, which has this built in already.)
const key = 0x90;
function protect_email(email) {
bytes = []
for (char of email) {
bytes.push(String.fromCharCode(char.charCodeAt(0) ^ key));
}
return btoa(bytes.join(''));
}
function reveal_email(cipher) {
chars = []
for (byte of atob(cipher)) {
chars.push(String.fromCharCode(byte.charCodeAt(0) ^ key));
}
return chars.join('');
}
for (tag of document.querySelectorAll('a.email')) {
if (!('cipher' in tag.dataset)) continue;
const email = reveal_email(tag.dataset.cipher);
tag.textContent = email;
tag.setAttribute('href', 'mailto:'+email);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment