Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
110 lines (100 loc) · 3.48 KB
/
Copy pathscript.js
File metadata and controls
110 lines (100 loc) · 3.48 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
const imageInput = document.getElementById('imageInput');
const preview = document.getElementById('preview');
const output = document.getElementById('output');
const loading = document.getElementById('loading');
const copyBtn = document.getElementById('copyBtn');
const dropZone = document.getElementById('dropZone');
const pasteBtn = document.getElementById('pasteBtn');
// Process the image file (shared logic for upload, drag-and-drop, and paste)
async function processImage(file) {
if (!file || !file.type.startsWith('image/')) {
output.textContent = 'Please upload, drop, or paste a valid image file.';
preview.style.display = 'none';
loading.style.display = 'none';
return;
}
try {
const url = URL.createObjectURL(file);
preview.src = url;
preview.style.display = 'block';
} catch (error) {
console.error('Error creating object URL:', error);
preview.style.display = 'none';
}
output.textContent = 'Extracted text will appear here...';
loading.style.display = 'block';
try {
const { createWorker } = Tesseract;
const worker = await createWorker('eng');
const { data: { text } } = await worker.recognize(file);
output.textContent = text || 'No text detected.';
await worker.terminate();
} catch (error) {
output.textContent = 'Error processing image: ' + error.message;
} finally {
loading.style.display = 'none';
}
}
// File input change event (for clicking "Upload Image")
imageInput.addEventListener('change', async (event) => {
const file = event.target.files[0];
if (!file) return;
processImage(file);
});
// Drag-and-drop functionality
dropZone.addEventListener('dragover', (event) => {
event.preventDefault();
dropZone.classList.add('drag-over');
});
dropZone.addEventListener('dragleave', () => {
dropZone.classList.remove('drag-over');
});
dropZone.addEventListener('drop', (event) => {
event.preventDefault();
dropZone.classList.remove('drag-over');
const file = event.dataTransfer.files[0];
if (!file) return;
processImage(file);
});
// Keyboard support for drag-and-drop area
dropZone.addEventListener('keydown', (event) => {
if (event.key === 'Enter' || event.key === ' ') {
event.preventDefault();
imageInput.click();
}
});
// Paste button functionality
pasteBtn.addEventListener('click', async () => {
try {
const clipboardItems = await navigator.clipboard.read();
for (const item of clipboardItems) {
if (item.types.includes('image/png') || item.types.includes('image/jpeg')) {
const blob = await item.getType(item.types.find(type => type.startsWith('image/')));
processImage(blob);
return; // Process only the first valid image
}
}
output.textContent = 'No valid image found in clipboard.';
preview.style.display = 'none';
loading.style.display = 'none';
} catch (error) {
console.error('Error accessing clipboard:', error);
output.textContent = 'Error pasting image: ' + error.message;
preview.style.display = 'none';
loading.style.display = 'none';
}
});
// Copy button functionality
copyBtn.addEventListener('click', () => {
const text = output.textContent;
if (text && text !== 'Extracted text will appear here...' && text !== 'No text detected.') {
navigator.clipboard.writeText(text).then(() => {
copyBtn.classList.add('copied');
setTimeout(() => {
copyBtn.classList.remove('copied');
}, 2000);
}).catch((err) => {
console.error('Failed to copy text: ', err);
});
}
});