Tool to Unminify / Decompress JavaScript [closed]
Asked Answered
R

17

438

Are there any command line scripts and/or online tools that can reverse the effects of minification similar to how Tidy can clean up horrific HTML?

(I'm specifically looking to unminify a minified JavaScript file, so variable renaming might still be an issue.)

Revkah answered 4/5, 2009 at 21:20 Comment(5)
For those of you who may not be aware, minification changes internal variable names to a single letter and removes code comments. Undoing these changes cannot be automated.Penthouse
@DanielAllenLangdon Good point. I've edited the question so it no longer implies that minification does not change variable names.Revkah
FWIW... just found JS NICE jsnice.orgRevkah
marcusjenkins.com/linux/… worked well for me.Ericaericaceous
For @DanielAllenLangdon: jsnice.org can unminify and de-obfuscate even 30k angularjs code.Leeway
G
511

You can use this : http://jsbeautifier.org/ But it depends on the minify method you are using, this one only formats the code, it doesn't change variable names, nor uncompress base62 encoding.

edit: in fact it can unpack "packed" scripts (packed with Dean Edward's packer : http://dean.edwards.name/packer/)

Georgy answered 4/5, 2009 at 21:24 Comment(4)
did you mean base64 encoding? I've never heard of base62 encoding...Terrorist
Nope, I said base 62 : dean.edwards.name/packer and en.wikipedia.org/wiki/Base_62Ramulose
is there such a thing as a tool that "changes variable names" and is still aware of JS syntax to not break the code?Columbary
Fantastic. I just solved my problem, then emailed the author of the framework with the exact fix.Pacha
A
161

Chrome developer tools has this feature built-in. Bring up the developer tools (pressing F12 is one way), in the Sources tab, the bottom left bar has a set of icons. The "{}" icon is "Pretty print" and does this conversion on demand.

UPDATE: IE9 "F12 developer tools" also has a "Format JavaScript" feature in the Script tab under the Tools icon there. (see Tip #4 in F12 The best kept web debugging secret)

enter image description here

Amide answered 22/8, 2011 at 15:22 Comment(6)
For the particular .js I was trying to un-minify, this method worked the best, cheers.Kelbee
This is now in the "Sources" tab, not the Scripts tab.Parke
@mhenry1384: Yes, Chrome did rename the tab. I updated the answer accordingly.Amide
YES!! (our app does not have a debug/dev mode for js that I am aware of -- we thankfully do not shorten/transform variable names.)Node
Just a heads-up: Chrome sometimes messes up the JavaScript code. For instance, if (a) /regex/.match(foo); is pretty printed as if (a)\n / /regex/ .\nmatch(foo); which is a syntax error.Speculum
Firefox does this as well. On the current version, you right-click on the tab for the file you want to examine and select Pretty Print source.Brogan
H
58

Got it! JSBeautifier does exactly this, and you even have options for the auto-formatting.

Handout answered 22/6, 2009 at 16:26 Comment(3)
stackoverflow-then-google instead of google-then-stackoverflow? :))))Being
I did the google for "javascript formatter online"Stans
This FireFox plugin uses JSBeautifier to un-minify javascript so you can then set breakpoints on lines in FireBug's js debugger!!: addons.mozilla.org/en-US/firefox/addon/javascript-deminifierAttain
H
16

Can't you just use a javascript formatter (http://javascript.about.com/library/blformat.htm) ?

Heterolecithal answered 4/5, 2009 at 21:25 Comment(3)
+1 for answer that also works. fabien just happened to reply firstRevkah
This only answer the "decompress" part of the question.Columbary
Nope this is not working good. It break the code... :(Cynthy
D
14

In Firefox, SpiderMonkey and Rhino you can wrap any code into an anonymous function and call its toSource method, which will give you a nicely formatted source of the function.

toSource also strips comments.

E. g.:

(function () { /* Say hello. */ var x = 'Hello!'; print(x); }).toSource()

Will be converted to a string:

function () {
    var x = "Hello!";
    print(x);
}

P. S.: It's not an "online tool", but all questions about general beautifying techniques are closed as duplicates of this one.

Descant answered 3/10, 2011 at 12:46 Comment(2)
Interesting. I asked the question 2.5 years ago so I feel it's too late to edit the title, but I still like seeing both online and offline options. Thanks for chiming in. +1Revkah
Doesn't seem to work any more on firefoxAged
F
10

If you have a Mac and TextMate - An easy alternative for formatting Javascript is:

  1. Open the file with Textmate.
  2. Click on > Bundles > JavaScript > Reformat Document
  3. Crack open a beer.
Faulkner answered 11/10, 2011 at 22:53 Comment(1)
Beer being served right now :)Tarra
C
7

Most of the IDEs also offer auto-formatting features. For example in NetBeans, just press CTRL+K.

Conchology answered 21/6, 2011 at 17:9 Comment(0)
J
5

As an alternative (since I didn't know about jsbeautifier.org until now), I have used a bookmarklet that reenabled the decode button in Dean Edward's Packer.

I found the instructions and bookmarklet here.

here is the bookmarklet (in case the site is down)

javascript:for%20(i=0;i<document.forms.length;++i)%20{for(j=0;j<document.forms[i].elements.length;++j){document.forms[i].elements[j].removeAttribute(%22readonly%22);document.forms[i].elements[j].removeAttribute(%22disabled%22);}}
Jape answered 21/9, 2009 at 22:57 Comment(0)
F
5

I'm not sure if you need source code. There is a free online JavaScript formatter at http://www.blackbeltcoder.com/Resources/JSFormatter.aspx.

Fructuous answered 23/5, 2011 at 3:18 Comment(0)
P
5

Try this one, with code coloration:

http://labs.swelen.com/tools/javascript/beauty.html

Package answered 22/10, 2011 at 12:59 Comment(0)
M
5

click on these link for JS deminification. That will install on FF as extension that help you in debugging js at runtime.

https://addons.mozilla.org/en-US/firefox/addon/javascript-deminifier/eula/141018?src=dp-btn-primary

Make answered 18/1, 2012 at 5:52 Comment(0)
S
5

Similar to Stone's answer, but for Windows/.NET developers:

If you have Visual Studio and ReSharper - An easy alternative for formatting Javascript is:

  • Open the file with Visual Studio;
  • Click on ReSharper > Tools > Cleanup Code (Ctrl+E, C);
  • Select "Default: Reformat code", and click OK;
  • Crack open a beer.
Spite answered 13/6, 2012 at 11:53 Comment(0)
B
4

Despite its miles-away-from-being-pretty interface, JSPretty is a good, free and online tool for making javascript source codes human-readable. You can enforce your preferred type of indentation and it can also detect obfuscation.

Brodeur answered 13/8, 2013 at 12:49 Comment(0)
F
3

If one is in JS possibility of using Firefox is more. And if its Firefox add on is for rescue. Following one is particularly useful.

https://addons.mozilla.org/en-US/firefox/addon/phoenix/

Fettling answered 29/4, 2011 at 8:34 Comment(0)
B
3

Pretty Diff will beautify (pretty print) JavaScript in a way that conforms to JSLint and JSHint white space algorithms.

Bulwark answered 29/7, 2012 at 10:4 Comment(1)
It's ironic the site is so un-pretty, but this is a cool tool.Revkah
D
3

Wasn't really happy with the output of jsbeautifier.org for what I was putting in, so I did some more searching and found this site: http://www.centralinternet.com.br/javascript-beautifier

Worked extremely well for me.

Desolate answered 18/11, 2013 at 20:21 Comment(0)
P
2

http://unminify.appspot.com/ Great tools for unminify javascript and json

Plauen answered 28/12, 2012 at 3:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.