Created
December 28, 2022 12:56
-
-
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.)
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
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