因此今天我把它json化了.用json傳輸數據,也開放了api
本工具所有的功能實現都是由 http://jscompress.sinaapp.com/api 處理.(包括現在可以使用的這個在線壓縮)
所有的數據交換均由 HTTP POST 輸入處理后由 json 作為數據輸出格式. API參數 : http://jscompress.sinaapp.com/api?get={type}&code=(code)&type={compress only}
get={type},{type} 為可選的 compress (壓縮) format (格式化) shuffle(混淆)
code=(code),(code) 為必要的源代碼. JavaScript的源代碼
type={compress},{compress} 注意該參數只有壓縮的時候生效,可選 1(默認壓縮) 2(YUI壓縮) 3(GC壓縮)
例:使用 CURL... POST
{"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%"}
然后我寫了一個php文件,可以調用這個網站的api,把整個目錄所有的js文件壓縮或者混淆,格式化后保存到一個新目錄。
這樣就對那些懶上傳文件的同學們基于方便了~~
直接下載地址: jstools.rar
高亮顯示
代碼如下:
<?php /* /## js 合并和壓縮PHP腳本...可用于本地或者服務器. /## 本工具只能處理utf-8編碼的 *.js 文件.否則會接收不到結果 @ 風吟 (fengyin.name) @ http://jscompress.sinaapp.com/ */ set_time_limit(0); function JsTools($options = array( 'basepath' =>'./', //需要處理的腳本路徑... 'compiled' =>'./compiled/', //處理后新文件的路徑... 'type' =>'compress', //可選 compress (壓縮) format (格式化) shuffle (混淆) 'is_merger' =>true, // 是否需要把全部文件合并再進行處理 (壓縮,格式化,混淆) 'engine' =>'1'//此項只對 type 為 compress 時有效,1(默認) 2 (yui) 3(Closure Compiler) /* yui 和 Google Closure Compiler 壓縮是不可逆的,一般情況下使用默認即可 不推薦使用混淆. */ )){ 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(); ?>
聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com