| 1 |
/* QR Code — frontend */ |
| 2 |
(function () { |
| 3 |
function renderAll() { |
| 4 |
document.querySelectorAll('.bkqr-box[data-content]').forEach(function (box) { |
| 5 |
if (box._bkqrDone) return; |
| 6 |
box._bkqrDone = true; |
| 7 |
var canvas = box.querySelector('.bkqr-canvas'); |
| 8 |
if (!canvas) return; |
| 9 |
try { |
| 10 |
var qr = new window.QRCode(canvas, { |
| 11 |
text: box.getAttribute('data-content') || 'https://example.com', |
| 12 |
width: parseInt(box.getAttribute('data-size'), 10) || 200, |
| 13 |
height: parseInt(box.getAttribute('data-size'), 10) || 200, |
| 14 |
colorDark: box.getAttribute('data-fg') || '#1f2937', |
| 15 |
colorLight: box.getAttribute('data-bg') || '#ffffff', |
| 16 |
correctLevel: window.QRCode.CorrectLevel[box.getAttribute('data-level') || 'M'], |
| 17 |
}); |
| 18 |
|
| 19 |
/* download button */ |
| 20 |
var btn = box.querySelector('.bkqr-download'); |
| 21 |
if (btn) { |
| 22 |
btn.addEventListener('click', function () { |
| 23 |
var cvs = canvas.querySelector('canvas'); |
| 24 |
if (!cvs) return; |
| 25 |
var link = document.createElement('a'); |
| 26 |
link.download = 'qrcode.png'; |
| 27 |
link.href = cvs.toDataURL('image/png'); |
| 28 |
link.click(); |
| 29 |
}); |
| 30 |
} |
| 31 |
} catch (e) { console.warn('bkqr error', e); } |
| 32 |
}); |
| 33 |
} |
| 34 |
|
| 35 |
function init() { |
| 36 |
if (typeof window.QRCode !== 'undefined') { renderAll(); return; } |
| 37 |
var s = document.createElement('script'); |
| 38 |
s.src = 'https://cdnjs.cloudflare.com/ajax/libs/qrcodejs/1.0.0/qrcode.min.js'; |
| 39 |
s.onload = renderAll; |
| 40 |
document.head.appendChild(s); |
| 41 |
} |
| 42 |
|
| 43 |
if (document.readyState === 'loading') { |
| 44 |
document.addEventListener('DOMContentLoaded', init); |
| 45 |
} else { |
| 46 |
init(); |
| 47 |
} |
| 48 |
}()); |
| 49 |
|