-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
90 lines (82 loc) · 3.29 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1, user-scalable=no">
<title>QR Code Generator</title>
<link rel="stylesheet" href="main.css">
</head>
<body>
<div class="container">
<h1>QR Code Generator</h1>
<div class="image">
<img src="No-Image-Placeholder.png" id="image">
</div>
<div class="form">
<input id="content" type="text" placeholder="Enter Content">
<div class="buttons">
<button class="generate" id="btnCreate">Generate QR</button>
<button id="resetbtn">Reset</button>
</div>
<div class="download">
<button id="download">Download</button>
</div>
</div>
</div>
<script>
let image = document.getElementById('image');
let content = document.getElementById('content');
let generateButton = document.getElementById('btnCreate');
let resetButton = document.getElementById('resetbtn');
let downloadButton = document.getElementById('download');
// Initially hide the download button
downloadButton.style.display = 'none';
generateButton.addEventListener('click', generateQR);
function generateQR(e) {
if (content.value == '') {
return;
}
let url = 'https://api.qrserver.com/v1/create-qr-code/';
let qrImageUrl = url + '?size=500x500&data=' + encodeURIComponent(content.value); // No need to specify format
fetch(qrImageUrl)
.then(response => {
if (!response.ok) {
throw new Error('Failed to generate QR code.');
}
return response.blob();
})
.then(blob => {
// Display generated QR code
image.src = URL.createObjectURL(blob);
// Show download button
downloadButton.style.display = 'block';
// Set download link
downloadButton.onclick = function() {
// Trigger download
const a = document.createElement('a');
a.href = URL.createObjectURL(blob);
a.download = 'qrcode.png';
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
// Reset content and image
content.value = '';
image.src = 'No-Image-Placeholder.png';
// Hide download button
downloadButton.style.display = 'none';
};
})
.catch(error => {
console.error('Error:', error);
alert('Failed to generate QR code.');
});
}
resetButton.addEventListener('click', function() {
content.value = '';
image.src = 'No-Image-Placeholder.png';
// Hide download button
downloadButton.style.display = 'none';
});
</script>
</body>
</html>