Quick-fix for obfuscated Javascript and bad indentation

Technology
Quick-fix for obfuscated Javascript and bad indentation

beautify_20070609.jpg
To save bandwidth and make code harder for third parties to understand, a lot of web developers will use a Javascript compression or obfuscation tool. There are a number of methods used, but almost all of them save space by removing all unnecessary whitespace.

There’s a useful GNU command line utility called “indent” that can help you deconstruct this mess. It won’t fix variables that have been renamed unintelligibly, but, as it’s name implies, it will correct the formatting and indentation of your .js file.

Indent is designed to work with C syntax, but it’s pretty forgiving and does a fairly nice job on C-like syntaxes such as Javascript, Java and (I assume) C#.Net. It can mangle a regexp here or there, but it’s a quick, essential tool if you want to quickly beautify a compressed javascript file and save it to a state that can be read.

De-obfuscate Javascript with indent

indent -bl infile.js outfile.js

You can see some sample output from this command in the image above. That’s a snippet of code from the Google taskbar for Firefox. Note that if you don’t use the second file parameter, indent will just overwrite the original file.

Tweak your block formatting preference
If there’s one thing that programmers all have a strong opinion about, it’s the proper placement of brackets. Specifically, should left brackets be placed on the same line as the function or clause that begins a block, or should they be placed on their own line at the same indentation as the close bracket?

Well, I think they should go on their own line, but if you’re not satisfied with my opinion, you can use indent to get your way:


-br, -bl
Specifying -bl lines up compound statements like this:
if (...)
{
code
}

Specifying -br (the default) makes them look like this:
if (...) {
code
}

This reminds me to mention another caveat, which is that indent uses the wrong block formatting by default. :)

GNU Indent (man page) – Link

0 thoughts on “Quick-fix for obfuscated Javascript and bad indentation

  1. ggrk says:

    My grandpa used to do this 40+ years ago! We never used to get the right sizes readily those days!

  2. Battery says:

    That must be a dedicated one. Please join Worldwide Battery Resources to gain more information.

  3. Battery says:

    That must be a dedicated one. Please join global battery directory at http://www.global-battery-directory.com to gain more information.

  4. Eva Smith says:

    this sounds cool! Im so going to try this

Discuss this article with the rest of the community on our Discord server!
Tagged

ADVERTISEMENT

Maker Faire Bay Area 2023 - Mare Island, CA

Escape to an island of imagination + innovation as Maker Faire Bay Area returns for its 15th iteration!

Buy Tickets today! SAVE 15% and lock-in your preferred date(s).

FEEDBACK