Now you can read between the lines of obfuscated Javascript

A few months ago I wrote a simple Javascript beautifier, which suited my needs at the time.  But since then while dissecting longer AJAX-centric scripts in various websites, I found some things that were giving the beautifier some trouble.  So I took some time this morning to fix a few bugs and improve overall performance.

Some of you may be asking, “What is a beautifier?”  Put in nasty obfuscated code that is all run together, and it spits out something that's nicely formatted and very readable:

function layoutmouseup() {if ((moving != 0) && (target != 0)) {rearrange(
moving, movingcol, target, targetcol);var targ = target;if (movingcol == targetcol
&& target > moving) {targ = targ - 1;}displaylayout();}moving = 0;movingcol =
0;target = 0;targetcol = 0;}

function layoutmouseup() { if ((moving != 0) && (target != 0)) { rearrange(moving, movingcol... var targ = target; if (movingcol == targetcol ... { targ = targ - 1; } displaylayout(); }
moving = 0; movingcol = 0; target = 0; targetcol = 0; }

 

I've now tested it with all kinds of crazy obfuscated Javascript, and the only thing it doesn't handle properly at this point is RegEx.  I'll have to save that update for the next revision.  For the moment it's a pretty good way to quickly get a handle on obfuscated code.  Pull some crazy code out from any compacted .js file out there and give it a try:
 
http://hdvforever.com/beautify.aspx
 
This will always remain 100% free and open-source.  I hope the developer community at large can benefit from it.

(My post about the original version is here.  The links there point to the updated code.)

This article is part of the GWB Archives. Original Author: Lorin Thwaits

New on Geeks with Blogs

  • We Won The One Award I Actually Care About

    Full Scale made the Inc. 5000 for the fifth year straight, the 12th listing across my three companies. Here is why the one award you cannot buy is worth stopping for.

  • Your Customers Build the Features Now

    I let a tool I liked sit dead for a year rather than build the features I wanted. An MCP server meant I never had to, and your customers can do the same to your product.

  • Get the Size of a Directory in Linux the Easy Way

    du -sh for the quick answer, ncdu for the cleanup, df for the disk itself: every command for checking directory size in Linux, plus why du and df never agree.

  • Vim Search and Replace: The Ultimate Guide

    One :%s command replaces every match in a file before a find dialog would even open. The Vim substitute patterns worth the muscle memory: flags, ranges, capture groups, and multi-file edits.