-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_js_converters.py
More file actions
320 lines (291 loc) · 10.7 KB
/
generate_js_converters.py
File metadata and controls
320 lines (291 loc) · 10.7 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
308
309
310
311
312
313
314
315
316
317
318
319
320
#!/usr/bin/env python3
# 通用转换器模板
def create_converter(input_label, output_label, convert_fn, button_text="Convert"):
return f'''const toolSection = document.getElementById('toolSection');
toolSection.innerHTML = `
<div class="input-area">
<label for="inputText">{input_label}:</label>
<textarea id="inputText" rows="8" placeholder="Enter text here..."></textarea>
</div>
<div class="buttons-grid">
<button onclick="convert()" class="btn btn-primary">{button_text}</button>
<button onclick="clearAll()" class="btn btn-clear">Clear</button>
</div>
<div class="output-area">
<label for="outputText">{output_label}:</label>
<textarea id="outputText" rows="8" readonly placeholder="Result will appear here..."></textarea>
<button onclick="copyOutput()" class="btn btn-copy" id="copyBtn">📋 Copy</button>
</div>
`;
{convert_fn}
function copyOutput() {{
const output = document.getElementById('outputText');
if (!output.value) return;
navigator.clipboard.writeText(output.value).then(() => {{
document.getElementById('copyBtn').textContent = '✅ Copied!';
setTimeout(() => {{ document.getElementById('copyBtn').textContent = '📋 Copy'; }}, 2000);
}});
}}
function clearAll() {{
document.getElementById('inputText').value = '';
document.getElementById('outputText').value = '';
}}
'''
# 转换类工具
converters = {
"text-repeater": '''const toolSection = document.getElementById('toolSection');
toolSection.innerHTML = `
<div class="input-area">
<label for="inputText">Text to Repeat:</label>
<textarea id="inputText" rows="4" placeholder="Enter text here..."></textarea>
<label style="margin-top:12px;">Repeat Count:</label>
<input type="number" id="repeatCount" value="3" min="1" max="1000" style="width:100px;padding:8px;">
</div>
<div class="buttons-grid">
<button onclick="convert()" class="btn btn-primary">Repeat Text</button>
</div>
<div class="output-area">
<label for="outputText">Result:</label>
<textarea id="outputText" rows="8" readonly></textarea>
<button onclick="copyOutput()" class="btn btn-copy" id="copyBtn">📋 Copy</button>
</div>
`;
function convert() {
const text = document.getElementById('inputText').value;
const count = parseInt(document.getElementById('repeatCount').value) || 1;
const result = Array(count).fill(text).join('\\n');
document.getElementById('outputText').value = result;
}
function copyOutput() {
const output = document.getElementById('outputText');
if (!output.value) return;
navigator.clipboard.writeText(output.value).then(() => {
document.getElementById('copyBtn').textContent = '✅ Copied!';
setTimeout(() => { document.getElementById('copyBtn').textContent = '📋 Copy'; }, 2000);
});
}
''',
"text-reverser": create_converter(
"Text to Reverse",
"Reversed Text",
'''function convert() {
const input = document.getElementById('inputText').value;
const reversed = input.split('').reverse().join('');
document.getElementById('outputText').value = reversed;
}''',
"Reverse Text"
),
"remove-duplicate-lines": create_converter(
"Text with Duplicate Lines",
"Unique Lines",
'''function convert() {
const input = document.getElementById('inputText').value;
const lines = input.split('\\n');
const unique = [...new Set(lines)];
document.getElementById('outputText').value = unique.join('\\n');
}''',
"Remove Duplicates"
),
"remove-empty-lines": create_converter(
"Text with Empty Lines",
"Text without Empty Lines",
'''function convert() {
const input = document.getElementById('inputText').value;
const lines = input.split('\\n').filter(line => line.trim());
document.getElementById('outputText').value = lines.join('\\n');
}''',
"Remove Empty Lines"
),
"remove-line-breaks": create_converter(
"Text with Line Breaks",
"Single Line Text",
'''function convert() {
const input = document.getElementById('inputText').value;
const result = input.replace(/\\n/g, ' ').replace(/\\s+/g, ' ').trim();
document.getElementById('outputText').value = result;
}''',
"Remove Line Breaks"
),
"add-line-numbers": create_converter(
"Text",
"Text with Line Numbers",
'''function convert() {
const input = document.getElementById('inputText').value;
const lines = input.split('\\n');
const numbered = lines.map((line, i) => `${i + 1}. ${line}`);
document.getElementById('outputText').value = numbered.join('\\n');
}''',
"Add Line Numbers"
),
"sort-lines": '''const toolSection = document.getElementById('toolSection');
toolSection.innerHTML = `
<div class="input-area">
<label for="inputText">Text to Sort:</label>
<textarea id="inputText" rows="8" placeholder="Enter text here..."></textarea>
</div>
<div class="buttons-grid">
<button onclick="sortAsc()" class="btn btn-primary">Sort A-Z</button>
<button onclick="sortDesc()" class="btn btn-secondary">Sort Z-A</button>
<button onclick="clearAll()" class="btn btn-clear">Clear</button>
</div>
<div class="output-area">
<label for="outputText">Sorted Text:</label>
<textarea id="outputText" rows="8" readonly></textarea>
<button onclick="copyOutput()" class="btn btn-copy" id="copyBtn">📋 Copy</button>
</div>
`;
function sortAsc() {
const input = document.getElementById('inputText').value;
const lines = input.split('\\n').sort();
document.getElementById('outputText').value = lines.join('\\n');
}
function sortDesc() {
const input = document.getElementById('inputText').value;
const lines = input.split('\\n').sort().reverse();
document.getElementById('outputText').value = lines.join('\\n');
}
function copyOutput() {
const output = document.getElementById('outputText');
if (!output.value) return;
navigator.clipboard.writeText(output.value).then(() => {
document.getElementById('copyBtn').textContent = '✅ Copied!';
setTimeout(() => { document.getElementById('copyBtn').textContent = '📋 Copy'; }, 2000);
});
}
function clearAll() {
document.getElementById('inputText').value = '';
document.getElementById('outputText').value = '';
}
''',
"text-to-binary": create_converter(
"Text",
"Binary",
'''function convert() {
const input = document.getElementById('inputText').value;
const binary = input.split('').map(char =>
char.charCodeAt(0).toString(2).padStart(8, '0')
).join(' ');
document.getElementById('outputText').value = binary;
}''',
"Convert to Binary"
),
"binary-to-text": create_converter(
"Binary",
"Text",
'''function convert() {
try {
const input = document.getElementById('inputText').value;
const binary = input.replace(/\\s/g, '').match(/.{1,8}/g) || [];
const text = binary.map(bin => String.fromCharCode(parseInt(bin, 2))).join('');
document.getElementById('outputText').value = text;
} catch(e) {
document.getElementById('outputText').value = 'Error: Invalid binary input';
}
}''',
"Convert to Text"
),
"text-to-hex": create_converter(
"Text",
"Hexadecimal",
'''function convert() {
const input = document.getElementById('inputText').value;
const hex = input.split('').map(char =>
char.charCodeAt(0).toString(16).padStart(2, '0')
).join(' ');
document.getElementById('outputText').value = hex;
}''',
"Convert to Hex"
),
"hex-to-text": create_converter(
"Hexadecimal",
"Text",
'''function convert() {
try {
const input = document.getElementById('inputText').value;
const hex = input.replace(/\\s/g, '').match(/.{1,2}/g) || [];
const text = hex.map(h => String.fromCharCode(parseInt(h, 16))).join('');
document.getElementById('outputText').value = text;
} catch(e) {
document.getElementById('outputText').value = 'Error: Invalid hex input';
}
}''',
"Convert to Text"
),
"base64-encode": create_converter(
"Text",
"Base64",
'''function convert() {
const input = document.getElementById('inputText').value;
const encoded = btoa(unescape(encodeURIComponent(input)));
document.getElementById('outputText').value = encoded;
}''',
"Encode to Base64"
),
"base64-decode": create_converter(
"Base64",
"Decoded Text",
'''function convert() {
try {
const input = document.getElementById('inputText').value;
const decoded = decodeURIComponent(escape(atob(input)));
document.getElementById('outputText').value = decoded;
} catch(e) {
document.getElementById('outputText').value = 'Error: Invalid Base64 input';
}
}''',
"Decode from Base64"
),
"url-encode": create_converter(
"Text",
"URL Encoded",
'''function convert() {
const input = document.getElementById('inputText').value;
const encoded = encodeURIComponent(input);
document.getElementById('outputText').value = encoded;
}''',
"URL Encode"
),
"url-decode": create_converter(
"URL Encoded Text",
"Decoded Text",
'''function convert() {
try {
const input = document.getElementById('inputText').value;
const decoded = decodeURIComponent(input);
document.getElementById('outputText').value = decoded;
} catch(e) {
document.getElementById('outputText').value = 'Error: Invalid URL encoded input';
}
}''',
"URL Decode"
),
"html-encode": create_converter(
"Text",
"HTML Encoded",
'''function convert() {
const input = document.getElementById('inputText').value;
const div = document.createElement('div');
div.textContent = input;
document.getElementById('outputText').value = div.innerHTML;
}''',
"HTML Encode"
),
"html-decode": create_converter(
"HTML Encoded Text",
"Decoded Text",
'''function convert() {
const input = document.getElementById('inputText').value;
const div = document.createElement('div');
div.innerHTML = input;
document.getElementById('outputText').value = div.textContent;
}''',
"HTML Decode"
),
}
# 生成所有转换类工具
for tool_id, js_code in converters.items():
filename = f"{tool_id}.js"
with open(filename, 'w') as f:
f.write(js_code)
print(f"✅ Generated {filename}")
print(f"\\n🎯 Generated {len(converters)} converter tools!")