blah blah blah is here! blah blah » Close

up0down
link

Hi,
I want to fix up some compressed javascript file, basically it has been minimized by removing all whitespaces, carriage returns, etc. so that the file size is as small as possible.
So the javascript looks something like:
http://pagead2.googlesyndication.com/pagead/show_ads.js
I want to be able to read the javascript so I can learn from it, so I want some help creating a little console application that will make this javascript more readable.
I think if we do simple things to the file, we can make it much more readable, it mostly has to do with inserting carriage returns since everything is put on a single line.
Suggestiongs:
- insert a CR whenever you encounter a ;
- insert a CR before any 'return' statements
- insert a CR before any 'function' statements
- insert a CR before any { (but it can't be followed by a closing '}' or single or double quotes because then it could be initializing right?
The goal is just to make it more readable, I mean making it perfect would be really challenging I know!

last answered 2 years ago

1 answers

up0down
link

Source code reformatting is quite a tricky matter and it would be far easier to take advantage of a free javascript 'tidy' utility rather than try and write your own.
<a target="_new" href="http://www.howtocreate.co.uk/tutorials/jsexamples/JSTidy.html">Here's</a> one which worked just great when I pasted your javascript into the box using my Opera browser.

up0down
link

Funny, I was reading how just using the eval() function and inputing the entire javascript into it would automatically get it formatted.
That is basically what is being done:
<script type="text/javascript">
function tidyup(oTx) {
var damer;
try {
eval( 'damer = function () {'+oTx.value+'\n}' );
oTx.value = ''; //force scrolling back to the top left corner
oTx.value = ' '+damer.toString().replace(/^\s*function\s*\(\s*\)\s*\{\s*|\s*\}\s*$/g,'');
oTx.focus();
oTx.select();
} catch(e) {
alert( 'Syntax error. Failed to evaluate code.\n\nThe code must be valid - put the entire contents of the script element (without any HTML comments) if you are unsure.\n\nSee JavaScript console for more details.' )
throw(e);
}
}
</script>

up0down
link

What you are looking for is called a JavaScript Decompresser. See if this link helps:
http://johannburkard.de/blog/programming/javascript/decompress-javascript-compressed-by-packer-and-other-compressors.html

This post was imported from csharpfriends, if you have a similiar question please ask it again.

All previous members have been migrated, hope you enjoy the new platform!

Feedback