# JS Beautifier

[](https://pypi.python.org/pypi/jsbeautifier)
[](https://cdnjs.com/libraries/js-beautify)
[](https://www.npmjs.com/package/js-beautify)
[](https://www.npmjs.com/package/js-beautify?activeTab=versions)
[](https://gitter.im/beautify-web/js-beautify?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
[](https://twitter.com/intent/user?screen_name=js_beautifier)
[](https://www.npmjs.org/package/js-beautify) [](https://greenkeeper.io/)
This little beautifier will reformat and re-indent bookmarklets, ugly
JavaScript, unpack scripts packed by Dean Edward’s popular packer,
as well as partly deobfuscate scripts processed by the npm package
[javascript-obfuscator](https://github.com/javascript-obfuscator/javascript-obfuscator).
Open [beautifier.io](https://beautifier.io/) to try it out. Options are available via the UI.
# Contributors Needed
I'm putting this front and center above because existing owners have very limited time to work on this project currently.
This is a popular project and widely used but it desperately needs contributors who have time to commit to fixing both
customer facing bugs and underlying problems with the internal design and implementation.
If you are interested, please take a look at the [CONTRIBUTING.md](https://github.com/beautify-web/js-beautify/blob/main/CONTRIBUTING.md) then fix an issue marked with the ["Good first issue"](https://github.com/beautify-web/js-beautify/issues?q=is%3Aissue+is%3Aopen+label%3A%22good+first+issue%22) label and submit a PR. Repeat as often as possible. Thanks!
# Installation
You can install the beautifier for node.js or python.
## Node.js JavaScript
You may install the NPM package `js-beautify`. When installed globally, it provides an executable `js-beautify` script. As with the Python script, the beautified result is sent to `stdout` unless otherwise configured.
```bash
$ npm -g install js-beautify
$ js-beautify foo.js
```
You can also use `js-beautify` as a `node` library (install locally, the `npm` default):
```bash
$ npm install js-beautify
```
## Node.js JavaScript (vNext)
The above install the latest stable release. To install beta or RC versions:
```bash
$ npm install js-beautify@next
```
## Web Library
The beautifier can be added on your page as web library.
JS Beautifier is hosted on two CDN services: [cdnjs](https://cdnjs.com/libraries/js-beautify) and rawgit.
To pull the latest version from one of these services include one set of the script tags below in your document:
```html
<script src="https://cdnjs.cloudflare.com/ajax/libs/js-beautify/1.13.5/beautify.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/js-beautify/1.13.5/beautify-css.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/js-beautify/1.13.5/beautify-html.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/js-beautify/1.13.5/beautify.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/js-beautify/1.13.5/beautify-css.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/js-beautify/1.13.5/beautify-html.min.js"></script>
<script src="https://cdn.rawgit.com/beautify-web/js-beautify/v1.13.5/js/lib/beautify.js"></script>
<script src="https://cdn.rawgit.com/beautify-web/js-beautify/v1.13.5/js/lib/beautify-css.js"></script>
<script src="https://cdn.rawgit.com/beautify-web/js-beautify/v1.13.5/js/lib/beautify-html.js"></script>
```
Older versions are available by changing the version number.
Disclaimer: These are free services, so there are [no uptime or support guarantees](https://github.com/rgrove/rawgit/wiki/Frequently-Asked-Questions#i-need-guaranteed-100-uptime-should-i-use-cdnrawgitcom).
## Python
To install the Python version of the beautifier:
```bash
$ pip install jsbeautifier
```
Unlike the JavaScript version, the Python version can only reformat JavaScript. It does not work against HTML or CSS files, but you can install _css-beautify_ for CSS:
```bash
$ pip install cssbeautifier
```
# Usage
You can beautify javascript using JS Beautifier in your web browser, or on the command-line using node.js or python.
## Web Browser
Open [beautifier.io](https://beautifier.io/). Options are available via the UI.
## Web Library
The script tags above expose three functions: `js_beautify`, `css_beautify`, and `html_beautify`.
## Node.js JavaScript
When installed globally, the beautifier provides an executable `js-beautify` script. The beautified result is sent to `stdout` unless otherwise configured.
```bash
$ js-beautify foo.js
```
To use `js-beautify` as a `node` library (after install locally), import and call the appropriate beautifier method for javascript (js), css, or html. All three method signatures are `beautify(code, options)`. `code` is the string of code to be beautified. options is an object with the settings you would like used to beautify the code.
The configuration option names are the same as the CLI names but with underscores instead of dashes. For example, `--indent-size 2 --space-in-empty-paren` would be `{ indent_size: 2, space_in_empty_paren: true }`.
```js
var beautify = require('js-beautify').js,
fs = require('fs');
fs.readFile('foo.js', 'utf8', function (err, data) {
if (err) {
throw err;
}
console.log(beautify(data, { indent_size: 2, space_in_empty_paren: true }));
});
```
## Python
After installing, to beautify using Python:
```bash
$ js-beautify file.js
```
Beautified output goes to `stdout` by default.
To use `jsbeautifier` as a library is simple:
```python
import jsbeautifier
res = jsbeautifier.beautify('your javascript string')
res = jsbeautifier.beautify_file('some_file.js')
```
...or, to specify some options:
```python
opts = jsbeautifier.default_options()
opts.indent_size = 2
opts.space_in_empty_paren = True
res = jsbeautifier.beautify('some javascript', opts)
```
The configuration option names are the same as the CLI names but with underscores instead of dashes. The example above would be set on the command-line as `--indent-size 2 --space-in-empty-paren`.
# Options
These are the command-line flags for both Python and JS scripts:
```text
CLI Options:
-f, --file Input file(s) (Pass '-' for stdin)
-r, --replace Write output in-place, replacing input
-o, --outfile Write output to file (default stdout)
--config Path to config file
--type [js|css|html] ["js"] Select beautifier type (NOTE: Does *not* filter files, only defines which beautifier type to run)
-q, --quiet Suppress logging to stdout
-h, --help Show this help
-v, --version Show the version
Beautifier Options:
-s, --indent-size Indentation size [4]
-c, --indent-char Indentation character [" "]
-t, --indent-with-tabs Indent with tabs, overrides -s and -c
-e, --eol Character(s) to use as line terminators.
[first newline in file, otherwise "\n]
-n, --end-with-newline End output with newline
--editorconfig Use EditorConfig to set up the options
-l, --indent-level Initial indentation level [0]
-p, --preserve-newlines Preserve line-breaks (--no-preserv
2201_75684541
- 粉丝: 0
- 资源: 1
会员权益专享
资源目录
libeay32.dll 1.21MB
ReleaseNote.md 108KB
Qt5WinExtras.dll 231KB
qsvgicon.dll 31KB
api-ms-win-core-handle-l1-1-0.dll 11KB
api-ms-win-crt-string-l1-1-0.dll 17KB
api-ms-win-crt-environment-l1-1-0.dll 12KB
api-ms-win-crt-locale-l1-1-0.dll 12KB
api-ms-win-core-datetime-l1-1-0.dll 11KB
api-ms-win-core-localization-l1-2-0.dll 14KB
api-ms-win-core-file-l1-1-0.dll 15KB
api-ms-win-core-sysinfo-l1-1-0.dll 12KB
api-ms-win-core-synch-l1-2-0.dll 12KB
api-ms-win-core-memory-l1-1-0.dll 12KB
api-ms-win-crt-convert-l1-1-0.dll 15KB
api-ms-win-core-errorhandling-l1-1-0.dll 11KB
api-ms-win-core-synch-l1-1-0.dll 13KB
api-ms-win-crt-math-l1-1-0.dll 22KB
api-ms-win-core-processthreads-l1-1-0.dll 13KB
vcruntime140.dll 83KB
api-ms-win-crt-conio-l1-1-0.dll 12KB
api-ms-win-core-file-l1-2-0.dll 11KB
api-ms-win-crt-utility-l1-1-0.dll 12KB
api-ms-win-crt-filesystem-l1-1-0.dll 13KB
api-ms-win-core-profile-l1-1-0.dll 11KB
msvcp140.dll 429KB
api-ms-win-core-timezone-l1-1-0.dll 11KB
api-ms-win-core-string-l1-1-0.dll 11KB
QuaZIP.dll 256KB
api-ms-win-core-processenvironment-l1-1-0.dll 12KB
api-ms-win-crt-private-l1-1-0.dll 63KB
api-ms-win-core-debug-l1-1-0.dll 11KB
api-ms-win-crt-multibyte-l1-1-0.dll 19KB
api-ms-win-core-util-l1-1-0.dll 11KB
api-ms-win-crt-time-l1-1-0.dll 14KB
api-ms-win-core-namedpipe-l1-1-0.dll 11KB
api-ms-win-crt-process-l1-1-0.dll 12KB
api-ms-win-core-libraryloader-l1-1-0.dll 12KB
api-ms-win-crt-stdio-l1-1-0.dll 17KB
msvcr120.dll 948KB
api-ms-win-crt-runtime-l1-1-0.dll 16KB
api-ms-win-core-heap-l1-1-0.dll 12KB
api-ms-win-core-interlocked-l1-1-0.dll 12KB
api-ms-win-core-processthreads-l1-1-1.dll 12KB
api-ms-win-core-rtlsupport-l1-1-0.dll 11KB
ucrtbase.dll 863KB
api-ms-win-core-file-l2-1-0.dll 11KB
api-ms-win-core-console-l1-1-0.dll 12KB
api-ms-win-crt-heap-l1-1-0.dll 12KB
uac.exe 24KB
Qt5Svg.dll 250KB
package.json 1KB
css.json 9KB
nss.json 2B
stylus.json 666B
condition_comment.json 801B
quick-css.json 2B
package.json 424B
RootFolderOpen_16x_inverse.svg 1KB
Folder_16x.svg 750B
Folder_16x_inverse.svg 760B
RootFolder_16x_inverse.svg 989B
FolderOpen_16x.svg 745B
Document_16x.svg 682B
RootFolderOpen_16x.svg 1KB
FolderOpen_16x_inverse.svg 755B
Document_16x_inverse.svg 692B
RootFolder_16x.svg 989B
vs_minimal-icon-theme.json 2KB
package.nls.en.json 46B
package.nls.json 46B
package.json 16KB
main.js 3KB
package.nls.en.json 1KB
package.nls.json 1KB
package.json 467B
package.json 2KB
zipEntry.js 12KB
LICENSE 1KB
index.js 94B
mainHeader.js 4KB
entryHeader.js 11KB
index.js 128B
inflater.js 936B
deflater.js 1021B
zipcrypto.js 5KB
zipFile.js 13KB
README.md 2KB
utils.js 8KB
index.js 179B
fattr.js 2KB
fileSystem.js 353B
constants.js 6KB
errors.js 2KB
adm-zip.js 30KB
safer.js 2KB
package.json 2KB
tests.js 15KB
dangerous.js 1KB
Readme.md 8KB
加载中...