3

I use YUI Compressor for years to minify and obfuscate JS files. But now I need to obfuscate and minify dynamically generated JavaScript code on the server. So the main difference here is that there will not be JS files that should be obfuscated but requests to the server (Java) which should generate portions of JS code and return them already minified and obfuscated.

YUI Compressor does not give the possibility to do that. I watched in the sources and have already figured out what methods should be overridden to make it work. I would not like to do this (but will have to in case there is no other way).

So are there any other Java libraries allowing to minify and obfuscate JavaScript strings directly from Java code?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Dmitry B.
  • 99
  • 3
  • 9
  • Is the code really generated? Why can't the generation logic create code that is minified (using short variable names). – closure Dec 22 '12 at 13:27
  • This is the good point and I also thought about this. But not whole code is completely generated. It is built of some already created templates and produces some code as well. Your solution is a good way to think about but I just want to be sure there are no other solutions I can use. – Dmitry B. Dec 22 '12 at 13:31
  • My suggestion is to do a little read up on minification. I don't think minifier do much. Your minification will be superior as you are generating the code and you are in full control of the logic. – closure Dec 22 '12 at 13:36
  • see [this java library](http://ajaxian.com/archives/utility-javascript-obfuscator). – Eliran Malka Dec 22 '12 at 14:01
  • @EliranMalka I removed the link to [What is the best free JavaScript obfuscator that is available as a Java library?](https://stackoverflow.com/questions/4050099/what-is-the-best-free-javascript-obfuscator-that-is-available-as-a-java-library) as the question is still opened. – Cœur Jul 08 '18 at 16:53
  • Possible duplicate of [What is the best free JavaScript obfuscator that is available as a Java library?](https://stackoverflow.com/questions/4050099/what-is-the-best-free-javascript-obfuscator-that-is-available-as-a-java-library) – Eliran Malka Jul 09 '18 at 14:31

1 Answers1

1

Plan a)

Use the YUI not as a command line tool, but as a Java library and call it in your code.

The JavaScriptCompressor accepts a Reader descendant (e.g. InputFileReader).

    JavaScriptCompressor compressor = new JavaScriptCompressor(in, new YuiCompressorErrorReporter());

You can actually get your raw JavaScript and convert it to a Stream, e.g. How do I convert a String to an InputStream in Java?

A full example of integration is here:

http://blog.teamextension.com/yui-compressor-in-java-246

Plan b)

Note that you could simply save the generated javascript into the filesystem, and call the command line Yui from Java. Then read the minified file from the filesystem, and that's it, you have a minified JS.

Gee Bee
  • 1,794
  • 15
  • 17