Spaces:
Running
Running
| let qrCode = null; | |
| let currentQRText = ''; | |
| // Update character count | |
| document.getElementById('inputText').addEventListener('input', function() { | |
| document.getElementById('textLength').textContent = this.value.length; | |
| }); | |
| // Generate QR Code | |
| function generateQR() { | |
| const text = document.getElementById('inputText').value.trim(); | |
| if (!text) { | |
| alert('β Please enter some text or URL'); | |
| return; | |
| } | |
| currentQRText = text; | |
| const size = parseInt(document.getElementById('sizeSelect').value); | |
| const errorCorrection = document.getElementById('errorSelect').value; | |
| // Clear previous QR code | |
| const container = document.getElementById('qrcodeContainer'); | |
| container.innerHTML = ''; | |
| // Generate new QR code | |
| try { | |
| qrCode = new QRCode(container, { | |
| text: text, | |
| width: size, | |
| height: size, | |
| colorDark: '#000000', | |
| colorLight: '#ffffff', | |
| correctLevel: QRCode.CorrectLevel[errorCorrection] | |
| }); | |
| // Show download buttons | |
| document.getElementById('downloadPNG').style.display = 'block'; | |
| document.getElementById('downloadJPG').style.display = 'block'; | |
| // Update status | |
| document.getElementById('status').textContent = 'β QR Code Generated'; | |
| document.getElementById('status').style.color = '#4CAF50'; | |
| } catch (error) { | |
| alert('β Error generating QR code: ' + error.message); | |
| document.getElementById('status').textContent = 'β Error generating QR code'; | |
| document.getElementById('status').style.color = '#f44336'; | |
| } | |
| } | |
| // Download QR Code | |
| function downloadQR(format) { | |
| const canvas = document.querySelector('#qrcodeContainer canvas'); | |
| if (!canvas) { | |
| alert('β Please generate QR code first'); | |
| return; | |
| } | |
| let link = document.createElement('a'); | |
| if (format === 'png') { | |
| link.download = 'qrcode.png'; | |
| link.href = canvas.toDataURL('image/png'); | |
| } else if (format === 'jpg') { | |
| link.download = 'qrcode.jpg'; | |
| link.href = canvas.toDataURL('image/jpeg', 0.95); | |
| } | |
| document.body.appendChild(link); | |
| link.click(); | |
| document.body.removeChild(link); | |
| } | |
| // Clear all | |
| function clearAll() { | |
| document.getElementById('inputText').value = ''; | |
| document.getElementById('qrcodeContainer').innerHTML = ''; | |
| document.getElementById('downloadPNG').style.display = 'none'; | |
| document.getElementById('downloadJPG').style.display = 'none'; | |
| document.getElementById('textLength').textContent = '0'; | |
| document.getElementById('status').textContent = 'No QR generated yet'; | |
| document.getElementById('status').style.color = '#666'; | |
| currentQRText = ''; | |
| } | |
| // Set example | |
| function setExample(text) { | |
| document.getElementById('inputText').value = text; | |
| document.getElementById('textLength').textContent = text.length; | |
| setTimeout(generateQR, 100); | |
| } | |
| // Allow Enter key to generate | |
| document.getElementById('inputText').addEventListener('keydown', function(e) { | |
| if (e.ctrlKey && e.key === 'Enter') { | |
| generateQR(); | |
| } | |
| }); | |