-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
211 lines (193 loc) · 4.69 KB
/
index.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>代码演示页面</title>
<style>
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
background-color: #f9f9fa;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
}
.container {
background-color: white;
padding: 30px;
border-radius: 12px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
width: 100%;
max-width: 1200px;
display: flex;
flex-direction: column;
gap: 20px;
}
h1 {
text-align: center;
color: #333;
}
.row {
display: flex;
gap: 20px;
}
.input-output-container {
flex: 1;
}
textarea {
width: 100%;
height: calc(100vh - 300px);
margin-bottom: 10px;
padding: 15px;
border: 1px solid #ddd;
border-radius: 8px;
resize: vertical;
font-family: 'Courier New', Courier, monospace;
font-size: 14px;
}
button {
width: 100%;
padding: 15px;
background-color: #007bff;
color: white;
border: none;
border-radius: 8px;
cursor: pointer;
font-size: 16px;
transition: background-color 0.3s ease;
margin: 0 10px;
}
button:hover {
background-color: #0056b3;
}
.result {
height: calc(100% - 55px);
margin-top: 10px;
padding: 15px;
border: 1px solid #eee;
border-radius: 8px;
background-color: #fafafa;
white-space: pre-wrap;
overflow-x: auto;
font-family: 'Courier New', Courier, monospace;
font-size: 14px;
color: #333;
}
h2 {
margin-bottom: 10px;
color: #555;
}
.flex {
display: flex;
}
.align-center {
align-items: center;
}
.justify-beteen {
justify-content: space-between;
}
</style>
</head>
<body>
<div class="container">
<div class="row">
<div class="input-output-container">
<h2>JSON</h2>
<textarea id="data" placeholder="JSON或JS对象">
</textarea>
</div>
<div class="input-output-container">
<div class="flex justify-beteen align-center">
<h2>压缩后文本</h2>
<small id="length"></small>
</div>
<textarea id="content" placeholder="压缩后文本"></textarea>
</div>
</div>
<div class="flex" style="margin: 0 -10px;">
<button id="compress">>> 压缩 >></button>
<button id="decompress"><< 解压 <<</button>
</div>
</div>
<script src="//unpkg.com/@kscript/require"></script>
<script type="module">
const jsonpack = await require('../index.js')
const dataElement = document.getElementById('data')
const contentElement = document.getElementById('content')
const compressElement = document.getElementById('compress')
const decompressElement = document.getElementById('decompress')
const lengthElement = document.getElementById('length')
const defaultData = {
a: 1,
aa: 11,
aaa: 111,
aaaa: {
b: 2,
bb: 22,
bbb: 222,
bbbb: {
a: 3,
aa: 33,
aa: 333
}
},
d: [
{
a: 1,
d: 4
},
{
aa: 11,
dd: 44
},
{
aaa: 111,
ddd: 444
},
{
aaaa: 1111,
dddd: 4444
},
{
aaaa: 1111,
dddd: 4444
},
{
aaaa: 1111,
dddd: 4444
}
]
}
const convert = (type) => {
if (type === 0) {
try {
const data = eval(`(${dataElement.value})`)
contentElement.value = jsonpack.compress(data)
contentElement.dispatchEvent(new Event('change'))
} catch (e) {
alert('参数错误, 压缩失败')
}
} else if (type === 1) {
const content = contentElement.value
if (content.length === 0) {
alert('空字符串, 解压失败')
}
dataElement.value = JSON.stringify(jsonpack.decompress(content), null, 2)
}
}
dataElement.value = JSON.stringify(defaultData, null, 2)
compressElement.addEventListener('click', () => convert(0))
decompressElement.addEventListener('click', () => convert(1))
contentElement.addEventListener('change', () => {
lengthElement.innerText = '压缩后长度:' + contentElement.value.length
})
</script>
</body>
</html>