JavaScript (JS) compression/obfuscation/Formatting batch tools _javascript tips

Source: Internet
Author: User
Tags closure http post shuffle

After sharing the gadget of http://jscompress.sinaapp.com/yesterday, we found that we still loved it.

So today I'm going to json it. Use JSON to transfer data and also open APIs

All function implementations of this tool are handled by HTTP://JSCOMPRESS.SINAAPP.COM/API. (including this online compression that can now be used)
All data exchanges are processed by HTTP post input and are formatted as data output by JSON.

API parameters: http://jscompress.sinaapp.com/api?get={type}&code= (code) &type={compress only}

Get={type},{type} is an optional compress (compressed) format (formatted) shuffle (garbled)
Code= (code), (code) is the necessary source code. Source code for JavaScript
Type={compress},{compress} Note that this parameter only takes effect when compressed, optional 1 (default compression) 2 (Yui compressed) 3 (GC compression)

Example: Using CURL ... POST

Http://jscompress.sinaapp.com/api?get=compress&code=var A=1;var b=2;&type=2

Returns the result if the execution succeeds:

{"Code": "var a=1,b=2;\n", "Original_size": "Byte", "Now_size": "Byte", "status": "Closure Compiler \u538b\u7f29\ u5b8c\u6210. "," minify ":" 81.25% "}

Then I wrote a PHP file, you can call the Web site's API, the entire directory of all JS files compressed or confused, formatted and saved to a new directory.

This is for those lazy upload files of the students based on convenience ~ ~

Direct Download Address: Jstools.rar
Highlight Display

Copy Code code as follows:

<?php
/*
/## js Merge and compress PHP scripts ... Can be used locally or as a server.
/## This tool can only handle UTF-8 encoded *.js files. Otherwise, the result will not be received.
@ Feng Yin (fengyin.name)
@ http://jscompress.sinaapp.com/
*/
Set_time_limit (0);
function Jstools ($options = Array (
' BasePath ' => './',//need to process the script path ...
' Compiled ' => './compiled/',//The path to the new file after processing ...
' Type ' => ' compress ',//optional compress (compressed) format (formatted) shuffle (confusing)
' Is_merger ' =>true,/whether all files need to be merged and processed (compressed, formatted, confused)
' Engine ' => ' 1 '//This entry is valid only for type compress, 1 (default) 2 (YUI) 3 (Closure Compiler)
/*
Yui and Google Closure Compiler compression is irreversible, under normal circumstances using the default can be
Use of obfuscation is not recommended.
*/
)){
if (Is_dir ($options [' BasePath '])) {
if ($dh = Opendir ($options [' BasePath '])) {
while (($file = Readdir ($DH))!== false) {
if (Strpos ($file, '. js ')!== false && Strpos ($file, '. Min.js ') = = False) {
$js [] = $file;
}
}
Closedir ($DH);
}
}
if ($options [' Is_merger ']) {
foreach ($js as $jsfile) {
$jscode. = file_get_contents ($jsfile). ';
}
$jscode = Json_decode (API ($jscode, $options [' type '], $options [' engine ']), true);
File_put_contents ($options [' compiled ']. All.min.js ', $jscode [' Code ']);
} else {
foreach ($js as $jsfile) {
$jscode = Json_decode (API (file_get_contents ($jsfile), $options [' type '], $options [' engine ']), true);
File_put_contents ($options [' Compiled '].str_replace ('. js ', '. Min.js ', $jsfile), $jscode [' Code ']);
}
}
}
function APIs ($code, $type, $engine) {
$ch = Curl_init (' Http://jscompress.sinaapp.com/api ');
curl_setopt ($ch, Curlopt_returntransfer, 1);
curl_setopt ($ch, Curlopt_post, 1);
curl_setopt ($ch, Curlopt_postfields, ' get= '. $type. &code= '. UrlEncode ($code). ' &type= '. $engine);
$output = curl_exec ($ch);
Curl_close ($ch);
return $output;
}
Jstools ();
?>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.