22

This question is exactly the opposite of Which Javascript minifier (cruncher) does the same things that the one Google uses for its JS APIs?

I want to learn how google does it's loading so I can build my own with non-popular JS toolkits.

Community
  • 1
  • 1
Jorge Vargas
  • 6,712
  • 7
  • 32
  • 29
  • 1
    I just found I didn't search for "unminify" http://stackoverflow.com/questions/822119/online-tool-to-unminify-decompress-javascript, although I still think this is valid as neither of the answers works to "undo the variable renaming" – Jorge Vargas Sep 07 '09 at 07:03
  • 1
    You are using the wrong term in your question. What you are really interested in reversing "obfuscation" (identifier renaming). THus you've gotten a bunch of answers correct for your question but not for your apparant purpose. – Ira Baxter Sep 07 '09 at 08:29
  • If there's no sourcemap, the best you can do is beautify it. If the variable names were all shortened, there's no way to... unshorten them without having the source. (and if you had the source, there's no point in deobfuscating it.) – Kevin B Nov 30 '15 at 19:34
  • Possible duplicate of [How to deminify javascript](http://stackoverflow.com/questions/7634890/how-to-deminify-javascript) – John Slegers Feb 23 '16 at 22:56

10 Answers10

33

Try this: JS Beautifier

DisgruntledGoat
  • 70,219
  • 68
  • 205
  • 290
yoda
  • 10,834
  • 19
  • 64
  • 92
  • 3
    This is just a code formatter, very good at it but not what I'm look for here. – Jorge Vargas Sep 07 '09 at 07:49
  • It doesn't just format youy code, it's supposed to do the "deminifier" thing .. had you tried actually? – yoda Sep 07 '09 at 07:57
  • 3
    @Yoda: How can it dobfuscate? It appears to know nothing about identifiers. – Ira Baxter Sep 07 '09 at 09:19
  • [JavaScript Obfuscator](https://wtools.io/javascript-obfuscator) You can paste your code to the "Obfuscated Output" textarea and press "decode" – overals Jan 09 '21 at 12:29
23

Try http://www.jsnice.org/

I just stumbled on it and it is great. It expands the code. It has statistical variable renaming. for example, if you have this code:

var g = f.originalEvent.targetTouches[0];

Then it it turns your code into:

var touches = event.originalEvent.targetTouches[0];

Pretty good guess, methinks.

It turned this:

d.slide.hasClass("selected") ? (e.onSlideOpen.call(d.prev.children("div")[0]), q ? (e.rtl && d.slide.position().left > i.w / 2 || d.slide.position().left < i.w / 2) && h.animateSlide.call(d) : t && d.index ? (h.animateGroup(d, !0), h.fitToContent(d.prev)) : d.slide.position().top < i.h / 2 && h.animateSlide.call(d)) : (setTimeout(function() { e.onSlideOpen.call(d.slide.children("div")[0]) }, e.slideSpeed), h.animateGroup(d), t && h.fitToContent(d.slide)), e.autoPlay && (f.stop(), f.play(l.index(j.filter(".selected"))))

Into this:

if (e.slide.hasClass("selected")) {
    settings.onSlideOpen.call(e.prev.children("div")[0]);
    if (val) {
      if (settings.rtl && e.slide.position().left > box.w / 2 || e.slide.position().left < box.w / 2) {
        self.animateSlide.call(e);
      }
    } else {
      if (isMac && e.index) {
        self.animateGroup(e, true);
        self.fitToContent(e.prev);
      } else {
        if (e.slide.position().top < box.h / 2) {
          self.animateSlide.call(e);
        }
      }
    }
  } else {
    setTimeout(function() {
      settings.onSlideOpen.call(e.slide.children("div")[0]);
    }, settings.slideSpeed);
    self.animateGroup(e);
    if (isMac) {
      self.fitToContent(e.slide);
    }
  }
  if (settings.autoPlay) {
    node.stop();
    node.play(tabs.index(options.filter(".selected")));
  }

A library I'm working on has a couple of bugs, and after spending hours trying to decipher the code, finding this is going to save me a bunch of time.

Seriously, this tool wipes the floor with JS Beautifier.

Shane
  • 827
  • 8
  • 18
15

Uhhh, it would be impossible to restore variable names unless there was a mapping of minified -> original variable names available. Otherwise, I think the authors of that tool could win the Randi prize for psychic feats.

patros
  • 7,719
  • 3
  • 28
  • 37
  • 1
    Of course you will not be able to regain the original names. But if it at least get you better names you can later find&replace, in other word the tool will know JS syntax and replace the variable a with method_named_a but will not replace it in ba() because that will generate a syntax error – Jorge Vargas Sep 07 '09 at 07:41
  • 4
    You specifically complained that the beautifiers didn't "undo the variable renaming", implying you wanted the original symbols back. – patros Sep 07 '09 at 23:26
7

Chrome Developer tools has this built in

Matjaz Lipus
  • 702
  • 1
  • 5
  • 10
2

You will not be able to reconstruct method name or variable names. The best you can hope for is a simple JS code formater (like those previously mentioned), and then to go through the file method by method, line by line, working out what each part does.

Perhaps using a good JS refactoring tool would make this easier as well (being able to rename/document methods)

Matthew
  • 3,086
  • 2
  • 20
  • 9
  • 1
    Ok this seems like a better lead, so which is a good JS refractoring tool? – Jorge Vargas Sep 07 '09 at 07:45
  • 1
    Intellij IDEA can do method/variable renaming on Javascript files. Apparantly CodeRush / Refactor Pro will do the same in Visual Studio – Matthew Sep 08 '09 at 14:53
1

You can use the \b (word boundary) feature in regular expressions to find single-letter variable names in a file.

for i in "abcdefghij..z"; do
    sed -i "s/\b$i\b/$(random /usr/share/dict/words)/g" somefile.js
done

You can also use this in vim with something like :%s/\<a\>/truesaiyanpower/g.

kanzure
  • 1,331
  • 11
  • 15
0

See our SD ECMAScript Formatter for a tool that will nicely format code.

EDIT: If you want to reverse the renaming process you need something can rename the obfuscated names back to the originals.

This tool can technically do that: SD Thicket ECMAScript Obfuscator.

It does so by applying a renaming map over which you have precise control. Typically you implicitly construct such a map during the obfuscation process by choosing which names to obfuscate and which to preserve, and the obfuscator applies that map to produce the obfuscated code.

The Thicket obfuscator generates this map as side effect when you obfuscate in the form essentially of a set of pairs (originalname,obfuscatedname) for reference and debugging purposes.

Swapping elements gives the map (obfuscatedname,originalname). That inverted map can be applied by Thicket to recover the code with the original names, from the obfuscated code. And the Thicket obfuscator includes the Formatter to let you make it look nice again.

The catch to "reversing minification" (as you put it poorly, you are trying to reverse obfuscation), is that you need the map. Since people doing obfuscation don't give away the map, you, as a recipient of obfuscated code, have nothing to apply. A would-be pirate would have to reconstruct the map presumably by painful reverse engineering.

The "reversing" process also can't recover comments. They're gone forever.

This is all by design, so the real question is why are you looking to reverse obfuscation?

Ira Baxter
  • 93,541
  • 22
  • 172
  • 341
  • @Jorge: that's because your original question misused the term "minification" when you meant "obfuscation". I've expanded the answer to tell you exactly how to reverse the obfuscation process to the extent possible. The tools will do it if you have the right data. You are unlikely to have that data. – Ira Baxter Sep 07 '09 at 08:28
  • This is not a deobfuscator too. It just maps identifer names and pretty prints. – artificialidiot Sep 07 '09 at 10:53
  • That's exactly what I said, and that you'd couldn't do better than that. – Ira Baxter Sep 07 '09 at 14:58
  • @Ira, I do not agree variable renaming is not obfuscation, it's simply minification of "letters" rather than "whitespace". Obfuscation of variable names is to add variables/methods that are not needed or to replace 1 call with 2 calls one to a dummy method that simply redirects. – Jorge Vargas Sep 18 '09 at 06:00
  • Most users/victims of such tools think they are fine obfuscators even if you have a different definition. It is true that one could do more to obfuscate code. I could claim that adding useless method calls isn't obfuscation, and that I was satisfied only if you scrambled control flow, data flow, and converted the result to a 3 state 7 symbol Turing machine, but this is disingenuous. The issue here is degree of obfuscation, not whether obfuscated. YMMV. – Ira Baxter Sep 18 '09 at 13:42
0

To unminify js files, Unminify would be the best!

John Slegers
  • 45,213
  • 22
  • 199
  • 169
Shafizadeh
  • 9,960
  • 12
  • 52
  • 89
0

To unminify css, html and js files, you can use Unminify or Unminify JS online tool!

  • How is this any different to the one posted by Shafizadeh? I can see the actual link is different, is it just someone else's online implementation of unminify? – K Mo Mar 03 '20 at 11:26
  • There are multiple tools are available to format and convert into read only formats. I am not sure all tools are same, but it help you to format all your web elements like JS, html and CSS in effective manner. Thanks. – Mohandas P G Mar 03 '20 at 14:26
-2

Javascript minifier and Javascript Obfuscator are two different things.

Minifier - removes the comments, unnecessary whitespace and newlines from a program.

Obfuscator - make modifications to the program, changing the names of variables, functions, and members, making the program much harder to understand. Some obfuscators are quite aggressive in their modifications to code.

This Javascript Obfuscator will obfuscate your code.

If you want to deobfuscate your code, try Javascript Beautifier. It will deobfuscate if obfuscation is found and then beautify the code.

  • minifiers may also obfuscate with the purpose of making the code smaller, it being hard to read is just a side effect. – Kevin B Nov 30 '15 at 19:32