-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
165 lines (141 loc) · 4.35 KB
/
app.js
File metadata and controls
165 lines (141 loc) · 4.35 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
/**
* QR Code Reader - Tutorial Example
*
* A simple QR code scanner using the APIVerve QR Code Reader API.
* https://apiverve.com/marketplace/qrcodereader
*/
// ============================================
// CONFIGURATION - Add your API key here
// Get a free key at: https://dashboard.apiverve.com
// ============================================
const API_KEY = 'your-api-key-here';
const API_URL = 'https://api.apiverve.com/v1/qrcodereader';
// DOM Elements
const uploadArea = document.getElementById('uploadArea');
const fileInput = document.getElementById('fileInput');
const preview = document.getElementById('preview');
const previewImage = document.getElementById('previewImage');
const clearBtn = document.getElementById('clearBtn');
const scanBtn = document.getElementById('scanBtn');
const error = document.getElementById('error');
const result = document.getElementById('result');
const resultText = document.getElementById('resultText');
const copyBtn = document.getElementById('copyBtn');
let selectedFile = null;
// Upload area click handler
uploadArea.addEventListener('click', () => fileInput.click());
// File selection handler
fileInput.addEventListener('change', (e) => {
if (e.target.files.length > 0) {
handleFile(e.target.files[0]);
}
});
// Drag and drop handlers
uploadArea.addEventListener('dragover', (e) => {
e.preventDefault();
uploadArea.classList.add('dragover');
});
uploadArea.addEventListener('dragleave', () => {
uploadArea.classList.remove('dragover');
});
uploadArea.addEventListener('drop', (e) => {
e.preventDefault();
uploadArea.classList.remove('dragover');
if (e.dataTransfer.files.length > 0) {
handleFile(e.dataTransfer.files[0]);
}
});
// Handle selected file
function handleFile(file) {
// Validate file type
if (!file.type.startsWith('image/')) {
showError('Please select an image file');
return;
}
// Validate file size (5MB max)
if (file.size > 5 * 1024 * 1024) {
showError('File size must be less than 5MB');
return;
}
selectedFile = file;
// Show preview
const reader = new FileReader();
reader.onload = (e) => {
previewImage.src = e.target.result;
preview.classList.add('show');
uploadArea.style.display = 'none';
scanBtn.disabled = false;
};
reader.readAsDataURL(file);
// Clear previous results
hideError();
result.classList.remove('show');
}
// Clear selection
clearBtn.addEventListener('click', () => {
selectedFile = null;
fileInput.value = '';
preview.classList.remove('show');
uploadArea.style.display = 'block';
scanBtn.disabled = true;
result.classList.remove('show');
hideError();
});
// Scan QR code
scanBtn.addEventListener('click', async () => {
if (!selectedFile) return;
// Check API key
if (API_KEY === 'your-api-key-here') {
showError('Add your API key to js/app.js first');
return;
}
scanBtn.disabled = true;
scanBtn.textContent = 'Scanning...';
hideError();
result.classList.remove('show');
try {
// Create form data with the image
const formData = new FormData();
formData.append('image', selectedFile);
// Call the API
const response = await fetch(API_URL, {
method: 'POST',
headers: {
'x-api-key': API_KEY
},
body: formData
});
const data = await response.json();
if (data.status === 'ok' && data.data) {
// Show result
resultText.textContent = data.data.text || 'No text found';
result.classList.add('show');
} else {
showError(data.error || 'No QR code found in image');
}
} catch (err) {
showError('Failed to scan QR code. Check your API key.');
console.error('API Error:', err);
} finally {
scanBtn.disabled = false;
scanBtn.textContent = 'Scan QR Code';
}
});
// Copy to clipboard
copyBtn.addEventListener('click', () => {
navigator.clipboard.writeText(resultText.textContent).then(() => {
const originalText = copyBtn.textContent;
copyBtn.textContent = 'Copied!';
setTimeout(() => {
copyBtn.textContent = originalText;
}, 2000);
});
});
// Error helpers
function showError(message) {
error.textContent = message;
error.classList.add('show');
}
function hideError() {
error.classList.remove('show');
}