-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
139 lines (114 loc) · 4.31 KB
/
script.js
File metadata and controls
139 lines (114 loc) · 4.31 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
const qrText = document.getElementById('qr-text');
const sizes = document.getElementById('sizes');
const generateBtn = document.getElementById('generateBtn');
const downloadBtn = document.getElementById('downloadBtn');
const shareBtn = document.getElementById('shareBtn'); // New Share button
const qrContainer = document.querySelector('.qr-body');
const historyContainer = document.getElementById('history-container'); // For displaying history
let size = 200; // Default QR code size
// Load history on page load
window.addEventListener('load', loadHistory);
generateBtn.addEventListener('click', (e) => {
e.preventDefault();
isEmptyInput();
});
downloadBtn.addEventListener('click', () => {
let img = document.querySelector('.qr-body img');
if (img !== null) {
let imgAttr = img.getAttribute('src');
downloadBtn.setAttribute("href", imgAttr);
} else {
downloadBtn.setAttribute("href", `${document.querySelector('canvas').toDataURL()}`);
}
});
shareBtn.addEventListener('click', async (e) => {
e.preventDefault();
const qrCanvas = document.querySelector('.qr-body canvas'); // Locate the QR Code canvas
if (!qrCanvas) {
alert('Please generate a QR code before sharing!');
return;
}
// Convert the QR code canvas to a Blob
qrCanvas.toBlob(async (blob) => {
const file = new File([blob], 'qr-code.png', { type: 'image/png' });
// Check if the Web Share API is supported
if (navigator.share) {
try {
await navigator.share({
title: 'My QR Code',
text: 'Check out this QR Code I generated!',
files: [file],
});
alert('QR Code shared successfully!');
} catch (error) {
console.error('Error sharing:', error);
alert('Failed to share the QR code.');
}
} else {
alert('Sharing is not supported on this device.');
}
});
});
function isEmptyInput() {
qrText && qrText.value.length > 0 ? generateQRCode() : alert("Enter the text or URL to generate your QR code");
}
function generateQRCode() {
let text = qrText ? qrText.value.trim() : '';
// Check if the input is a valid URL
if (!text.startsWith('http://') && !text.startsWith('https://')) {
text = 'https://' + text;
}
qrContainer.innerHTML = "";
const qrCode = new QRCode(qrContainer, {
text: text,
height: size,
width: size,
colorLight: "#fff",
colorDark: "#000",
});
saveToHistory(text); // Save the generated QR code to history
}
// Save the QR code text to localStorage
function saveToHistory(text) {
let history = JSON.parse(localStorage.getItem('qrHistory')) || [];
if (!history.includes(text)) {
history.push(text);
// Keep only the last two entries in localStorage
if (history.length > 2) {
history = history.slice(-2);
}
localStorage.setItem('qrHistory', JSON.stringify(history));
}
displayHistory();
}
// Load and display the history from localStorage
function loadHistory() {
displayHistory();
}
function displayHistory() {
const history = JSON.parse(localStorage.getItem('qrHistory')) || [];
const recentHistory = history.slice(-2); // Show only the last two entries
historyContainer.innerHTML = recentHistory.length
? `<h3>Generated QR Code History</h3><ul>${recentHistory
.map((item) => `<li><a href="${item}" target="_blank">${item}</a></li>`)
.join('')}</ul>`
: '<h3>No history available</h3>';
}
resetBtn.addEventListener('click', (e) => {
e.preventDefault(); // Prevent default anchor behavior
// Clear the QR text input
if (qrText) qrText.value = '';
// Clear the QR container
qrContainer.innerHTML = '';
// Reset the history display
historyContainer.innerHTML = '<h3>No history available</h3>';
// Clear localStorage
localStorage.removeItem('qrHistory');
// Show the reset notification
const notification = document.getElementById('reset-notification');
notification.style.display = 'block';
// Hide the notification after 3 seconds
setTimeout(() => {
notification.style.display = 'none';
}, 2000); // Adjust time as needed
});