JSFiddle - React, Tailwind, and code Playground
HTML
<a href="#eml" id="eml"></a>
JavaScript
String.prototype.obfuscate = function (domain) {
var str = this.toString() + '@' + domain;
str = str.toLowerCase();
str = str.toLowerCase().replace(/[\.@a-z]/gi, function (match, position, str) {
var num = str.charCodeAt(position);
/*
The caracters @ and a-z reappear in many fonts
with a different character code in many places in the character map.
You can shift the caracter code leading to a string
that looks like what you intended but in fact it isn't.
*/
return ('&#' + (num + 65248) + ';');
});
return str;
};
String.prototype.decodeHTML = function() {
var map = {"gt":">" /* , … */};
return this.replace(/&(#(?:x[0-9a-f]+|\d+)|[a-z]+);?/gi, function($0, $1) {
if ($1[0] === "#") {
return String.fromCharCode($1[1].toLowerCase() === "x" ? parseInt($1.substr(2), 16) : parseInt($1.substr(1), 10));
} else {
return map.hasOwnProperty($1) ? map[$1] : $0;
}
});
};
String.prototype.unobfuscate = function () {
var str = this;
var unobfuscated = '';
for (var i = 0; i < str.length; i++) {
var ent = '&#' + (str.charCodeAt(i) - 65248) + ';';
unobfuscated += ent.decodeHTML();
}
return unobfuscated;
};
var userName = 'az';
var eml = userName.obfuscate('domain.com');
$('#eml').html(eml).one('click', function () {
$(this)
.html($(this).html().unobfuscate())
.attr('href', null)
.prepend('<div>ready to copy/paste</div>');
return false;
});