JavaScript (JS) compression/obfuscation/formatting batch processing tools

Source: Internet
Author: User
Tags shuffle

Yesterday shared the http://jscompress.sinaapp.com/this small tool, found that you are still very fond.

So today, I converted it into json. I used json to transmit data and opened APIs.

All functional implementations of this tool are handled by the http://jscompress.sinaapp.com/api. (including the online compression that can be used now)
All data exchanges are processed by the http post input and json is used as the data output format.

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 (obfuscated)
Code = (code), (code) is the source code of necessary source code. JavaScript
Type = {compress}, {compress} note that this parameter takes effect only when compression is enabled. Optional values: 1 (default compression) 2 (YUI compression) 3 (GC compression)

Example: Use CURL... POST

Http://jscompress.sinaapp.com/api? Get = compress & code = var a = 1; var B = 2; & type = 2

If the execution is successful, the following result is returned:

{"Code": "var a = 1, B = 2; \ n", "original_size": "16 Byte", "now_size": "13 Byte ", "status": "Closure Compiler \ u538b \ u7f29 \ u5b8c \ u6210.", "minify": "81.25% "}

Then I wrote a PHP file, which can call the api of this website to compress or confuse all js files in the entire directory, format them, and save them to a new directory.

This makes it easy for students who are lazy to upload files ~~

Direct: jstools.rar
Highlight

Copy codeThe Code is as follows: <? Php
/*
// # Js merge and compress PHP scripts... can be used locally or on servers.
/## This tool can only process 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' => './', // path of the script to be processed...
'Compiled '=>'./compiled/', // path of the new file after processing...
'Type' => 'companys', // optional compress (Compressed) format (formatted) shuffle (obfuscation)
'Is _ merger '=> true, // whether to merge all files for processing (compression, formatting, and obfuscation)
'Engine' => '1' // This option is only valid when type is compress. 1 (default) 2 (yui) 3 (Closure Compiler)
/*
Yui and Google Closure Compiler compression are irreversible. Generally, use the default one.
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 api ($ 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.