-
Notifications
You must be signed in to change notification settings - Fork 0
/
smart-js-minifier.html
143 lines (112 loc) · 2.54 KB
/
smart-js-minifier.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<html>
<head>
<title>
Smart JS Minifier
</title>
<style>
#input-js, #output-js {height:400px;width:100%;}
</style>
</head>
<body>
<textarea id='input-js'></textarea>
<button onclick='minify()'>Minify</button>
<textarea id='output-js'></textarea>
<script>
var input_txt="";
var output_txt="";
function minify()
{
input_txt = document.getElementById("input-js").value;
output_txt = input_txt;
remove_singleline_comments();
//document.getElementById("output-js").value = output_txt;
remove_multiline_comments();
document.getElementById("output-js").value = output_txt;
remove_newlines();
document.getElementById("output-js").value = output_txt;
input_txt="";
output_txt="";
}
function remove_singleline_comments()
{
var txt = output_txt;
console.log(txt.search("//"));
var index=0; var rem_txt = txt;
txt="";
while(rem_txt.search("//")>=0)
{
index = rem_txt.search("//");
var i=0;var rem=1;
var temp_txt = "";
temp_txt = rem_txt.slice(0,index+1);
//console.log("temp:"+temp_txt);
var num = 0;
if(temp_txt.search('"')>=0)
{
temp_txt = temp_txt.split('"');
num = temp_txt.length-1;
}
//console.log("num:"+num);
if(num>0)
{
if(num%2==0) //even
{
rem = 1;
}
else //odd
{
rem = 0;
}
}
else{
while(rem_txt.charAt(index+i)!="\n")
{
/*if((rem_txt.charAt(index+i)=="'")||(rem_txt.charAt(index+i)=='"'))
{rem=0;break;}*/
if((index+i)==rem_txt.length)
{rem=1;break;}
else
{rem = 1;}
i++;
}
}
if(rem==1)
{
txt = txt + rem_txt.slice(0,index);
rem_txt = rem_txt.slice(index+i,rem_txt.length);
}
else if(rem==0)
{
txt = txt + rem_txt.slice(0,index+2 );
rem_txt = rem_txt.slice(index+2,rem_txt.length);
}
//console.log(txt.search("//"));
//console.log(rem_txt);
}
output_txt = txt+rem_txt;
}
function remove_multiline_comments()
{
//return;
var txt = output_txt;
//console.log(txt)
while(txt.search("[/][*]")>=0)
{
var index1 = txt.search("[/][*]");
console.log("index1:"+index1);
//var temp = txt.slice(index1,txt.length);
var index2 = txt.search("[*][/]");
console.log("index2:"+index2);
txt = txt.slice(0,index1) + txt.slice(index2+2,txt.length);
}
output_txt = txt;
}
function remove_newlines()
{
output_txt = output_txt.replace(/[\n]/g,"");
output_txt = output_txt.replace(/ /g," ");
output_txt = output_txt.replace(/[\t]/g,"");
}
</script>
</body>
</html>