Js整理工具-开发必备

Javascript整理工具,请将下列代码保存为Html格式即可。

<!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;charset=utf-8" />
    <title>JavaScript整理工具</title>
</head>
<body>

    <script type="text/javascript">
function JsDecoder()
{
    this.s = "";
    this.len = 0;
    this.i = 0;
    this.lvl = 0;
    this.code = [""];
    this.row = 0;
    this.switches = [];
    this.lastWord = "";
    this.nextChar = "";
    this.prevChar = "";
    this.isAssign = false;
    this.decode = function ()
    {
        this.s = this.s.replace(/[\r\n\f]+/g, "\n");
        this.len = this.s.length;
        while (this.i < this.len) {
            var a = this.s.charAt(this.i);
            this.charInit();
            this.switch_c(a);
            this.i++
        }
        return this.code.join("\n");
    };
    this.switch_c = function (a)
    {
        switch (a)
        {
            case "\n":
                this.linefeed();
                break;
            case " ":
            case "\t":
                this.space();
                break;
            case "{":
                this.blockBracketOn();
                break;
            case "}":
                this.blockBracketOff();
                break;
            case ":":
                this.colon();
                break;
            case ";":
                this.semicolon();
                break;
            case "(":
                this.bracketOn();
                break;
            case ")":
                this.bracketOff();
                break;
            case "[":
                this.squareBracketOn();
                break;
            case "]":
                this.squareBracketOff();
                break;
            case '"':
            case "'":
                this.quotation(a);
                break;
            case "/":
                if ("/" == this.nextChar) {
                    this.lineComment()
                }
                else {
                    if ("*" == this.nextChar) {
                        this.comment()
                    }
                    else {
                        this.slash()
                    }
                }
                break;
            case ",":
                this.comma();
                break;
            case ".":
                this.dot();
                break;
            case "~":
            case "^":
                this.symbol1(a);
                break;
            case "-":
            case "+":
            case "*":
            case "%":
            case "<":
            case "=":
            case ">":
            case "?":
            case ":":
            case "&":
            case "|":
            case "/":
                this.symbol2(a);
                break;
            case "!":
                if ("=" == this.nextChar) {
                    this.symbol2(a)
                }
                else {
                    this.symbol1(a)
                }
                break;
            default:
                if (/\w/.test(a)) {
                    this.alphanumeric(a)
                }
                else {
                    this.unknown(a)
                }
                break
        }
        a = this.s.charAt(this.i);
        if (!/\w/.test(a)) {
            this.lastWord = "";
        }
    };
    this.blockBracketOn = function ()
    {
        this.isAssign = false;
        var f = this.nextNonWhite(this.i);
        if ("}" == f)
        {
            var c = (this.prevChar == ")" ? " " : "");
            this.write(c + "{");
            this.lvl++;
            return
        }
        if (/^\s*switch\s/.test(this.getCurrentLine())) {
            this.switches.push(this.lvl)
        }
        var a = this.getCurrentLine();
        var e = this.row;
        var d = /(,)\s*(\w+\s*:\s*function\s*\([^\)]*\)\s*)$/;
        if (d.test(a))
        {
            this.replaceLine(this.code[e].replace(d, "$1"));
            this.writeLine();
            var b = d.exec(a);
            this.write(b[2])
        }
        if (/^\s*return\s*/.test(this.code[this.row]))
        {
            if (/^\s*return\s+\w+/.test(this.code[this.row])) {
                this.writeLine()
            }
            else {
                if (this.prevChar != " ") {
                    this.write(" ")
                }
            }
            this.write("{");
            this.writeLine();
            this.lvl++;
            return
        }
        if (/function\s*/.test(this.code[this.row]) || this.isBlockBig())
        {
            this.writeLine()
        }
        else {
            if (this.prevChar != " " && this.prevChar != "\n" && this.prevChar != "(") {
                this.write(" ")
            }
        }
        this.write("{");
        this.lvl++;
        if ("{" != f) {
            this.writeLine()
        }
    };
    this.isBlockBig = function ()
    {
        var b = this.i + 1;
        var d = 0;
        var e = 0;
        var a = 0;
        while (b < this.len - 1) {
            b++;
            var f = this.s.charAt(b);
            if (/\s/.test(f)) {
                continue
            }
            if ("}" == f && e == a) {
                break
            }
            if ("{" == f) {
                e++
            }
            if ("}" == f) {
                a++
            }
            d++;
            if (d > 80) {
                return true;
            }
        }
        return (d > 80);
    };
    this.blockBracketOff = function ()
    {
        var l = this.nextNonWhite(this.i);
        var k = this.prevNonWhite(this.i);
        var n = this.getCurrentLine();
        if (k != "{") {
            if (n.length && l != ";" && l != "}" && l != ")" && l != ",") {
                this.writeLine()
            }
            else {
                if (n.length && k != ";" && l == "}" && this.isAssign) {
                    this.semicolon()
                }
                else
                {
                    if (n.length && this.isAssign && k != ";") {
                        this.semicolon()
                    }
                    else
                    {
                        if (n.length && k != ";") {
                            if (/^\s*(else)?\s*return[\s(]+/i.test(n)) {
                                this.semicolon()
                            }
                            else {
                                this.writeLine()
                            }
                        }
                    }
                }
            }
        }
        this.write("}");
        if ("," == l) {
            this.write(",");
            this.goNextNonWhite()
        }
        var j = this.nextManyNW(3);
        if (j == "(),") {
            this.write("(),");
            this.goNextManyNW("(),");
            this.writeLine()
        }
        else
        {
            if (j == "();") {
                this.write("();");
                this.goNextManyNW("();");
                this.writeLine()
            }
            else
            {
                if (j == "():") {
                    this.write("()");
                    this.goNextManyNW("()");
                    this.write(" : ");
                    this.goNextNonWhite()
                }
                else
                {
                    if ("{" == k)
                    {
                        if ("," == l && this.getCurrentLine().length < 80) {
                            this.write(" ")
                        }
                        else {
                            if (this.nextWord() || "}" == l) {
                                this.writeLine()
                            }
                        }
                    }
                    else
                    {
                        if (")" != l && "]" != l)
                        {
                            if ("," == l &&/^[\s\w,]+\)/.test(this.s.substr(this.i, 20))) {
                                this.write(" ")
                            }
                            else {
                                this.writeLine()
                            }
                        }
                    }
                }
            }
        }
        this.lvl--;
        if (this.switches.length && this.switches[this.switches.length - 1] == this.lvl)
        {
            var m = this.row - 1;
            var e = str_repeat(" ", this.lvl * 4);
            var c = str_repeat(" ", (this.lvl + 1) * 4);
            var g = new RegExp("^" + e + "(switch\\s|{)");
            var d = new RegExp("^" + c + "(case|default)[\\s:]");
            var b = new RegExp("^" + c + "[^\\s]");
            while (m > 0)
            {
                m--;
                if (g.test(this.code[m])) {
                    break
                }
                if (d.test(this.code[m])) {
                    continue
                }
                this.replaceLine("    " + this.code[m], m)
            }
            this.switches.pop()
        }
        if (this.sub) {
            return
        }
        var h = /^(\s*else\s*if)\s*\(/;
        var f = /^(\s*else)\s+[^{]+/;
        var a = this.s.substr(this.i + 1, 100);
        if (h.test(a))
        {
            this.i += h.exec(a)[1].length;
            this.write("else if");
            this.lastWord = "if";
            this.fixSub("else if")
        }
        else
        {
            if (f.test(a))
            {
                this.i += f.exec(a)[1].length;
                this.write("else");
                this.lastWord = "else";
                this.fixSub("else")
            }
        }
    };
    this.bracketOn = function ()
    {
        if (this.isKeyword() && this.prevChar != " " && this.prevChar != "\n") {
            this.write(" (")
        }
        else {
            this.write("(")
        }
    };
    this.bracketOff = function ()
    {
        this.write(")");
        if (this.sub) {
            return
        }
        var j = new RegExp("^\\s*(if|for|while|do)\\s*\\([^{}]+\\)$", "i");
        var l = this.getCurrentLine();
        if (j.test(l))
        {
            var h = this.nextNonWhite(this.i);
            if ("{" != h && ";" != h && ")" != h)
            {
                var b = 0;
                var f = 0;
                var g = false;
                var d = false;
                var e = false;
                for (var a = 0; a < l.length; a++)
                {
                    if (l.charAt(a) == "(") {
                        g = true;
                        b++
                    }
                    if (l.charAt(a) == ")") {
                        f++;
                        if (g && b == f) {
                            if (a == l.length - 1) {
                                e = true
                            }
                            else {
                                break
                            }
                        }
                    }
                }
                if (e) {
                    this.fixSub(j.exec(l)[1])
                }
            }
        }
    };
    this.sub = false;
    this.orig_i = null;
    this.orig_lvl = null;
    this.orig_code = null;
    this.orig_row = null;
    this.orig_switches = null;
    this.restoreOrig = function (a)
    {
        this.sub = false;
        if (!a) {
            this.i = this.orig_i
        }
        this.lvl = this.orig_lvl;
        this.code = this.orig_code;
        this.row = this.orig_row;
        this.switches = this.orig_switches;
        this.prevCharInit();
        this.lastWord = "";
        this.charInit();
        this.isAssign = false;
    };
    this.combineSub = function ()
    {
        for (i = 0; i < this.code.length; i++)
        {
            var a = this.orig_code[this.orig_row];
            if (0 == i && a.length)
            {
                if (a.substr(a.length - 1, 1) != " ") {
                    this.orig_code[this.orig_row] += " "
                }
                this.orig_code[this.orig_row] += this.code[i].trim()
            }
            else {
                this.orig_code[this.orig_row + i] = this.code[i];
            }
        }
    };
    this.fixSub = function (d)
    {
        if (this.sub) {
            return
        }
        if ("{" == this.nextNonWhite(this.i)) {
            return
        }
        var l = this.nextWord();
        this.orig_i = this.i;
        this.orig_lvl = this.lvl;
        this.orig_code = this.code;
        this.orig_row = this.row;
        this.orig_switches = this.switches;
        this.sub = true;
        this.code = [""];
        this.prevChar = "";
        this.row = 0;
        this.switches = [];
        this.isAssign = false;
        this.i++;
        var k = 0;
        var h = 0;
        var g = 0;
        if ("else if" == d) {
            var e = false
        }
        var m = false;
        var b = false;
        var j = false;
        while (!m && this.i < this.len)
        {
            var f = this.s.charAt(this.i);
            this.charInit();
            switch (f)
            {
                case "{":
                    k++;
                    break;
                case "}":
                    k--;
                    if (0 == k && 0 == h && 0 == g && this.lvl - 1 == this.orig_lvl)
                    {
                        var a = this.nextWord();
                        if ("switch" == l) {
                            m = true;
                            break
                        }
                        if ("try" == l && "catch" == b) {
                            m = true;
                            break
                        }
                        if ("while" == l && "do" == b) {
                            m = true;
                            break
                        }
                        if ("if" == l) {}
                        if ("if" == d && "else" == a && "if" != l) {
                            m = true;
                            break
                        }
                        b = a
                    }
                    break;
                case "(":
                    h++;
                    break;
                case ")":
                    h--;
                    if ("else if" == d && 0 == h && !e)
                    {
                        if (this.nextNonWhite(this.i) == "{") {
                            this.write(f);
                            this.combineSub();
                            this.restoreOrig(true);
                            return
                        }
                        this.write(f);
                        this.combineSub();
                        this.restoreOrig(true);
                        this.fixSub("if");
                        return
                    }
                    break;
                case "[":
                    g++;
                    break;
                case "]":
                    g--;
                    break;
                case ";":
                    if (0 == k && 0 == h && 0 == g && this.lvl == this.orig_lvl && "if" != l) {
                        m = true
                    }
                    break
            }
            if (-1 == k && h == 0 && g == 0 && this.prevNonWhite(this.i) != "}") {
                this.write(";");
                this.i--;
                m = true
            }
            else {
                if (k < 0 || h < 0 || g < 0) {
                    m = false;
                    break
                }
                else {
                    this.switch_c(f)
                }
            }
            this.i++
        }
        this.i--;
        if (m)
        {
            this.s = this.s.substr(0, this.orig_i + 1) + "{" + this.code.join("\n") + "}" + this.s.substr(this.i + 1);
            this.len = this.s.length
        }
        this.restoreOrig(false);
    };
    this.squareBracketOn = function ()
    {
        this.checkKeyword();
        this.write("[")
    };
    this.squareBracketOff = function ()
    {
        this.write("]")
    };
    this.isKeyword = function ()
    {
        return this.lastWord.length && this.keywords.indexOf(this.lastWord) !=- 1;
    };
    this.linefeed = function () {};
    this.space = function ()
    {
        if (!this.prevChar.length) {
            return
        }
        if (" " == this.prevChar || "\n" == this.prevChar) {
            return
        }
        if ("}" == this.prevChar && "]" == this.nextChar) {}
        this.write(" ");
        return
    };
    this.checkKeyword = function ()
    {
        if (this.isKeyword() && this.prevChar != " " && this.prevChar != "\n") {
            this.write(" ")
        }
    };
    this.nextWord = function ()
    {
        var a = this.i;
        var b = "";
        while (a < this.len - 1)
        {
            a++;
            var d = this.s.charAt(a);
            if (b.length) {
                if (/\s/.test(d)) {
                    break
                }
                else {
                    if (/\w/.test(d)) {
                        b += d
                    }
                    else {
                        break
                    }
                }
            }
            else {
                if (/\s/.test(d)) {
                    continue
                }
                else {
                    if (/\w/.test(d)) {
                        b += d
                    }
                    else {
                        break
                    }
                }
            }
        }
        if (b.length) {
            return b
        }
        return false;
    };
    this.nextManyNW = function (e)
    {
        var a = "";
        var b = this.i;
        while (b < this.len - 1) {
            b++;
            var d = this.s.charAt(b);
            if (!/^\s+$/.test(d)) {
                a += d;
                if (a.length == e) {
                    return a;
                }
            }
        }
        return false;
    };
    this.goNextManyNW = function (e)
    {
        var a = "";
        var b = this.i;
        while (b < this.len - 1)
        {
            b++;
            var d = this.s.charAt(b);
            if (!/^\s+$/.test(d))
            {
                a += d;
                if (a == e) {
                    this.i = b;
                    this.charInit();
                    return true
                }
                if (a.length >= e.length) {
                    return false;
                }
            }
        }
        return false;
    };
    this.nextNonWhite = function (a)
    {
        while (a < this.len - 1) {
            a++;
            var b = this.s.charAt(a);
            if (!/^\s+$/.test(b)) {
                return b;
            }
        }
        return false;
    };
    this.prevNonWhite = function (a)
    {
        while (a > 0) {
            a--;
            var b = this.s.charAt(a);
            if (!/^\s+$/.test(b)) {
                return b;
            }
        }
        return false;
    };
    this.goNextNonWhite = function ()
    {
        var a = this.i;
        while (a < this.len - 1)
        {
            a++;
            var b = this.s.charAt(a);
            if (!/^\s+$/.test(b)) {
                this.i = a;
                this.charInit();
                return true;
            }
        }
        return false;
    };
    this.colon = function ()
    {
        var a = this.getCurrentLine();
        if (/^\s*case\s/.test(a) ||/^\s*default$/.test(a)) {
            this.write(":");
            this.writeLine()
        }
        else {
            this.symbol2(":")
        }
    };
    this.isStart = function ()
    {
        return this.getCurrentLine().length === 0;
    };
    this.backLine = function ()
    {
        if (!this.isStart) {
            throw "backLine() may be called only at the start of the line"
        }
        this.code.length = this.code.length - 1;
        this.row--
    };
    this.semicolon = function ()
    {
        this.isAssign = false;
        if (this.isStart()) {
            this.backLine()
        }
        this.write(";");
        if (/^\s*for\s/.test(this.getCurrentLine())) {
            this.write(" ")
        }
        else {
            this.writeLine()
        }
    };
    this.quotation = function (a)
    {
        this.checkKeyword();
        var b = false;
        this.write(a);
        while (this.i < this.len - 1)
        {
            this.i++;
            var d = this.s.charAt(this.i);
            if ("\\" == d) {
                b = (b ? false : true)
            }
            this.write(d);
            if (d == a) {
                if (!b) {
                    break
                }
            }
            if ("\\" != d) {
                b = false;
            }
        }
    };
    this.lineComment = function ()
    {
        this.write("//");
        this.i++;
        while (this.i < this.len - 1)
        {
            this.i++;
            var a = this.s.charAt(this.i);
            if ("\n" == a) {
                this.writeLine();
                break
            }
            this.write(a)
        }
    };
    this.comment = function ()
    {
        this.write("/*");
        this.i++;
        var b = "";
        var a = "";
        while (this.i < this.len - 1)
        {
            this.i++;
            a = b;
            b = this.s.charAt(this.i);
            if (" " == b || "\t" == b || "\n" == b)
            {
                if (" " == b) {
                    if (this.getCurrentLine().length > 100) {
                        this.writeLine()
                    }
                    else {
                        this.write(" ", true)
                    }
                }
                else {
                    if ("\t" == b) {
                        this.write("    ", true)
                    }
                    else {
                        if ("\n" == b) {
                            this.writeLine()
                        }
                    }
                }
            }
            else {
                this.write(b, true)
            }
            if ("/" == b && "*" == a) {
                break
            }
        }
        this.writeLine()
    };
    this.slash = function ()
    {
        var f = this.i - 1;
        var j = this.s.charAt(f);
        for (f = this.i - 1; f >= 0; f--) {
            var e = this.s.charAt(f);
            if (" " == e || "\t" == e) {
                continue
            }
            j = this.s.charAt(f);
            break
        }
        var l = /^\w+$/.test(j) || "]" == j || ")" == j;
        var k = ("*" == this.prevChar);
        if (l || k)
        {
            if (l)
            {
                if ("=" == this.nextChar) {
                    var m = this.prevChar == " " ? "" : " ";
                    this.write(m + "/")
                }
                else {
                    this.write(" / ")
                }
            }
            else {
                if (k) {
                    this.write("/ ")
                }
            }
        }
        else
        {
            if (")" == this.prevChar) {
                this.write(" / ")
            }
            else
            {
                var g = "";
                if ("=" == this.prevChar || ":" == this.prevChar) {
                    g += " /"
                }
                else {
                    g += "/"
                }
                var d = false;
                while (this.i < this.len - 1)
                {
                    this.i++;
                    var h = this.s.charAt(this.i);
                    if ("\\" == h) {
                        d = (d ? false : true)
                    }
                    g += h;
                    if ("/" == h) {
                        if (!d) {
                            break
                        }
                    }
                    if ("\\" != h) {
                        d = false;
                    }
                }
                this.write(g)
            }
        }
    };
    this.comma = function ()
    {
        this.write(", ");
        var a = this.getCurrentLine();
        if (a.replace(" ", "").length > 100) {
            this.writeLine()
        }
    };
    this.dot = function ()
    {
        this.write(".")
    };
    this.symbol1 = function (a)
    {
        if ("=" == this.prevChar && "!" == a) {
            this.write(" " + a)
        }
        else {
            this.write(a)
        }
    };
    this.symbol2 = function (d)
    {
        if ("+" == d || "-" == d) {
            if (d == this.nextChar || d == this.prevChar) {
                this.write(d);
                return
            }
        }
        var a = (this.prevChar == " " ? "" : " ");
        var b = " ";
        if ("(" == this.prevChar) {
            a = "";
            b = ""
        }
        if ("-" == d && (">" == this.prevChar || ">" == this.prevChar)) {
            this.write(" " + d);
            return
        }
        if (this.symbols2.indexOf(this.prevChar) !=- 1)
        {
            if (this.symbols2.indexOf(this.nextChar) !=- 1) {
                this.write(d + (this.nextChar == "!" ? " " : ""))
            }
            else {
                this.write(d + b)
            }
        }
        else
        {
            if (this.symbols2.indexOf(this.nextChar) !=- 1) {
                this.write(a + d)
            }
            else {
                this.write(a + d + b)
            }
        }
        if ("=" == d &&/^[\w\]]$/.test(this.prevNonWhite(this.i)) &&/^[\w\'\"\[]$/.test(this.nextNonWhite(this.i))) {
            this.isAssign = true;
        }
    };
    this.alphanumeric = function (a)
    {
        if (this.lastWord) {
            this.lastWord += a
        }
        else {
            this.lastWord = a
        }
        if (")" == this.prevChar) {
            a = " " + a
        }
        this.write(a);
    };
    this.unknown = function (a)
    {
        this.write(a)
    };
    this.charInit = function ()
    {
        if (this.len - 1 === this.i) {
            this.nextChar = ""
        }
        else {
            this.nextChar = this.s.charAt(this.i + 1);
        }
    };
    this.write = function (d, b)
    {
        if (b)
        {
            if (!/\s/.test(d))
            {
                if (this.code[this.row].length < this.lvl * 4) {
                    this.code[this.row] += str_repeat(" ", this.lvl * 4 - this.code[this.row].length)
                }
            }
            this.code[this.row] += d
        }
        else
        {
            if (0 === this.code[this.row].length) {
                var a = ("}" == d ? this.lvl - 1 : this.lvl);
                for (var c = 0; c < a; c++) {
                    this.code[this.row] += "    "
                }
                this.code[this.row] += d
            }
            else {
                this.code[this.row] += d
            }
        }
        this.prevCharInit()
    };
    this.writeLine = function ()
    {
        this.code.push("");
        this.row++;
        this.prevChar = "\n";
    };
    this.replaceLine = function (a, b)
    {
        if ("undefined" == typeof b) {
            b = false
        }
        if (b !== false) {
            if (!/^\d+$/.test(b) || b < 0 || b > this.row) {
                throw "replaceLine() failed: invalid row=" + b
            }
        }
        if (b !== false) {
            this.code[b] = a
        }
        else {
            this.code[this.row] = a
        }
        if (b === false || b == this.row) {
            this.prevCharInit()
        }
    };
    this.prevCharInit = function ()
    {
        this.prevChar = this.code[this.row].charAt(this.code[this.row].length - 1);
    };
    this.writeTab = function ()
    {
        this.write("    ");
        this.prevChar = " ";
    };
    this.getCurrentLine = function ()
    {
        return this.code[this.row];
    };
    this.symbols1 = "~!^";
    this.symbols2 = "-+*%<=>?:&|/!";
    this.keywords = ["abstract", "boolean", "break", "byte", "case", "catch", "char", "class", "const", 
    "continue", "default", "delete", "do", "double", "else", "extends", "false", "final", "finally", "float", 
    "for", "function", "goto", "if", "implements", "import", "in", "instanceof", "int", "interface", "long", 
    "native", "new", "null", "package", "private", "protected", "public", "return", "short", "static", 
    "super", "switch", "synchronized", "this", "throw", "throws", "transient", "true", "try", "typeof", 
    "var", "void", "while", "with"]
}
if (typeof Array.prototype.indexOf == "undefined")
{
    Array.prototype.indexOf = function (b)
    {
        for (var a = 0; a < this.length; a++) {
            if ((typeof this [a] == typeof b) && (this [a] == b)) {
                return a;
            }
        }
        return - 1;
    }
}
if (!String.prototype.trim) {
    String.prototype.trim = function ()
    {
        return this.replace(/^\s*|\s*$/g, "");
    }
}
function str_repeat(c, b)
{
    ret = "";
    for (var a = 0; a < b; a++) {
        ret += c
    }
    return ret
}
var debug_w;
function debug(d, a)
{
    if (!debug_w)
    {
        var b = 600;
        var k = 600;
        var j = (screen.width / 2 - b / 2);
        var h = (screen.height / 2 - k / 2);
        debug_w = window.open("", "", "scrollbars=yes,resizable=yes,width=" + b + ",height=" + k + ",screenX=" + (j) + ",screenY=" + h + ",left=" + j + ",top=" + h);
        debug_w.document.open();
        debug_w.document.write("<html><head><style>body{margin: 1em;padding: 0;font-family: courier new; font-size: 12px;}h1,h2{margin: 0.2em 0;}</style></head><body><h1>Debug</h1></body></html>");
        debug_w.document.close()
    }
    var g = "";
    if ("undefined" !== typeof a && a.length) {
        g = "<h2>" + a + "</h2>\n"
    }
    if ("object" === typeof d) {
        for (var c = 0; c < d.length; c++) {
            g += "[" + c + "] => " + d[c] + "\n"
        }
    }
    else {
        if ("string" == typeof d) {
            g += d
        }
        else {
            try {
                g += d.toString()
            }
            catch (f) {}
            g += " (" + typeof d + ")"
        }
    }
    debug_w.document.body.innerHTML += "<pre>" + g + "</pre>"
};
    </script>

    <script type="text/javascript">
function JsColorizer()
{
    this.color = {
        keyword : "#0000FF", object : "#FF0000", quotation : "#FF00FF", comment : "#008000"
    };
    this.s = "";
    this.i = 0;
    this.len = 0;
    this.ret = "";
    this.lastWord = "";
    this.nextChar = "";
    this.prevChar = "";
    this.code = [""];
    this.row = 0;
    this.times = 
    {
        quotation : 0, quotation_calls : 0, lineComment : 0, lineComment_calls : 0, comment : 0, comment_calls : 0, 
        slash : 0, slash_calls : 0, word : 0, word_calls : 0
    };
    this.write = function (a)
    {
        this.code[this.row] += a;
        if (a.length == 1) {
            this.prevChar = a
        }
        else {
            this.prevCharInit()
        }
    };
    this.writeLine = function ()
    {
        this.code.push("");
        this.row++;
        this.prevChar = "\n";
    };
    this.prevCharInit = function ()
    {
        this.prevChar = this.code[this.row].charAt(this.code[this.row].length - 1);
    };
    this.showTimes = function ()
    {
        var a = "";
        for (var c in this.times)
        {
            var b = this.times[c];
            if (/_calls/.test(c)) {
                a += c + ": " + b + "\n"
            }
            else {
                a += c + ": " + time_round(b) + " sec\n"
            }
        }
        return a;
    };
    this.colorize = function ()
    {
        this.len = this.s.length;
        while (this.i < this.len)
        {
            var a = this.s.charAt(this.i);
            if (this.len - 1 == this.i) {
                this.nextChar = ""
            }
            else {
                this.nextChar = this.s.charAt(this.i + 1)
            }
            switch (a)
            {
                case "\n":
                    if (this.lastWord.length) {
                        this.word()
                    }
                    this.lastWord = "";
                    this.writeLine();
                    break;
                case "'":
                case '"':
                    if (this.lastWord.length) {
                        this.word()
                    }
                    this.lastWord = "";
                    this.quotation(a);
                    break;
                case "/":
                    if (this.lastWord.length) {
                        this.word()
                    }
                    this.lastWord = "";
                    if ("/" == this.nextChar) {
                        this.lineComment()
                    }
                    else {
                        if ("*" == this.nextChar) {
                            this.comment()
                        }
                        else {
                            this.slash()
                        }
                    }
                    break;
                default:
                    if (/^\w$/.test(a)) {
                        this.lastWord += a
                    }
                    else {
                        if (this.lastWord.length) {
                            this.word()
                        }
                        this.lastWord = "";
                        this.write(a)
                    }
                    break
            }
            this.i++
        }
        this.write(this.lastWord);
        return this.code.join("\n");
    };
    this.quotation = function (a)
    {
        var b = a;
        var d = false;
        while (this.i < this.len - 1)
        {
            this.i++;
            var e = this.s.charAt(this.i);
            if ("\\" == e) {
                d = (d ? false : true)
            }
            b += e;
            if (e == a) {
                if (!d) {
                    break
                }
            }
            if ("\\" != e) {
                d = false;
            }
        }
        this.write('<font color="' + this.color.quotation + '">' + b + "</font>")
    };
    this.lineComment = function ()
    {
        var a = "//";
        this.i++;
        while (this.i < this.len - 1) {
            this.i++;
            var b = this.s.charAt(this.i);
            a += b;
            if ("\n" == b) {
                break
            }
        }
        this.write('<font color="' + this.color.comment + '">' + a + "</font>")
    };
    this.comment = function ()
    {
        var a = "/*";
        this.i++;
        var d = "";
        var b = "";
        while (this.i < this.len - 1) {
            this.i++;
            b = d;
            d = this.s.charAt(this.i);
            a += d;
            if ("/" == d && "*" == b) {
                break
            }
        }
        this.write('<font color="' + this.color.comment + '">' + a + "</font>")
    };
    this.slash = function ()
    {
        var h = this.i - 1;
        var k = this.s.charAt(h);
        for (h = this.i - 1; h >= 0; h--) {
            var g = this.s.charAt(h);
            if (" " == g || "\t" == g) {
                continue
            }
            k = this.s.charAt(h);
            break
        }
        var e = /^\w+$/.test(k) || "]" == k || ")" == k;
        var d = ("*" == this.prevChar);
        if (e || d)
        {
            if (e) {
                if ("=" == this.nextChar) {
                    this.write("/")
                }
                else {
                    this.write("/")
                }
            }
            else {
                if (d) {
                    this.write("/")
                }
            }
        }
        else
        {
            if (")" == this.prevChar) {
                this.write("/")
            }
            else
            {
                var f = "";
                if ("=" == this.prevChar) {
                    f += "/"
                }
                else {
                    f += "/"
                }
                var i = false;
                while (this.i < this.len - 1)
                {
                    this.i++;
                    var j = this.s.charAt(this.i);
                    if ("\\" == j) {
                        i = (i ? false : true)
                    }
                    f += j;
                    if ("/" == j) {
                        if (!i) {
                            break
                        }
                    }
                    if ("\\" != j) {
                        i = false;
                    }
                }
                this.write('<font color="' + this.color.quotation + '">' + f + "</font>")
            }
        }
    };
    this.word = function ()
    {
        if (this.keywords.indexOf(this.lastWord) !=- 1) {
            this.write('<font color="' + this.color.keyword + '">' + this.lastWord + "</font>")
        }
        else
        {
            if (this.objects.indexOf(this.lastWord) !=- 1) {
                this.write('<font color="' + this.color.object + '">' + this.lastWord + "</font>")
            }
            else {
                this.write(this.lastWord)
            }
        }
    };
    this.keywords = ["abstract", "boolean", "break", "byte", "case", "catch", "char", "class", "const", 
    "continue", "default", "delete", "do", "double", "else", "extends", "false", "final", "finally", "float", 
    "for", "function", "goto", "if", "implements", "import", "in", "instanceof", "int", "interface", "long", 
    "native", "new", "null", "package", "private", "protected", "public", "return", "short", "static", 
    "super", "switch", "synchronized", "this", "throw", "throws", "transient", "true", "try", "typeof", 
    "var", "void", "while", "with"];
    this.objects = ["Anchor", "anchors", "Applet", "applets", "Area", "Array", "Button", "Checkbox", "Date", 
    "document", "FileUpload", "Form", "forms", "Frame", "frames", "Hidden", "history", "Image", "images", 
    "Link", "links", "Area", "location", "Math", "MimeType", "mimeTypes", "navigator", "options", "Password", 
    "Plugin", "plugins", "Radio", "Reset", "Select", "String", "Submit", "Text", "Textarea", "window"];
};
    </script>

    <script type="text/javascript">
var base_code = "";
var jsdecoder;
var jscolorizer;
var code = "";
var time = 0;
function decode()
{
    code = "";
    base_code = "";
    jsdecoder = new JsDecoder();
    jscolorizer = new JsColorizer();
    if ($("msg").innerHTML.length) {
        do_clean_init()
    }
    else {
        jsdecoder.s = $("a1").value;
        do_decode_init()
    }
}
function do_decode_init()
{
    $("msg").innerHTML += "整理 .. ";
    setTimeout(do_decode, 50)
}
function do_decode()
{
    time = time_start();
    try {
        code = jsdecoder.decode();
        base_code = code
    }
    catch (a) {
        $("msg").innerHTML += "error<br><br>" + new String(a).replace(/\n/g, "<br>");
        return
    }
    $("msg").innerHTML += "完成 (" + time_end(time) + " 秒)<br>";
    setTimeout(do_colorize_init, 50)
}
function do_colorize_init()
{
    $("msg").innerHTML += "上色 .. ";
    setTimeout(do_colorize, 50)
}
function do_colorize()
{
    time = time_start();
    code = code.replace(/&/g, "&amp;");
    code = code.replace(/</g, "&lt;");
    code = code.replace(/>/g, "&gt;");
    jscolorizer.s = code;
    try {
        code = jscolorizer.colorize()
    }
    catch (a) {
        $("msg").innerHTML += "error<br><br>" + new String(a).replace(/\n/g, "<br>");
        return
    }
    $("msg").innerHTML += "完成 (" + time_end(time) + " 秒)<br>";
    setTimeout(do_insert_init, 50)
}
function do_insert_init()
{
    $("msg").innerHTML += "显示代码 .. ";
    setTimeout(do_insert, 50)
}
function do_insert()
{
    time = time_start();
    try
    {
        code = new String(code);
        code = code.replace(/(\r\n|\r|\n)/g, "<br>\n");
        code = code.replace(/<font\s+/gi, "<font@@@@@");
        code = code.replace(/( |\t)/g, "&nbsp;");
        code = code.replace(/<font@@@@@/gi, "<font ");
        code = code.replace(/\n$/, "");
        var c = 0;
        var h = code.indexOf("\n");
        while (h !=- 1) {
            c++;
            h = code.indexOf("\n", h + 1)
        }
        c++;
        pad = new String(c).length;
        var a = "";
        for (var b = 0; b < c; b++)
        {
            var f = pad - new String(b + 1).length;
            var g = new String(b + 1);
            for (k = 0; k < f; k++) {
                g = "&nbsp;" + g
            }
            g += "&nbsp;";
            a += '<div style="background: #fff; color: #666;">' + g + "</div>"
        }
        $("lines").innerHTML = a;
        $("code_area").style.display = "block";
        $("sel_all").style.display = "block";
        $("a2").innerHTML = code
    }
    catch (d) {
        $("msg").innerHTML += "error<br><br>" + new String(d).replace(/\n/g, "<br>");
        return
    }
    $("msg").innerHTML += "完成 (" + time_end(time) + " 秒)";
    code = ""
}
function do_clean_init()
{
    $("msg").innerHTML = "";
    do_clean()
}
function do_clean()
{
    time = time_start();
    $("code_area").style.display = "none";
    base_code = "";
    $("sel_all").style.display = "none";
    $("insert_div").style.display = "none";
    jsdecoder.s = $("a1").value;
    do_decode_init()
}
function insert_textarea()
{
    $("insert_div").style.display = "block";
    $("insert_area").value = base_code;
    $("insert_area").focus();
    $("insert_area").select()
}
function $(a)
{
    return document.getElementById(a)
}
function time_micro()
{
    var a = new String(new Date().getTime());
    a = a.substr(0, a.length - 3) + "." + a.substr(a.length - 3, 3);
    return parseFloat(a)
}
function time_start()
{
    return time_micro()
}
function time_get(a)
{
    return time_micro() - a
}
function time_end(a)
{
    return time_round(time_micro() - a)
}
function time_round(a)
{
    a = Math.round(a * 100) / 100;
    if (a === 0) {
        a = 0.01
    }
    return a;
};
    </script>

    <div align="center">
        <textarea id="a1" style="height: 200px; width: 95%;">请把你需要格式化的内容粘贴在这里!</textarea></div>
    <p />
    <div align="center">
        <input type="button" onclick="decode()" value="格式化" />
    </div>
    <div id="msg" style="font-family: courier new; font-size: 12px; background: #ffffd7;
        margin: 1em 0; text-align: left">
    </div>
    <div id="sel_all" style="display: none; margin: 1em 0; text-align: left">
        <a href="javascript:void(0)" onclick="insert_textarea()"><u>在textarea中显示</u></a></div>
    <div id="insert_div" style="display: none; margin: 1em 0; text-align: center">
        <textarea id="insert_area" style="height: 300px; width: 95%;"></textarea></div>
    <div id="code_area" style="display: none;">
        <table cellspacing="0" cellpadding="0" style="font-family: courier new; font-size: 12px;"
            width="100%">
            <tr>
                <td valign="top" id="lines" width="10">
                </td>
                <td nowrap id="a2" style="background: #f5f5f5; text-align: left">
                </td>
            </tr>
        </table>
</html>

 

posted @ 2013-04-26 13:55  爱智旮旯  阅读(799)  评论(1编辑  收藏  举报