-
-
Notifications
You must be signed in to change notification settings - Fork 153
Expand file tree
/
Copy pathjson-string-extractor.html
More file actions
419 lines (353 loc) · 9.7 KB
/
json-string-extractor.html
File metadata and controls
419 lines (353 loc) · 9.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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>JSON String Extractor</title>
<style>
* {
box-sizing: border-box;
}
body {
font-family: Helvetica, Arial, sans-serif;
margin: 0;
padding: 20px;
background: #f5f5f5;
}
.container {
max-width: 1200px;
margin: 0 auto;
background: white;
padding: 30px;
border-radius: 8px;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}
h1 {
margin: 0 0 20px 0;
font-size: 24px;
font-weight: normal;
}
.input-section {
margin-bottom: 30px;
}
#jsonInput {
width: 100%;
min-height: 150px;
padding: 12px;
border: 2px solid #ddd;
border-radius: 4px;
font-family: 'Courier New', monospace;
font-size: 16px;
resize: vertical;
}
#jsonInput:focus {
outline: none;
border-color: #4a90e2;
}
.error {
color: #e74c3c;
padding: 12px;
background: #fef5f5;
border-radius: 4px;
margin-top: 10px;
display: none;
}
.results {
margin-top: 30px;
}
.string-item {
margin-bottom: 20px;
padding: 15px;
background: #fafafa;
border-radius: 4px;
border: 1px solid #e0e0e0;
}
.string-path {
font-size: 12px;
color: #666;
margin-bottom: 8px;
font-family: 'Courier New', monospace;
}
.string-content {
position: relative;
}
.string-textarea {
width: 100%;
padding: 10px;
border: 1px solid #ddd;
border-radius: 4px;
font-family: 'Courier New', monospace;
font-size: 16px;
resize: none;
background: white;
overflow-y: hidden;
}
.copy-button {
margin-top: 8px;
padding: 8px 16px;
background: #4a90e2;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
font-size: 14px;
font-family: Helvetica, Arial, sans-serif;
}
.copy-button:hover {
background: #357abd;
}
.copy-button:active {
background: #2868a8;
}
.copy-button.copied {
background: #27ae60;
}
.load-example-button {
padding: 6px 12px;
background: #666;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
font-size: 13px;
font-family: Helvetica, Arial, sans-serif;
}
.load-example-button:hover {
background: #555;
}
.load-example-button:active {
background: #444;
}
.load-example-button:disabled {
background: #ccc;
cursor: not-allowed;
}
.count {
font-size: 14px;
color: #666;
margin-bottom: 15px;
}
</style>
</head>
<body>
<div class="container">
<h1>JSON string extractor</h1>
<p style="margin: 0 0 20px 0; font-size: 14px; color: #666; line-height: 1.2;">Paste JSON below to extract all strings that are longer than 20 characters or contain newlines.</p>
<div class="input-section">
<div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 8px;">
<label for="jsonInput" style="font-size: 14px; color: #666;">Paste JSON here:</label>
<div style="display: flex; gap: 8px;">
<button id="loadGist" class="load-example-button">Load Gist</button>
<a href="#gist=1d1013ba059af76461153722005a039d" class="load-example-button" style="text-decoration: none; display: inline-block; line-height: 1.5;">Load example JSON</a>
</div>
</div>
<textarea id="jsonInput" placeholder="Paste your JSON here..."></textarea>
<div id="error" class="error"></div>
</div>
<div id="results" class="results"></div>
</div>
<script type="module">
const jsonInput = document.getElementById('jsonInput');
const errorDiv = document.getElementById('error');
const resultsDiv = document.getElementById('results');
const loadGistButton = document.getElementById('loadGist');
function extractGistId(input) {
// Try to extract Gist ID from URL or use input directly
const urlMatch = input.match(/gist\.github\.com\/[^\/]+\/([a-fA-F0-9]+)/i);
if (urlMatch) {
return urlMatch[1];
}
// Check if it's just a Gist ID (alphanumeric string)
if (/^[a-fA-F0-9]+$/i.test(input.trim())) {
return input.trim();
}
return null;
}
async function loadGistJSON(gistId) {
const response = await fetch(`https://api.github.com/gists/${gistId}`);
if (!response.ok) {
throw new Error(`Failed to fetch Gist: ${response.status} ${response.statusText}`);
}
const gistData = await response.json();
// Find the first file that contains valid JSON
for (const filename in gistData.files) {
const file = gistData.files[filename];
try {
// Try to parse the content as JSON
JSON.parse(file.content);
return file.content;
} catch (e) {
// Not valid JSON, continue to next file
continue;
}
}
throw new Error('No valid JSON file found in the Gist');
}
async function loadGist() {
const input = prompt('Enter a Gist URL or Gist ID:');
if (!input) {
return; // User cancelled
}
const gistId = extractGistId(input);
if (!gistId) {
showError('Invalid Gist URL or ID. Please provide a valid GitHub Gist URL or ID.');
return;
}
loadGistButton.disabled = true;
loadGistButton.textContent = 'Loading...';
try {
const jsonContent = await loadGistJSON(gistId);
jsonInput.value = jsonContent;
processJSON();
// Update the URL hash
window.location.hash = `gist=${gistId}`;
loadGistButton.textContent = 'Load Gist';
} catch (e) {
showError(`Failed to load Gist: ${e.message}`);
loadGistButton.textContent = 'Load Gist';
} finally {
loadGistButton.disabled = false;
}
}
async function loadGistFromHash() {
const hash = window.location.hash;
const gistMatch = hash.match(/gist=([a-fA-F0-9]+)/i);
if (gistMatch) {
const gistId = gistMatch[1];
try {
const jsonContent = await loadGistJSON(gistId);
jsonInput.value = jsonContent;
processJSON();
} catch (e) {
showError(`Failed to load Gist from URL: ${e.message}`);
}
}
}
loadGistButton.addEventListener('click', loadGist);
function showError(message) {
errorDiv.textContent = message;
errorDiv.style.display = 'block';
}
function hideError() {
errorDiv.style.display = 'none';
}
function shouldDisplay(str) {
return str.includes('\n') || str.length > 20;
}
function extractStrings(obj, path = '') {
const results = [];
if (typeof obj === 'string') {
if (shouldDisplay(obj)) {
results.push({ path, value: obj });
}
} else if (Array.isArray(obj)) {
obj.forEach((item, index) => {
const newPath = path ? `${path}[${index}]` : `[${index}]`;
results.push(...extractStrings(item, newPath));
});
} else if (obj !== null && typeof obj === 'object') {
Object.keys(obj).forEach(key => {
const newPath = path ? `${path}.${key}` : key;
results.push(...extractStrings(obj[key], newPath));
});
}
return results;
}
function resizeTextarea(textarea) {
// Reset height to auto to get the correct scrollHeight
textarea.style.height = 'auto';
// Set height to scrollHeight to fit content
textarea.style.height = textarea.scrollHeight + 'px';
}
function resizeAllTextareas() {
document.querySelectorAll('.string-textarea').forEach(textarea => {
resizeTextarea(textarea);
});
}
function copyToClipboard(text, button) {
navigator.clipboard.writeText(text).then(() => {
const originalText = button.textContent;
button.textContent = 'Copied!';
button.classList.add('copied');
setTimeout(() => {
button.textContent = originalText;
button.classList.remove('copied');
}, 2000);
}).catch(err => {
console.error('Failed to copy:', err);
alert('Failed to copy to clipboard');
});
}
function renderResults(strings) {
if (strings.length === 0) {
resultsDiv.innerHTML = '<p class="count">No strings found that are longer than 20 characters or contain newlines.</p>';
return;
}
let html = `<p class="count">Found ${strings.length} string${strings.length === 1 ? '' : 's'}:</p>`;
strings.forEach((item, index) => {
const escapedValue = item.value
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"');
html += `
<div class="string-item">
<div class="string-path">${item.path || '(root)'}</div>
<div class="string-content">
<textarea class="string-textarea" data-index="${index}">${escapedValue}</textarea>
<button class="copy-button" data-index="${index}">Copy to clipboard</button>
</div>
</div>
`;
});
resultsDiv.innerHTML = html;
// Add event listeners to copy buttons
document.querySelectorAll('.copy-button').forEach(button => {
button.addEventListener('click', () => {
const index = button.getAttribute('data-index');
const textarea = document.querySelector(`.string-textarea[data-index="${index}"]`);
copyToClipboard(textarea.value, button);
});
});
// Resize all textareas after rendering
requestAnimationFrame(() => {
resizeAllTextareas();
});
}
function processJSON() {
const input = jsonInput.value.trim();
if (!input) {
resultsDiv.innerHTML = '';
hideError();
return;
}
try {
const parsed = JSON.parse(input);
hideError();
const strings = extractStrings(parsed);
renderResults(strings);
} catch (e) {
showError(`Invalid JSON: ${e.message}`);
resultsDiv.innerHTML = '';
}
}
jsonInput.addEventListener('input', processJSON);
// Resize textareas when window is resized
let resizeTimeout;
window.addEventListener('resize', () => {
clearTimeout(resizeTimeout);
resizeTimeout = setTimeout(() => {
resizeAllTextareas();
}, 100);
});
// Load Gist from hash if present, otherwise process any existing content
loadGistFromHash().then(() => {
processJSON();
});
// Listen for hash changes to load gists dynamically
window.addEventListener('hashchange', () => {
loadGistFromHash();
});
</script>
</body>
</html>