-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
142 lines (116 loc) · 3.86 KB
/
app.js
File metadata and controls
142 lines (116 loc) · 3.86 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
// QR Code Generator Application
// DOM Elements
const urlInput = document.getElementById('urlInput');
const generateBtn = document.getElementById('generateBtn');
const downloadBtn = document.getElementById('downloadBtn');
const qrCodeCard = document.getElementById('qrCodeCard');
const qrCodeContainer = document.getElementById('qrcode');
const errorMessage = document.getElementById('errorMessage');
// QR Code instance
let qrCodeInstance = null;
// URL Validation Function
function isValidURL(string) {
try {
const url = new URL(string);
return url.protocol === 'http:' || url.protocol === 'https:';
} catch (err) {
return false;
}
}
// Show Error Message
function showError(message) {
errorMessage.textContent = message;
errorMessage.classList.remove('hidden');
urlInput.classList.add('border-red-500');
urlInput.classList.remove('border-gray-300');
}
// Hide Error Message
function hideError() {
errorMessage.classList.add('hidden');
urlInput.classList.remove('border-red-500');
urlInput.classList.add('border-gray-300');
}
// Generate QR Code
function generateQRCode() {
const url = urlInput.value.trim();
// Hide any previous errors
hideError();
// Validate input
if (!url) {
showError('Please enter a URL');
return;
}
if (!isValidURL(url)) {
showError('Please enter a valid URL (must start with http:// or https://)');
return;
}
// Clear previous QR code
qrCodeContainer.innerHTML = '';
// Generate new QR code
try {
qrCodeInstance = new QRCode(qrCodeContainer, {
text: url,
width: 256,
height: 256,
colorDark: '#000000',
colorLight: '#ffffff',
correctLevel: QRCode.CorrectLevel.H
});
// Show QR code card with animation
qrCodeCard.classList.remove('hidden');
qrCodeCard.classList.add('animate-fadeIn');
// Scroll to QR code
setTimeout(() => {
qrCodeCard.scrollIntoView({ behavior: 'smooth', block: 'nearest' });
}, 100);
} catch (error) {
showError('Failed to generate QR code. Please try again.');
console.error('QR Code generation error:', error);
}
}
// Download QR Code
function downloadQRCode() {
try {
// Get the canvas element from QRCode.js
const canvas = qrCodeContainer.querySelector('canvas');
if (!canvas) {
alert('No QR code to download. Please generate one first.');
return;
}
// Convert canvas to blob and download
canvas.toBlob(function(blob) {
const url = URL.createObjectURL(blob);
const link = document.createElement('a');
const timestamp = new Date().getTime();
link.download = `qrcode-${timestamp}.png`;
link.href = url;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
// Clean up the URL object
URL.revokeObjectURL(url);
});
} catch (error) {
alert('Failed to download QR code. Please try again.');
console.error('Download error:', error);
}
}
// Event Listeners
generateBtn.addEventListener('click', generateQRCode);
downloadBtn.addEventListener('click', downloadQRCode);
// Allow Enter key to generate QR code
urlInput.addEventListener('keypress', function(event) {
if (event.key === 'Enter') {
generateQRCode();
}
});
// Clear error when user starts typing
urlInput.addEventListener('input', function() {
if (errorMessage.classList.contains('hidden') === false) {
hideError();
}
});
// Focus on input when page loads
window.addEventListener('load', function() {
urlInput.focus();
});