forked from simonw/tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage-alt-text-extractor.html
More file actions
366 lines (314 loc) · 12.4 KB
/
image-alt-text-extractor.html
File metadata and controls
366 lines (314 loc) · 12.4 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Image Alt Text Extractor</title>
<link rel="stylesheet" href="styles.css">
<style>
body {
max-width: 960px;
margin: 0 auto;
padding: 24px 20px 48px;
}
.page-header {
display: flex;
flex-direction: column;
gap: 0.5rem;
}
.site-link {
font-weight: 600;
color: var(--foreground-subtle);
text-decoration: none;
}
.site-link:hover,
.site-link:focus-visible {
color: var(--foreground);
}
main {
display: grid;
gap: 1.25rem;
}
.panel {
padding: clamp(1.25rem, 3vw, 2rem);
}
.input-grid {
display: grid;
gap: 1rem;
}
.drop-zone {
border: 2px dashed var(--color-border, #d0d7de);
border-radius: 12px;
padding: 28px 18px;
text-align: center;
cursor: pointer;
transition: border-color 0.2s ease, background-color 0.2s ease;
}
.drop-zone.dragover {
border-color: var(--color-accent, #0070f3);
background-color: rgba(0, 112, 243, 0.08);
}
.drop-zone:focus-visible {
outline: 2px solid var(--color-accent, #0070f3);
outline-offset: 2px;
}
.drop-zone input[type="file"] {
display: none;
}
.status-text {
color: var(--color-muted, #57606a);
min-height: 1.25rem;
}
.status-text.error {
color: #b42318;
}
.preview-wrapper {
display: grid;
gap: 0.75rem;
}
.preview-wrapper img {
max-width: 100%;
border-radius: 10px;
background: #fff;
box-shadow: 0 2px 10px rgba(15, 23, 42, 0.08);
}
.alt-text-area {
width: 100%;
min-height: 120px;
resize: vertical;
}
.badge {
display: inline-flex;
align-items: center;
gap: 0.35rem;
padding: 0.25rem 0.6rem;
border-radius: 999px;
background: var(--background-elevated, #f6f8fa);
color: var(--foreground-muted, #57606a);
font-size: 0.95rem;
width: fit-content;
}
.info-row {
display: flex;
flex-wrap: wrap;
gap: 0.75rem;
align-items: center;
}
.tool-actions {
display: flex;
flex-wrap: wrap;
gap: 0.75rem;
}
@media (max-width: 720px) {
body {
padding: 20px 16px 40px;
}
}
</style>
</head>
<body>
<header class="page-header">
<a class="site-link" href="https://tools.mathspp.com/" aria-label="Back to tools.mathspp.com">← tools.mathspp.com</a>
<h1>Image Alt Text Extractor</h1>
<p class="lead">Paste or drop an image and we will surface any ALT text that came with it. If none is present, we will tell you.</p>
</header>
<main>
<section class="surface panel">
<div class="input-grid">
<label class="drop-zone" id="drop-zone" tabindex="0" aria-label="Paste or drop an image here">
<strong>Paste, drop, or select an image</strong>
<p class="status-text" id="drop-hint">Drop a file, press Ctrl/Cmd+V, or use the buttons below.</p>
<input type="file" id="file-input" accept="image/*">
</label>
<div class="tool-actions">
<button type="button" id="clipboard-button">Paste image from clipboard</button>
<button type="button" id="file-button">Choose an image</button>
</div>
<p class="status-text" id="status" aria-live="polite">No image loaded yet.</p>
</div>
</section>
<section class="surface panel" aria-live="polite">
<div class="info-row" aria-hidden="true" id="info-row" hidden>
<span class="badge" id="image-info"></span>
</div>
<div class="preview-wrapper">
<div class="form-group">
<label for="alt-text">ALT text found</label>
<textarea id="alt-text" class="alt-text-area" placeholder="ALT text will appear here" spellcheck="true"></textarea>
</div>
<div class="tool-actions">
<button type="button" id="copy-button">Copy ALT text</button>
<button type="button" id="clear-button">Clear</button>
</div>
<img id="preview" alt="Preview of the pasted image" hidden>
</div>
</section>
</main>
<script>
const dropZone = document.getElementById('drop-zone');
const fileInput = document.getElementById('file-input');
const clipboardButton = document.getElementById('clipboard-button');
const fileButton = document.getElementById('file-button');
const statusEl = document.getElementById('status');
const previewEl = document.getElementById('preview');
const altTextEl = document.getElementById('alt-text');
const copyButton = document.getElementById('copy-button');
const clearButton = document.getElementById('clear-button');
const dropHint = document.getElementById('drop-hint');
const infoRow = document.getElementById('info-row');
const imageInfo = document.getElementById('image-info');
let lastObjectUrl = null;
function setStatus(message, isError = false) {
statusEl.textContent = message;
statusEl.classList.toggle('error', isError);
}
function resetPreview() {
previewEl.src = '';
previewEl.hidden = true;
altTextEl.value = '';
infoRow.hidden = true;
if (lastObjectUrl) {
URL.revokeObjectURL(lastObjectUrl);
lastObjectUrl = null;
}
setStatus('No image loaded yet.');
}
function describeImage(file) {
const sizeKb = file.size / 1024;
const kbDisplay = sizeKb >= 1024 ? `${(sizeKb / 1024).toFixed(1)} MB` : `${sizeKb.toFixed(0)} kB`;
imageInfo.textContent = `${file.name || 'clipboard image'} • ${kbDisplay}`;
infoRow.hidden = false;
}
function extractAltFromHtml(html) {
if (!html) return '';
try {
const parser = new DOMParser();
const doc = parser.parseFromString(html, 'text/html');
const img = doc.querySelector('img[alt]');
return img?.getAttribute('alt')?.trim() || '';
} catch (error) {
console.error('Failed to parse HTML clipboard content', error);
return '';
}
}
function handleImage(file, altText, sourceDescription) {
describeImage(file);
const objectUrl = URL.createObjectURL(file);
if (lastObjectUrl) {
URL.revokeObjectURL(lastObjectUrl);
}
lastObjectUrl = objectUrl;
previewEl.src = objectUrl;
previewEl.hidden = false;
altTextEl.value = altText || '';
if (altText) {
setStatus(`ALT text found from ${sourceDescription}.`);
} else {
setStatus('No ALT text found for this image.', true);
}
}
async function handleClipboardRead() {
if (!navigator.clipboard?.read) {
setStatus('Clipboard image reading is not supported in this browser.', true);
return;
}
try {
setStatus('Reading clipboard…');
const items = await navigator.clipboard.read();
let html = '';
let imageBlob = null;
for (const item of items) {
if (!html && item.types.includes('text/html')) {
const blob = await item.getType('text/html');
html = await blob.text();
}
const imageType = item.types.find(type => type.startsWith('image/'));
if (!imageBlob && imageType) {
imageBlob = await item.getType(imageType);
imageBlob.name = 'clipboard-image';
}
}
if (!imageBlob) {
setStatus('No image found in clipboard.', true);
return;
}
const altText = extractAltFromHtml(html);
handleImage(imageBlob, altText, 'clipboard metadata');
} catch (error) {
console.error(error);
setStatus('Failed to read clipboard image or metadata. The browser may have blocked access.', true);
}
}
function handlePasteEvent(event) {
const file = [...event.clipboardData.files].find(item => item.type.startsWith('image/'));
const html = event.clipboardData.getData('text/html');
const altText = extractAltFromHtml(html);
if (file) {
event.preventDefault();
handleImage(file, altText, 'clipboard metadata');
} else if (altText) {
altTextEl.value = altText;
setStatus('ALT text found in clipboard HTML, but no image was included.');
} else {
setStatus('Clipboard does not contain an image or ALT metadata.', true);
}
}
function handleFileInput(file, sourceDescription) {
if (!file || !file.type.startsWith('image/')) {
setStatus('Please provide an image file.', true);
return;
}
handleImage(file, '', sourceDescription);
}
dropZone.addEventListener('dragover', (event) => {
event.preventDefault();
dropZone.classList.add('dragover');
});
dropZone.addEventListener('dragleave', () => {
dropZone.classList.remove('dragover');
});
dropZone.addEventListener('drop', (event) => {
event.preventDefault();
dropZone.classList.remove('dragover');
const [file] = event.dataTransfer.files;
handleFileInput(file, 'drag and drop');
});
dropZone.addEventListener('click', () => fileInput.click());
dropZone.addEventListener('keydown', (event) => {
if (event.key === 'Enter' || event.key === ' ') {
event.preventDefault();
fileInput.click();
}
});
dropZone.addEventListener('paste', handlePasteEvent);
fileInput.addEventListener('change', (event) => {
const [file] = event.target.files;
handleFileInput(file, 'file picker');
fileInput.value = '';
});
fileButton.addEventListener('click', () => fileInput.click());
clipboardButton.addEventListener('click', handleClipboardRead);
copyButton.addEventListener('click', async () => {
if (!altTextEl.value.trim()) {
setStatus('Nothing to copy yet.', true);
return;
}
try {
await navigator.clipboard.writeText(altTextEl.value.trim());
setStatus('ALT text copied to clipboard.');
} catch (error) {
console.error(error);
setStatus('Unable to copy ALT text.', true);
}
});
clearButton.addEventListener('click', () => {
resetPreview();
});
dropHint.textContent = navigator.clipboard?.read ? 'Drop a file, press Ctrl/Cmd+V, or use the buttons below.' : 'Drop a file or use the choose image button.';
setStatus('No image loaded yet.');
</script>
<footer class="page-footer">
<p>Built with ❤️, 🤖, and 🐍, by <a href="https://mathspp.com/">Rodrigo Girão Serrão</a></p>
</footer>
</body>
</html>