-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
131 lines (111 loc) · 4.05 KB
/
script.js
File metadata and controls
131 lines (111 loc) · 4.05 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
// DOM Elements
const dropZone = document.getElementById('dropZone');
const fileInput = document.getElementById('fileInput');
const resultSection = document.getElementById('resultSection');
const userData = document.getElementById('userData');
const copyButton = document.getElementById('copyButton');
const downloadButton = document.getElementById('downloadButton');
const processingIndicator = document.getElementById('processingIndicator');
// Update copyright year
document.getElementById('currentYear').textContent = new Date().getFullYear();
// Handle clipboard paste events
document.addEventListener('paste', (e) => {
const items = e.clipboardData.items;
for (let i = 0; i < items.length; i++) {
if (items[i].type.indexOf('image') !== -1) {
const blob = items[i].getAsFile();
processImage(blob);
break;
}
}
});
// Drag and drop event handlers
dropZone.addEventListener('dragover', (e) => {
e.preventDefault();
dropZone.style.backgroundColor = 'rgba(113, 137, 255, 0.2)';
});
dropZone.addEventListener('dragleave', () => {
dropZone.style.backgroundColor = 'rgba(113, 137, 255, 0.1)';
});
dropZone.addEventListener('drop', (e) => {
e.preventDefault();
dropZone.style.backgroundColor = 'rgba(113, 137, 255, 0.1)';
const file = e.dataTransfer.files[0];
if (file && file.type.startsWith('image/')) {
processImage(file);
}
});
// File input handler
fileInput.addEventListener('change', (e) => {
const file = e.target.files[0];
if (file) {
processImage(file);
}
});
// Process image with Tesseract OCR
async function processImage(file) {
try {
// Show result section and processing indicator
resultSection.style.display = 'block';
processingIndicator.style.display = 'flex';
userData.value = '';
// Create scheduler
const scheduler = Tesseract.createScheduler();
// Create workers
const worker1 = await Tesseract.createWorker('eng');
const worker2 = await Tesseract.createWorker('eng');
// Add workers to scheduler
scheduler.addWorker(worker1);
scheduler.addWorker(worker2);
// Initialize workers
await worker1.loadLanguage('eng');
await worker1.initialize('eng');
await worker2.loadLanguage('eng');
await worker2.initialize('eng');
// Convert file to image URL
const imageUrl = URL.createObjectURL(file);
// Perform OCR
const { data: { text } } = await scheduler.addJob('recognize', imageUrl);
// Update text area with results
userData.value = text;
// Clean up
URL.revokeObjectURL(imageUrl);
await scheduler.terminate();
} catch (error) {
console.error('Error processing image:', error);
userData.value = 'Error processing image. Please try again.';
} finally {
// Hide processing indicator
processingIndicator.style.display = 'none';
}
}
// Copy text to clipboard with visual feedback
copyButton.addEventListener('click', async () => {
try {
await navigator.clipboard.writeText(userData.value);
// Update button state
const originalContent = copyButton.innerHTML;
copyButton.innerHTML = '<i class="fas fa-check"></i> Copy Text';
copyButton.classList.add('success');
// Reset button state after 2 seconds
setTimeout(() => {
copyButton.innerHTML = originalContent;
copyButton.classList.remove('success');
}, 2000);
} catch (error) {
console.error('Error copying text:', error);
}
});
// Download text file
downloadButton.addEventListener('click', () => {
const text = userData.value;
const blob = new Blob([text], { type: 'text/plain' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = 'extracted-text.txt';
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
});