HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavascriptMinor

Email obfuscator

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
obfuscatoremailstackoverflow

Problem

Review this code for code quality.


Email obfuscator
.email { unicode-bidi: bidi-override; direction: rtl; }
Email obfuscator
A kick-ass email obfuscator, inspired by Mathias Bynens's obfuscate_email() PHP function. This tool will encode (HTML) and/or reverse (CSS) any email address you enter, making it less vulnerable to spammers who use email harvesting software aka spambots.
The output
(preview will be shown here)

The input
 
  Email: (enter the email address to be obfuscated) 
   Convert into HTML entities ("encode")
   Reverse using CSS
   Create HTML link
  

function obfuscate_email() {
  var email = document.OBFUSCATOR.email.value,
    encodedEmail = "",
    output = "";

  if (!email) {
    alert("Please enter an email address.");
  } else {
    if (document.OBFUSCATOR.reverse.checked) {
      email = email.split("").reverse().join("");
      output = "";
    }
    if (document.OBFUSCATOR.encode.checked) {
      for (var i = 0; i ";
    }

    document.getElementById("output").value = document.OBFUSCATOR.link.checked ? "" + output + "" : output;
    document.getElementById("preview").innerHTML = document.getElementById("output").value;
  }
}

Solution

Your code has room for improvement,

in top-bottom order:

  • Your code is littered with DOM queries, I would suggest to group them all on top, they reduce comprehension speed



  • Access elements through the form is considered old skool, always use getElementByID



  • Adding a span around the email should be done in 1 go, it would be much cleaner



  • You can determine the output in one shot, from the email address



  • If continuing does not makes sense ( when email is blank ), return early, this will reduce arrow coding



If you apply all this you should end up with something like this:

function obfuscateEmail( )
{
  var email = document.getElementById( 'email' ).value,
      encode = document.getElementById( 'encode' ).checked,
      reverse = document.getElementById( 'reverse' ).checked,
      link = document.getElementById( 'link' ).checked,
      output = document.getElementById( 'output' ),
      preview = document.getElementById('preview'),
      html = '',
      encodeEmail = '';

  if( !email ) {
    return alert('Please enter an email address.');
  }

  if( reverse ) {
    email = email.split('').reverse().join('');
  }
  if( encode ){
    for (var i = 0; i ' + email + '' ):email;

  output.value = link ? "" + html + "" : html;
  preview.innerHTML = output.value;  
}

Code Snippets

function obfuscateEmail( )
{
  var email = document.getElementById( 'email' ).value,
      encode = document.getElementById( 'encode' ).checked,
      reverse = document.getElementById( 'reverse' ).checked,
      link = document.getElementById( 'link' ).checked,
      output = document.getElementById( 'output' ),
      preview = document.getElementById('preview'),
      html = '',
      encodeEmail = '';

  if( !email ) {
    return alert('Please enter an email address.');
  }

  if( reverse ) {
    email = email.split('').reverse().join('');
  }
  if( encode ){
    for (var i = 0; i < email.length; i++) {
      encodeEmail += '&#' + email.charCodeAt(i) + ';';
    }       
    email = encodeEmail;
  }

  html = reverse?( '<span class="email">' + email + '</span>' ):email;

  output.value = link ? "<a href=\"&#109;&#97;&#105;&#108;&#116;&#111;&#58;" + email + "\">" + html + "</a>" : html;
  preview.innerHTML = output.value;  
}

Context

StackExchange Code Review Q#38020, answer score: 4

Revisions (0)

No revisions yet.