Unpack PACKED scripts

Often, people compress their JavaScripts using Dean Edwards' Packer. The snippet

document.write('Hello, world!');

would then become

eval(function(p,a,c,k,e,r){e=String;if(!''.replace(/^/,String)){while(c--)r[c]=k[c]||c;k=[function(e){return r[e]}];e=function(){return'\\w+'};c=1};while(c--)if(k[c])p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c]);return p}('0.1(\'2, 3!\');',4,4,'document|write|Hello|world'.split('|'),0,{}))

where you can cleary see, that JavaScript is used to unpack the source code. So you pay the smaller size with the time needed to decompress the whole thing. For larger scripts (such as JS-frameworks) this can take several seconds - even on 2 GHz computers.

To unpack the compressed script, you need to replace the eval( by document.write(. Since your browser will try to interpret a smaller-than character as a HTML-tag, you need to write the output into a TEXTAREA. For our example, the unpacking code will look like this:

document.write('<textarea cols="120" rows="10">');
document.write(function(p,a,c,k,e,r){e=String;if(!''.replace(/^/,String)){while(c--)r[c]=k[c]||c;k=[function(e){return r[e]}];e=function(){return'\\w+'};c=1};while(c--)if(k[c])p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c]);return p}('0.1(\'2, 3!\');',4,4,'document|write|Hello|world'.split('|'),0,{}));
document.write('</textarea>');

Which will result in the following:


 
snippets/javascript/unpack-packed-scripts.txt · Last modified: 2008-07-23 16:59.44 by mbirth
 
Except where otherwise noted, content on this wiki is licensed under the following license:CC Attribution-Noncommercial-Share Alike 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki Contents powered by Club-Mate Contents powered by BassDrive.com Labelled with ICRA