Files
scripts/QRCode.html
2025-02-01 13:47:16 +02:00

243 lines
7.1 KiB
HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>QR Code Generator</title>
<script src="https://cdn.jsdelivr.net/gh/davidshimjs/qrcodejs/qrcode.min.js"></script>
<style>
body {
font-family: Arial, sans-serif;
max-width: 800px;
margin: 0 auto;
padding: 20px;
background-color: #f0f0f0;
}
.container {
background-color: white;
padding: 20px;
border-radius: 8px;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}
.input-group {
margin-bottom: 15px;
}
label {
display: block;
margin-bottom: 5px;
font-weight: bold;
}
input, textarea {
width: 100%;
padding: 8px;
border: 1px solid #ddd;
border-radius: 4px;
box-sizing: border-box;
}
.button-group {
display: flex;
gap: 10px;
margin-bottom: 20px;
}
button {
background-color: #4CAF50;
color: white;
padding: 10px 20px;
border: none;
border-radius: 4px;
cursor: pointer;
font-size: 16px;
}
button:hover {
background-color: #45a049;
}
button.copy-button {
background-color: #2196F3;
}
button.copy-button:hover {
background-color: #1976D2;
}
button:disabled {
background-color: #cccccc;
cursor: not-allowed;
}
.qr-container {
margin-top: 20px;
display: flex;
flex-direction: column;
align-items: center;
gap: 10px;
}
#qrcode {
display: flex;
justify-content: center;
width: 256px;
}
#qrcode img {
display: block;
}
#qrLabel {
font-weight: bold;
text-align: center;
max-width: 256px;
word-wrap: break-word;
}
.copy-status {
color: #4CAF50;
font-size: 14px;
margin-top: 5px;
opacity: 0;
transition: opacity 0.3s ease;
}
.copy-status.visible {
opacity: 1;
}
/* Hidden canvas for combining QR and label */
#combinedCanvas {
display: none;
}
@media (max-width: 600px) {
body {
padding: 10px;
}
}
</style>
</head>
<body>
<div class="container">
<h1>QR Code Generator</h1>
<div class="input-group">
<label for="content">Content:</label>
<textarea id="content" rows="4" placeholder="Enter text or URL to encode"></textarea>
</div>
<div class="input-group">
<label for="label">Label:</label>
<input type="text" id="label" placeholder="Enter label for the QR code">
</div>
<div class="button-group">
<button onclick="generateQR()">Generate QR Code</button>
<button class="copy-button" onclick="copyQRToClipboard()" id="copyButton" disabled>
Copy for Word
</button>
</div>
<div class="copy-status" id="copyStatus">Copied to clipboard!</div>
<div class="qr-container">
<div id="qrcode"></div>
<div id="qrLabel"></div>
</div>
<!-- Hidden canvas for combining QR and label -->
<canvas id="combinedCanvas"></canvas>
</div>
<script>
function generateQR() {
// Clear previous QR code
const qrcodeDiv = document.getElementById('qrcode');
qrcodeDiv.innerHTML = '';
// Get input values
const content = document.getElementById('content').value;
const label = document.getElementById('label').value;
if (!content) {
alert('Please enter content for the QR code');
return;
}
// Create QR code
new QRCode(qrcodeDiv, {
text: content,
width: 256,
height: 256,
colorDark: '#000000',
colorLight: '#ffffff',
correctLevel: QRCode.CorrectLevel.H
});
// Update label
const labelDiv = document.getElementById('qrLabel');
labelDiv.textContent = label || '';
// Enable copy button after QR code is generated
document.getElementById('copyButton').disabled = false;
}
async function copyQRToClipboard() {
try {
const qrImage = document.querySelector('#qrcode img');
const label = document.getElementById('qrLabel').textContent;
if (!qrImage) return;
// Create a canvas for the combined image
const canvas = document.getElementById('combinedCanvas');
const ctx = canvas.getContext('2d');
// Set canvas size to accommodate both QR code and label
const padding = 20;
const fontSize = 16;
ctx.font = `bold ${fontSize}px Arial`;
const labelMetrics = ctx.measureText(label);
canvas.width = Math.max(qrImage.width, labelMetrics.width);
canvas.height = qrImage.height + (label ? fontSize + padding : 0);
// Fill with white background
ctx.fillStyle = '#ffffff';
ctx.fillRect(0, 0, canvas.width, canvas.height);
// Draw QR code
const qrX = (canvas.width - qrImage.width) / 2;
ctx.drawImage(qrImage, qrX, 0);
// Draw label if it exists
if (label) {
ctx.fillStyle = '#000000';
ctx.font = `bold ${fontSize}px Arial`;
ctx.textAlign = 'center';
ctx.fillText(label, canvas.width / 2, qrImage.height + fontSize);
}
// Convert canvas to blob
const blob = await new Promise(resolve => canvas.toBlob(resolve, 'image/png'));
// Create ClipboardItem and copy to clipboard
const item = new ClipboardItem({ 'image/png': blob });
await navigator.clipboard.write([item]);
// Show success message
const copyStatus = document.getElementById('copyStatus');
copyStatus.classList.add('visible');
setTimeout(() => {
copyStatus.classList.remove('visible');
}, 2000);
} catch (err) {
console.error('Failed to copy:', err);
alert('Failed to copy QR code. Your browser might not support this feature.');
}
}
</script>
</body>
</html>