-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.js
More file actions
69 lines (63 loc) · 2.23 KB
/
script.js
File metadata and controls
69 lines (63 loc) · 2.23 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
const imageInput = document.getElementById('imageInput');
const convertBtn = document.getElementById('convertBtn');
const clearBtn = document.getElementById('clearBtn');
const preview = document.getElementById('preview');
const result = document.getElementById('result');
const loading = document.getElementById('loading');
const progressContainer = document.querySelector('.progress-container');
const progressBar = document.getElementById('progress-bar');
imageInput.addEventListener('change', function(e) {
const file = e.target.files[0];
if (file) {
const reader = new FileReader();
reader.onload = function(e) {
preview.innerHTML = `<img src="${e.target.result}" alt="Preview" class="max-h-[400px] rounded-lg shadow-md">`;
}
reader.readAsDataURL(file);
}
});
clearBtn.addEventListener('click', function() {
imageInput.value = '';
preview.innerHTML = '';
result.textContent = '';
progressBar.style.width = '0%';
progressContainer.style.display = 'none';
loading.style.display = 'none';
progressContainer.classList.add('hidden');
loading.classList.add('hidden');
});
convertBtn.addEventListener('click', async function() {
const file = imageInput.files[0];
if (!file) {
alert('Please select an image first.');
return;
}
try {
loading.classList.remove('hidden');
progressContainer.classList.remove('hidden');
result.textContent = '';
progressBar.style.width = '0%';
const { data: { text } } = await Tesseract.recognize(
file,
'eng',
{
logger: m => {
if (m.status === 'recognizing text') {
progressBar.style.width = `${Math.round(m.progress * 100)}%`;
}
console.log(m);
}
}
);
result.textContent = text;
} catch (error) {
console.error(error);
result.textContent = 'Error occurred during conversion.';
} finally {
loading.classList.add('hidden');
progressBar.style.width = '100%';
setTimeout(() => {
progressContainer.classList.add('hidden');
}, 1000);
}
});