Javascript: Packing, Unpacking, and Beautifying

A lot of the Javascript on the web ships in compressed form. This is particularly irritating if you discover that, say, Drupal 5’s jstools module has bugs that you’d like to fix, but the module ships with nothing but compressed versions of the Javascript code. (What were they thinking?)

A common method of packing Javascript is Dean Edwards’ packer algorithm, which has a convenient online version. You can recognize code that’s been through the packer by looking for the cute function signature at the beginning:

eval(function(p,a,c,k,e,r){...

It turns out that you can unpack code like this using Dean Edwards’ online tool. This isn’t immediately apparent, because the tool doesn’t enable this feature by default (presumably because it’s trying to pretend that packing is an effective form of code obfuscation). But if you run the handy “reEnable” bookmarklet the packer page becomes an “unpacker” page.

Once your function is unpacked you are only halfway home, because it will look like this example (from the jQuery History/Remote plugin included with Drupal jstools):

(function($){$.ajaxHistory=new function(){var c='historyReset';var k=location.hash;var e=null;var g;this.update=function(){};var h=function(){$('.remote-output').empty()};$(document).bind(c,h);if($.browser.msie){var f,initialized=false;$(function(){f=$('<iframe style="display: none;"></iframe>').appendTo(document.body).get(0);var ...

No line breaks, not so readable. So feed it through the online beautifier for Javascript and you’ll get

(function($) {
$.ajaxHistory = new
function() {
    var c = 'historyReset';
    var k = location.hash;
    var e = null;
    var g;
    this.update = function() {};
    var h = function() {
        $('.remote-output').empty()
    };
    $(document).bind(c, h);
    if ($.browser.msie) {
        var f, initialized = false;
        $(function() {
           ...

Much better.

Tags:

Comments

Thanks for posting this. It was very useful!

by Leandro (not verified), 31 Oct 2008

Thanks for that awesome post. Nothing to add, your recipe clearly works.

by John (not verified), 01 Nov 2008

Awesome! Thx :)

by Neil (not verified), 04 Jan 2009

Beautifier now also UNPACKS for you... no need to do the reEnable thing anymore.

by Anonymous, 20 Apr 2009

Just tried out the beautifier with no extra steps to unpack code... That was extremely helpful.

I only landed here searching on "javascript unpack", but oddly enough my current scenario actually is debugging the jstools module in Drupal 5. Shipping with only packed JS code... Bad form!

by Anonymous, 22 Apr 2009

There is another unpacker tool at

http://www.strictly-software.com/unpacker.asp#unpacker

that allows you to paste packed code and hit one button to uncompress.

You can also use Firefox JS error console to do the same work by appending the following to the beginning of the input box at the top labelled code:

eval=alert;

paste the packed code after that and the unpacked code will appear in an alert which can be copied from. Not so good with huge files but shows the simplicity of how to unpack any packed code that uses eval to do the packing.

by Anonymous, 14 Jun 2009

thanks alot!

by Anonymous, 18 Oct 2009

thanks, it worked for me!

by Anonymous, 07 Dec 2009

Picking up an old but very useful post. Thanks.

Note that http://jsbeautifier.org/ has been updated to do the whole job.

Very handy when one wants to work with GPL or MIT licensed code that has been obfuscated for no good reason!

http://www.paulwhippconsulting.com.au/

by Anonymous, 02 Feb 2010

In order to get it to work for me, I had to use WebBug to re-enable the "Decode" buttons and the lower textarea box by clearing the attributes that made them unchangeable.

Then it worked fine! Thanks for a very useful tip!

by Anonymous, 16 Feb 2010

Post new comment

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <blockquote> <pre>
  • Lines and paragraphs break automatically.

More information about formatting options