-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextComparator.html
More file actions
307 lines (269 loc) · 8.45 KB
/
Copy pathTextComparator.html
File metadata and controls
307 lines (269 loc) · 8.45 KB
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>文本对比工具 - JustHTML 只需网页</title>
<script>
/**
* 动态加载(强制去除缓存) CSS 的函数
* @param {string} src - 要加载的 CSS 的 src 链接
* @returns {Promise} 返回一个 Promise 对象,用于处理异步加载
*/
function loadStyle(src) {
return new Promise((resolve, reject) => {
const link = document.createElement('link');
link.rel = 'stylesheet';
link.href = src + '?v=' + new Date().getTime(); // 使用时间戳避免缓存
link.async = true;
link.onload = resolve;
link.onerror = reject;
document.head.appendChild(link);
});
}
/**
* 动态加载(强制去除缓存) JS 的函数
* @param {string} src - 要加载的 JS 的 src 链接
* @returns {Promise} 返回一个 Promise 对象,用于处理异步加载
*/
function loadScript(src) {
return new Promise((resolve, reject) => {
const script = document.createElement('script');
script.src = src + '?v=' + new Date().getTime(); // 使用时间戳避免缓存
script.async = true;
script.onload = resolve;
script.onerror = reject;
document.head.appendChild(script);
});
}
</script>
<script>
loadStyle('../assets/style.css')
let scriptArray = [
"../assets/AddHeader_Tools.js",
"../assets/AddFooter.js",
"../assets/AddIco.js",
"../assets/AutoRun.js",
]
scriptArray.forEach(script => {
loadScript(script);
});
</script>
<script src="../package/Vue/code/vue.global.prod.js"></script>
<script type="module" src="../package/Sober/code/main.js"></script>
<style>
.compare-container {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 20px;
margin: 20px 0;
}
@media (max-width: 768px) {
.compare-container {
grid-template-columns: 1fr;
}
}
.input-group {
margin-bottom: 15px;
}
.input-group label {
display: block;
margin-bottom: 5px;
color: var(--accent-color);
}
.diff-result {
margin-top: 20px;
padding: 15px;
background-color: var(--bg-tertiary);
border-radius: var(--border-radius);
font-family: monospace;
white-space: pre-wrap;
}
.diff-added {
background-color: rgba(72, 199, 116, 0.3);
}
.diff-removed {
background-color: rgba(255, 107, 107, 0.3);
}
.stats-container {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
gap: 15px;
margin: 15px 0;
}
.stat-item {
padding: 10px;
background-color: var(--bg-secondary);
border-radius: var(--border-radius);
text-align: center;
}
.stat-value {
font-size: 1.5em;
font-weight: bold;
color: var(--accent-color);
}
.stat-label {
font-size: 0.9em;
color: var(--text-secondary);
}
</style>
</head>
<body>
<div id="app">
<div class="container">
<a href="../index.html" class="back-link">← 返回首页</a>
<div class="tool-section">
<h2>文本对比工具</h2>
<p>比较两个文本的差异</p>
<div>
<div class="compare-container">
<div>
<div class="input-group">
<label for="text1">文本1</label>
<textarea
id="text1"
v-model="text1"
@input="compareTexts"
placeholder="在此输入第一个文本..."
rows="10"
></textarea>
</div>
</div>
<div>
<div class="input-group">
<label for="text2">文本2</label>
<textarea
id="text2"
v-model="text2"
@input="compareTexts"
placeholder="在此输入第二个文本..."
rows="10"
></textarea>
</div>
</div>
</div>
<div class="stats-container">
<div class="stat-item">
<div class="stat-value">{{ text1.length }}</div>
<div class="stat-label">文本1字符数</div>
</div>
<div class="stat-item">
<div class="stat-value">{{ text2.length }}</div>
<div class="stat-label">文本2字符数</div>
</div>
<div class="stat-item">
<div class="stat-value">{{ diffChars }}</div>
<div class="stat-label">字符差异</div>
</div>
<div class="stat-item">
<div class="stat-value">{{ similarity.toFixed(2) }}%</div>
<div class="stat-label">相似度</div>
</div>
</div>
<div class="controls">
<button @click="compareTexts">对比文本</button>
<button @click="swapTexts">交换文本</button>
<button @click="clearTexts">清空文本</button>
</div>
<div class="diff-result" v-if="diffResult" v-html="diffResult"></div>
</div>
</div>
</div>
</div>
<script>
const { createApp, ref, computed } = Vue;
createApp({
setup() {
const text1 = ref('');
const text2 = ref('');
const diffResult = ref('');
const diffChars = ref(0);
const similarity = ref(0);
// 简单的文本对比算法
const compareTexts = () => {
const t1 = text1.value;
const t2 = text2.value;
if (!t1 && !t2) {
diffResult.value = '';
diffChars.value = 0;
similarity.value = 100;
return;
}
if (!t1) {
diffResult.value = `<span class="diff-added">${escapeHtml(t2)}</span>`;
diffChars.value = t2.length;
similarity.value = 0;
return;
}
if (!t2) {
diffResult.value = `<span class="diff-removed">${escapeHtml(t1)}</span>`;
diffChars.value = t1.length;
similarity.value = 0;
return;
}
// 简单的差异计算
let result = '';
let removedCount = 0;
let addedCount = 0;
let maxLen = Math.max(t1.length, t2.length);
for (let i = 0; i < maxLen; i++) {
if (i >= t1.length) {
// 在t2中新增的字符
result += `<span class="diff-added">${escapeHtml(t2[i])}</span>`;
addedCount++;
} else if (i >= t2.length) {
// 在t1中删除的字符
result += `<span class="diff-removed">${escapeHtml(t1[i])}</span>`;
removedCount++;
} else if (t1[i] !== t2[i]) {
// 不同的字符
result += `<span class="diff-removed">${escapeHtml(t1[i])}</span><span class="diff-added">${escapeHtml(t2[i])}</span>`;
removedCount++;
addedCount++;
} else {
// 相同的字符
result += escapeHtml(t2[i]);
}
}
diffResult.value = result;
diffChars.value = removedCount + addedCount;
// 计算相似度
const maxLength = Math.max(t1.length, t2.length);
similarity.value = maxLength === 0 ? 100 : (1 - diffChars.value / maxLength) * 100;
};
const swapTexts = () => {
const temp = text1.value;
text1.value = text2.value;
text2.value = temp;
compareTexts();
};
const clearTexts = () => {
text1.value = '';
text2.value = '';
diffResult.value = '';
diffChars.value = 0;
similarity.value = 0;
};
// 转义HTML字符以防止XSS
const escapeHtml = (text) => {
return text
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/\n/g, "<br>");
};
return {
text1,
text2,
diffResult,
diffChars,
similarity,
compareTexts,
swapTexts,
clearTexts
};
}
}).mount('#app');
</script>
</body>
</html>