-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
111 lines (95 loc) · 3.65 KB
/
script.js
File metadata and controls
111 lines (95 loc) · 3.65 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
const logoImg = document.querySelector('.logo');
const logoCount = 8;
logoImg.src = `cat${Math.floor(Math.random()*logoCount)+1}.gif`;
const fileInput = document.getElementById('fileInput');
const previewCard = document.querySelector('.preview-card');
const previewImage = document.getElementById('previewImage');
const extractBtn = document.getElementById('extractBtn');
const resultText = document.getElementById('resultText');
const resultContainer = document.querySelector('.result');
const copyBtn = document.getElementById('copyBtn');
const cameraBtn = document.getElementById('cameraBtn');
const cameraModal = document.getElementById('cameraModal');
const cameraVideo = document.getElementById('cameraVideo');
const captureBtn = document.getElementById('captureBtn');
const closeCamera = document.getElementById('closeCamera');
const toastContainer = document.getElementById('toastContainer');
const fileBtn = document.querySelector('.file-btn');
let imageSrc = '';
let stream;
function showToast(msg,type='error'){
const toast=document.createElement('div');
toast.className='toast';
if(type==='success') toast.classList.add('success');
toast.textContent=msg;
toastContainer.appendChild(toast);
setTimeout(()=>toast.remove(),3500);
}
fileBtn.addEventListener('click',()=>fileInput.click());
function handleImage(file){
if(!file) return;
if(!file.type.startsWith('image/')) return showToast('Invalid file type.');
if(imageSrc.startsWith('blob:')) URL.revokeObjectURL(imageSrc);
imageSrc = URL.createObjectURL(file);
previewImage.src = imageSrc;
previewCard.setAttribute('aria-hidden','false');
resultContainer.setAttribute('aria-hidden','true');
resultText.textContent = '';
}
fileInput.addEventListener('change',e=>handleImage(e.target.files[0]));
document.addEventListener('paste', e=>{
const items=(e.clipboardData||e.originalEvent.clipboardData).items;
if(!items) return;
for(const item of items){
if(item.type.indexOf('image')!==-1){
const blob=item.getAsFile();
handleImage(blob);
e.preventDefault();
return;
}
}
});
cameraBtn.addEventListener('click', async ()=>{
try{
stream = await navigator.mediaDevices.getUserMedia({video:true});
cameraVideo.srcObject = stream;
cameraModal.classList.add('show');
}catch{
showToast('Camera access denied.');
}
});
captureBtn.addEventListener('click',()=>{
if(!stream) return showToast('No camera stream.');
const canvas = document.createElement('canvas');
canvas.width = cameraVideo.videoWidth;
canvas.height = cameraVideo.videoHeight;
canvas.getContext('2d').drawImage(cameraVideo,0,0);
canvas.toBlob(blob => handleImage(blob), 'image/png');
stopCamera();
});
closeCamera.addEventListener('click', stopCamera);
function stopCamera(){
cameraModal.classList.remove('show');
if(stream) stream.getTracks().forEach(t=>t.stop());
}
extractBtn.addEventListener('click', async ()=>{
if(!imageSrc) return showToast('Please select or capture an image.');
extractBtn.disabled = true;
resultContainer.setAttribute('aria-hidden','true');
resultText.textContent='Processing...';
try{
const { data: { text } } = await Tesseract.recognize(imageSrc,'eng',{logger:m=>console.log(m)});
resultText.textContent = text.trim()||'No text detected.';
resultContainer.setAttribute('aria-hidden','false');
}catch{
showToast('Failed to extract text.');
}
extractBtn.disabled = false;
});
copyBtn.addEventListener('click',()=>{
const text = resultText.textContent.trim();
if(!text) return showToast('Nothing to copy.');
navigator.clipboard.writeText(text)
.then(()=>showToast('Text copied!','success'))
.catch(()=>showToast('Failed to copy.'));
});