`
YongJie
  • 浏览: 73139 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

JS格式化&压缩工具

阅读更多

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">   
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>JS格式化工具JS代码转换为格式排版格式或压缩格式</title>
        <script type="text/javascript">
            < !--
            function CLASS_FORMAT(code) {
                //哈希表类
                function Hashtable() {
                    this._hash = new Object();
                    this.add = function(key, value) {
                        if (typeof(key) != "undefined") {
                            if (this.contains(key) == false) {
                                this._hash[key] = typeof(value) == "undefined" ? null: value;
                                return true;
                            } else {
                                return false;
                            }
                        } else {
                            return false;
                        }
                    }
                    this.remove = function(key) {
                        delete this._hash[key];
                    }
                    this.count = function() {
                        var i = 0;
                        for (var k in this._hash) {
                            i++;
                        }
                        return i;
                    }
                    this.items = function(key) {
                        return this._hash[key];
                    }
                    this.contains = function(key) {
                        return typeof(this._hash[key]) != "undefined";
                    }
                    this.clear = function() {
                        for (var k in this._hash) {
                            delete this._hash[k];
                        }
                    }
                }
                this._caseSensitive = true;
                //字符串转换为哈希表
                this.str2hashtable = function(key, cs) {
                    var _key = key.split(/,/g);
                    var _hash = new Hashtable();
                    var _cs = true;
                    if (typeof(cs) == "undefined" || cs == null) {
                        _cs = this._caseSensitive;
                    } else {
                        _cs = cs;
                    }
                    for (var i in _key) {
                        if (_cs) {
                            _hash.add(_key[i]);
                        } else {
                            _hash.add((_key[i] + "").toLowerCase());
                        }
                    }
                    return _hash;
                }
                //获得需要转换的代码
                this._codetxt = code;
                if (typeof(syntax) == "undefined") {
                    syntax = "";
                }
                this._deleteComment = false;
                //是否大小写敏感
                this._caseSensitive = true;
                //可以后面加块语句的关键字
                this._blockElement = this.str2hashtable("switch,if,while,try,finally");
                //是函数申明
                this._function = this.str2hashtable("function");
                //本行括号内分号不做换行
                this._isFor = "for";
                this._choiceElement = this.str2hashtable("else,catch");
                this._beginBlock = "{";
                this._endBlock = "}";
                this._singleEyeElement = this.str2hashtable("var,new,return,else,delete,in,case");
                //得到分割字符
                this._wordDelimiters = "  ,.?!;:\\/<>() {}[]\"'\r\n\t=+-|*%@#$^&";
                //引用字符
                this._quotation = this.str2hashtable("\",'");
                //行注释字符
                this._lineComment = "//";
                //转义字符
                this._escape = "\\";
                //多行引用开始
                this._commentOn = "/*";
                //多行引用结束
                this._commentOff = "*/";
                //行结束词
                this._rowEnd = ";";
                this._in = "in";
                this.isCompress = false;
                this.style = 0;
                this._tabNum = 0;
                this.format = function() {
                    var codeArr = new Array();
                    var word_index = 0;
                    var htmlTxt = new Array();
                    if (this.isCompress) {
                        this._deleteComment = true;
                    }
                    //得到分割字符数组(分词)
                    for (var i = 0; i < this._codetxt.length; i++) {
                        if (this._wordDelimiters.indexOf(this._codetxt.charAt(i)) == -1) {
                            //找不到关键字
                            if (codeArr[word_index] == null || typeof(codeArr[word_index]) == 'undefined') {
                                codeArr[word_index] = "";
                            }
                            codeArr[word_index] += this._codetxt.charAt(i);
                        } else {
                            if (typeof(codeArr[word_index]) != 'undefined' && codeArr[word_index].length > 0) word_index++;
                            codeArr[word_index++] = this._codetxt.charAt(i);
                        }
                    }
                    var quote_opened = false;
                    //引用标记
                    var slash_star_comment_opened = false;
                    //多行注释标记
                    var slash_slash_comment_opened = false;
                    //单行注释标记
                    var line_num = 1;
                    //行号
                    var quote_char = "";
                    //引用标记类型
                    var function_opened = false;
                    var bracket_open = false;
                    var for_open = false;
                    //按分割字,分块显示
                    for (var i = 0; i <= word_index; i++) {
                        //处理空行(由于转义带来)
                        if (typeof(codeArr[i]) == "undefined" || codeArr[i].length == 0) {
                            continue;
                        } else if (codeArr[i] == " " || codeArr[i] == "\t") {
                            if (slash_slash_comment_opened || slash_star_comment_opened) {
                                if (!this._deleteComment) {
                                    htmlTxt[htmlTxt.length] = codeArr[i];
                                }
                            }
                            if (quote_opened) {
                                htmlTxt[htmlTxt.length] = codeArr[i];
                            }
                        } else if (codeArr[i] == "\n") {
                            //处理换行
                        } else if (codeArr[i] == "\r") {
                            slash_slash_comment_opened = false;
                            quote_opened = false;
                            line_num++;
                            if (!this.isCompress) {
                                htmlTxt[htmlTxt.length] = "\r\n" + this.getIdent();
                            }
                            //处理function里的参数标记
                        } else if (!slash_slash_comment_opened && !slash_star_comment_opened && !quote_opened && this.isFunction(codeArr[i])) {
                            htmlTxt[htmlTxt.length] = codeArr[i] + " ";
                            function_opened = true;
                        } else if (!slash_slash_comment_opened && !slash_star_comment_opened && !quote_opened && codeArr[i] == this._isFor) {
                            htmlTxt[htmlTxt.length] = codeArr[i];
                            for_open = true;
                        } else if (!slash_slash_comment_opened && !slash_star_comment_opened && !quote_opened && codeArr[i] == "(") {
                            bracket_open = true;
                            htmlTxt[htmlTxt.length] = codeArr[i];
                        } else if (!slash_slash_comment_opened && !slash_star_comment_opened && !quote_opened && codeArr[i] == ")") {
                            bracket_open = false;
                            htmlTxt[htmlTxt.length] = codeArr[i];
                        } else if (!slash_slash_comment_opened && !slash_star_comment_opened && !quote_opened && codeArr[i] == this._rowEnd) {
                            if (!this.isCompress) {
                                if (!for_open) {
                                    if (i < word_index && (codeArr[i + 1] != "\r" && codeArr[i + 1] != "\n")) {
                                        htmlTxt[htmlTxt.length] = codeArr[i] + "\n" + this.getIdent();
                                    } else {
                                        htmlTxt[htmlTxt.length] = codeArr[i] + this.getIdent();
                                    }
                                } else {
                                    htmlTxt[htmlTxt.length] = codeArr[i];
                                }
                            } else {
                                htmlTxt[htmlTxt.length] = codeArr[i];
                            }
                        } else if (!slash_slash_comment_opened && !slash_star_comment_opened && !quote_opened && codeArr[i] == this._beginBlock) {
                            for_open = false;
                            if (!this.isCompress) {
                                switch (this.style) {
                                case 0:
                                    this._tabNum++;
                                    htmlTxt[htmlTxt.length] = codeArr[i] + "\n" + this.getIdent();
                                    break;
                                case 1:
                                    htmlTxt[htmlTxt.length] = "\n" + this.getIdent();
                                    this._tabNum++;
                                    htmlTxt[htmlTxt.length] = codeArr[i] + "\n" + this.getIdent();
                                    break;
                                default:
                                    this._tabNum++;
                                    htmlTxt[htmlTxt.length] = codeArr[i];
                                    break;
                                }
                            } else {
                                htmlTxt[htmlTxt.length] = codeArr[i];
                            }
                        } else if (!slash_slash_comment_opened && !slash_star_comment_opened && !quote_opened && codeArr[i] == this._endBlock) {
                            if (!this.isCompress) {
                                this._tabNum--;
                                if (i < word_index && codeArr[i + 1] != this._rowEnd) {
                                    htmlTxt[htmlTxt.length] = "\n" + this.getIdent() + codeArr[i];
                                } else {
                                    htmlTxt[htmlTxt.length] = "\n" + this.getIdent() + codeArr[i];
                                }
                            } else {
                                if (i < word_index && codeArr[i + 1] != this._rowEnd) {
                                    htmlTxt[htmlTxt.length] = codeArr[i] + this._rowEnd;
                                } else {
                                    htmlTxt[htmlTxt.length] = codeArr[i];
                                }
                            }
                            //处理关键字
                        } else if (!slash_slash_comment_opened && !slash_star_comment_opened && !quote_opened && this.isBlockElement(codeArr[i])) {
                            htmlTxt[htmlTxt.length] = codeArr[i];
                            //处理内置对象(后面加一个空格)
                        } else if (!slash_slash_comment_opened && !slash_star_comment_opened && !quote_opened && this.isSingleEyeElement(codeArr[i])) {
                            if (codeArr[i] == this._in) {
                                htmlTxt[htmlTxt.length] = " ";
                            }
                            htmlTxt[htmlTxt.length] = codeArr[i] + " ";
                            //处理双引号(引号前不能为转义字符)
                        } else if (!slash_star_comment_opened && !slash_slash_comment_opened && this._quotation.contains(codeArr[i])) {
                            if (quote_opened) {
                                //是相应的引号
                                if (quote_char == codeArr[i]) {
                                    htmlTxt[htmlTxt.length] = codeArr[i];
                                    quote_opened = false;
                                    quote_char = "";
                                } else {
                                    htmlTxt[htmlTxt.length] = codeArr[i];
                                }
                            } else {
                                htmlTxt[htmlTxt.length] = codeArr[i];
                                quote_opened = true;
                                quote_char = codeArr[i];
                            }
                            //处理转义字符
                        } else if (codeArr[i] == this._escape) {
                            htmlTxt[htmlTxt.length] = codeArr[i];
                            if (i < word_index - 1) {
                                if (codeArr[i + 1].charCodeAt(0) >= 32 && codeArr[i + 1].charCodeAt(0) <= 127) {
                                    htmlTxt[htmlTxt.length] = codeArr[i + 1].substr(0, 1);
                                    htmlTxt[htmlTxt.length] = codeArr[i + 1].substr(1);
                                    i = i + 1;
                                }
                            }
                            //处理多行注释的开始
                        } else if (!slash_slash_comment_opened && !slash_star_comment_opened && !quote_opened && this.isStartWith(this._commentOn, codeArr, i)) {
                            slash_star_comment_opened = true;
                            if (!this._deleteComment) {
                                htmlTxt[htmlTxt.length] = this._commentOn;
                            }
                            i = i + this.getSkipLength(this._commentOn);
                            //处理单行注释
                        } else if (!slash_slash_comment_opened && !slash_star_comment_opened && !quote_opened && this.isStartWith(this._lineComment, codeArr, i)) {
                            slash_slash_comment_opened = true;
                            if (!this._deleteComment) {
                                htmlTxt[htmlTxt.length] = this._lineComment;
                            }
                            i = i + this.getSkipLength(this._lineComment);
                            //处理忽略词
                        } else if (!slash_slash_comment_opened && !slash_star_comment_opened && !quote_opened && this.isStartWith(this._ignore, codeArr, i)) {
                            slash_slash_comment_opened = true;
                            htmlTxt[htmlTxt.length] = this._ignore;
                            i = i + this.getSkipLength(this._ignore);
                            //处理多行注释结束
                        } else if (!quote_opened && !slash_slash_comment_opened && this.isStartWith(this._commentOff, codeArr, i)) {
                            if (slash_star_comment_opened) {
                                slash_star_comment_opened = false;
                                if (!this._deleteComment) {
                                    htmlTxt[htmlTxt.length] = this._commentOff;
                                }
                                i = i + this.getSkipLength(this._commentOff);
                            }
                        } else {
                            //不是在字符串中
                            if (!quote_opened) {
                                //如果不是在注释重
                                if (!slash_slash_comment_opened && !slash_star_comment_opened) {
                                    htmlTxt[htmlTxt.length] = codeArr[i];
                                    //注释中
                                } else {
                                    if (!this._deleteComment) {
                                        htmlTxt[htmlTxt.length] = codeArr[i];
                                    }
                                }
                            } else {
                                htmlTxt[htmlTxt.length] = codeArr[i];
                            }
                        }
                    }
                    return htmlTxt.join("");
                }
                this.isStartWith = function(str, code, index) {
                    if (typeof(str) != "undefined" && str.length > 0) {
                        var cc = new Array();
                        for (var i = index; i < index + str.length; i++) {
                            cc[cc.length] = code[i];
                        }
                        var c = cc.join("");
                        if (this._caseSensitive) {
                            if (str.length >= code[index].length && c.indexOf(str) == 0) {
                                return true;
                            }
                        } else {
                            if (str.length >= code[index].length && c.toLowerCase().indexOf(str.toLowerCase()) == 0) {
                                return true;
                            }
                        }
                        return false;
                    } else {
                        return false;
                    }
                }
                this.isFunction = function(val) {
                    return this._function.contains(this._caseSensitive ? val: val.toLowerCase());
                }
                this.isBlockElement = function(val) {
                    return this._blockElement.contains(this._caseSensitive ? val: val.toLowerCase());
                }
                this.isChoiceElement = function(val) {
                    return this._choiceElement.contains(this._caseSensitive ? val: val.toLowerCase());
                }
                this.isSingleEyeElement = function(val) {
                    return this._singleEyeElement.contains(this._caseSensitive ? val: val.toLowerCase());
                }
                this.isNextElement = function(from, word) {
                    for (var i = from; i < word.length; i++) {
                        if (word[i] != " " && word[i] != "\t" && word[i] != "\r" && word[i] != "\n") {
                            return this.isChoiceElement(word[i]);
                        }
                    }
                    return false;
                }
                this.getSkipLength = function(val) {
                    var count = 0;
                    for (var i = 0; i < val.length; i++) {
                        if (this._wordDelimiters.indexOf(val.charAt(i)) >= 0) {
                            count++;
                        }
                    }
                    if (count > 0) {
                        count = count - 1;
                    }
                    return count;
                }
                this.getIdent = function() {
                    //添加字符缩进
                    var n = [];
                    for (var i = 0; i < this._tabNum; i++) {
                        n[n.length] = "\t";
                        //缩进字符
                    }
                    return n.join("");
                }
            }
            function doformat(o) {
                var htmltxt = "";
                if (o == null) {
                    alert("domNode is null!");
                    return;
                }
                var _codetxt = "";
                if (typeof(o) == "object") {
                    switch (o.tagName) {
                    case "TEXTAREA":
                    case "INPUT":
                        _codetxt = o.value;
                        break;
                    case "DIV":
                    case "SPAN":
                        _codetxt = o.innerText;
                        break;
                    default:
                        _codetxt = o.innerHTML;
                        break;
                    }
                } else {
                    _codetxt = o;
                }
                var _syn = new CLASS_FORMAT(_codetxt);
                htmltxt = _syn.format();
                return htmltxt;
            }
            function go() {
                var code = document.getElementById("code").value;
                var xx = new CLASS_FORMAT(code);
                var a = new Date();
                var code1;
                //格式化之后对code 继续格式化
                if (document.getElementById('cboOperate').selectedIndex == 1) {
                    xx.isCompress = true;
                } else {
                    xx.style = parseInt(document.getElementById('cboStyle').value);
                }
                code1 = xx.format();
                code1 = code1.replace(/\s*\n/g, '\n');
                //去掉行尾的空白符号
                code1 = code1.replace(/\n\n/g, '\n');
                //去掉联系的两个空行
                code1 = code1.replace(/\)\{/g, ') {');
                //在) {之间加一个空格
                document.getElementById("display").value = code1;
                alert("格式化完成,共花:" + (new Date().getTime() - a.getTime()) + "ms");
            }
            //-->
           
        </script>
    </head>
   
    <body>
        <div style=" padding:10px;">
            <h2 style="text-align:center; padding:5px;">
                JS格式化工具—排版格式,JS压缩
            </h2>
            <TEXTAREA style="width:98%; height:240px;" name="code" id=code></TEXTAREA>
            <SELECT name="cboOperate" id=cboOperate onchange="if(this.selectedIndex==1)document.getElementById('cboStyle').disabled=true;&#13;&#10;else document.getElementById('cboStyle').disabled=false;">
                <OPTION value=0 selected>格式化</OPTION>
                <OPTION value=1>压缩</OPTION>
            </SELECT>
            <SELECT name="cboStyle" id=cboStyle>
                <OPTION value=0 selected>经典</OPTION>
                <OPTION value=1>微软</OPTION>
            </SELECT>
            <INPUT onclick=go() type=button value=转换>
            <TEXTAREA style="width:98%; height:240px;" name="display" id=display></TEXTAREA>
        </div>
    </body>

</html>

分享到:
评论

相关推荐

    javascript格式化和压缩工具

    总的来说,JavaScript格式化和压缩工具是前端开发不可或缺的辅助工具,它们能够帮助我们保持代码的整洁,提高代码的运行效率,并简化开发流程。了解并熟练运用这些工具,能够大幅提升开发效率和代码质量。

    js格式化排版工具, 反向js压缩编码

    然而,为了提高页面加载速度和节省网络带宽,开发者通常会使用压缩工具对JS代码进行压缩,这使得代码变得难以阅读。本文将详细介绍如何使用JS格式化排版工具来对压缩的JS编码进行反向处理,使其恢复成可读性较高的...

    js(JavaScript)格式化压缩工具

    js格式化压缩工具,写个资源描述还要20个字,我靠,真是罗罗,这回够了吧

    实用工具 格式化Html格式,格式代javasctip格式,javascript代码压缩

    总结起来,这个压缩包提供了HTML格式化和JavaScript格式化及压缩的相关工具,对于网页程序员来说是非常实用的资源。通过使用这些工具,开发者可以更高效地管理和优化他们的代码,从而提高工作效率并提升项目质量。

    js代码在线压缩、格式化的工具

    本文将详细介绍一种用于JS代码在线压缩和格式化的工具。 首先,这种工具是一个Web网页应用,用户无需下载安装任何软件,只需通过浏览器访问,即可方便地对JS代码进行处理。这对于开发者来说,大大提高了工作效率,...

    js格式化工具_速度快,操作简易

    用户可以通过它们了解如何使用这些工具,包括输入JS代码、选择格式化选项(如缩进风格、最大行长度等)以及如何执行格式化、压缩和加密操作。 在实际开发中,掌握JS格式化工具的使用不仅可以提升个人编程效率,也有...

    css/js 压缩格式化工具

    - **Prettier**:一款广泛使用的代码格式化工具,支持多种编程语言,包括JavaScript和CSS。它可以根据预设的规则自动格式化代码,保持团队代码风格一致。 - **ESLint**:除了格式化,它还具有代码检查功能,可以...

    JS格式化工具

    6. **代码压缩**:对于生产环境,为了减少文件大小和加载时间,格式化工具可以将代码压缩为一行,移除不必要的空白和注释。 7. **代码风格转换**:允许用户在不同编码风格之间切换,例如ESLint、Airbnb等风格指南。...

    格式化及压缩js文件

    在JavaScript开发过程中,为了优化网页性能,我们经常会对JS文件进行格式化和压缩。格式化主要是为了让代码更易读、易维护,而压缩则是为了减少文件大小,加快页面加载速度。下面将详细介绍这两个过程。 首先,**...

    js 格式化工具,压缩

    js 格式化,压缩支持几种风格~

    2024 优化版 站长工具源码/网页工具箱/JS格式化/压缩网站源码自适应模板带管理后台

    2024.07月更新 请看演示 增加加了100多查询娱乐功能 这个是带手机版TOOL在线网页工具箱/站长工具源码/在线加密解密...JS格式化/压缩 JS加密/解密 JS代码混合加密 SQL压缩/格式化 PHP代码格式化工具 XML压缩/格

    js压缩和格式化

    二、JavaScript格式化 格式化JavaScript代码是为了提高代码可读性,便于团队协作和维护。这通常涉及添加适当的缩进、换行和空格,以及统一代码风格。 1. 格式化工具 - Prettier:一款流行的代码格式化工具,它可以...

    JS文件格式化工具

    总的来说,JS文件格式化工具是JavaScript开发中的重要辅助工具,它通过自动化代码格式化,提升了代码质量和开发效率。JavaScript Code Improver作为其中的一员,旨在帮助开发者实现这一目标,提供一个更加友好、规范...

    好用的本地json格式化小工具,无需联网

    因此,JSON格式化工具就显得尤为重要。 标题提到的“好用的本地json格式化小工具”是针对这一需求而设计的,它允许用户在离线环境下对JSON数据进行美化,使其更易读。这类工具通常具有以下功能: 1. **格式化**:...

    离线json格式化查看工具下载

    1. **格式化**:当JSON数据以紧凑的形式(单行无缩进)存在时,格式化工具可以帮助我们将这些数据转换成清晰易读的多行格式,通过增加缩进来提高可读性。在描述中提到,只需复制粘贴JSON数据到工具中,点击“Format...

    JS格式化工具,只要把复杂JS代码输入页面点击生成就可以看标准的JS格式化代码

    JS格式化工具的主要功能是将复杂的、未格式化的JavaScript代码整理成清晰、标准的格式。它通过自动添加缩进、换行、括号对齐等方式,使得代码结构更易于人眼识别,从而提高开发者的阅读效率和代码质量。这对于团队...

    json离线格式化工具

    在处理大量JSON数据时,为了便于理解和调试,我们通常会需要使用JSON格式化工具。 "json离线格式化工具"是一款专为处理JSON数据设计的实用工具,它允许用户在没有网络连接的情况下对JSON数据进行美化和解析。这个...

    前端工具 - js格式化,压缩,解压工具

    js格式化效果,实现js的格式化,压缩,解压,加密压缩

    JS代码格式化工具网页

    4. **Google Closure Compiler**:谷歌提供的这款工具除了压缩和优化代码,还包含了一个格式化器,可以将JS代码整理得规整一致。 5. **Atom-beautify** 和 **Sublime Text的JsFormat**:这些是针对特定编辑器的插件...

Global site tag (gtag.js) - Google Analytics