2

Sometimes I see that web pages are executing javascript code from pages like this one.

Screenshot of the page

enter image description here

The problem is that all the code is printed on a single line and that makes it unreadable for people. Is there a command line tool where I can create the new lines and indent javascript code automatically?

4
  • If you are looking for a program which can reformat Javascript code in a useful way then this question is not shell-related. If you want to use standard console tools for reformatting then you have to define how that shall be done. The problem is that using e.g. space or ; as a line break can alter the meaning of the code if it happens to be quoted. If you can be sure that this is not the case then the task is easy. Commented May 27, 2018 at 21:09
  • 4
    In javascript that is considered "minification", which basically is the removal of all extraneous white space and newlines and some other efficiency modifications. You want something to "unminify" those files, which can be difficult depending on the methods used to minify the file in the first place. A quick search reveals a few tools that claim they can perform the unminification of javascript. Commented May 27, 2018 at 21:15
  • @GracefulRestart Thanks, I didn't know this term. I've made a quick search and I found an online tool that can do the job on the page: unminify.com. I've asked for a bash solution because I thought the probability of already existing command line tools specialized on "unminifying" codes was high. But by now just this online tool works for me... Commented May 27, 2018 at 21:28
  • 2
    I use jsbeautifier when in rare need of batch unminifying: pip install jsbeautifier to install. Commented May 28, 2018 at 7:20

2 Answers 2

5

As stated in the comments, the process of removing white spaces and new lines in javascript is called "minification". For doing the reverse process we need to look for tools that "unminify" the code. On the command line, we can do it installing jsbeautifier with pip:

pip install jsbeautifier

And we can use it like this:

js-beautify myMinifiedFile.js > myNewUnminifiedFile.js

Alternatively, there're online tools like unminify.com where we can just paste the code and it'll do the reverse process.

PS: Minification is not generally a reversible operation, as information could be lost in the process. So, tools like these are used only to beautify the code.

Sign up to request clarification or add additional context in comments.

Comments

1

You can unminify using online tools, but if you just need a quick reference, Chrome Developer Tools can unminify to a degree: http://codegabber.com/2018/05/31/unminify-code-using-chrome-developer-tools/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.