-
Notifications
You must be signed in to change notification settings - Fork 1
/
script.js
171 lines (144 loc) · 4.71 KB
/
script.js
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// QR Code Reader
const readerBox = document.querySelector(".reader_box"),
form = document.querySelector("form"),
fileInp = form.querySelector("input"),
infoText = form.querySelector("p"),
iFont = form.querySelector("i"),
closeBtn = document.querySelector(".closeBtn"),
copyBtn = document.querySelector(".copyBtn"),
loader = document.querySelector("#loader");
function fetchRequest(file, formData) {
iFont.style.display = 'none';
loader.style.display = 'block';
infoText.innerText = "Scanning QR Code...";
fetch("http://api.qrserver.com/v1/read-qr-code/", {
method: 'POST', body: formData
}).then(res => res.json()).then(result => {
result = result[0].symbol[0].data;
infoText.innerText = result ? `Click To Choose Or
Drop QR Code To Read` : "Couldn't Scan QR Code...Click To Choose Or Drop Another QR Code To Read";
loader.style.display = 'none';
if(!result) return;
document.querySelector("textarea").innerText = result;
form.querySelector("img").src = URL.createObjectURL(file);
readerBox.classList.add("active");
}).catch(() => {
infoText.innerText = "Couldn't Scan QR Code...Click To Choose Or Drop Another QR Code To Read";
loader.style.display = 'none';
});
}
fileInp.addEventListener("change", async e => {
let file = e.target.files[0];
if(!file) return;
let formData = new FormData();
formData.append('file', file);
fetchRequest(file, formData);
});
copyBtn.addEventListener("click", () => {
let text = document.querySelector("textarea").textContent;
navigator.clipboard.writeText(text);
});
form.addEventListener("click", () => fileInp.click());
closeBtn.addEventListener("click", () => {
loader.style.display = 'none';
iFont.style.display = 'block';
readerBox.classList.remove("active");
});
// Drag & Drop
form.addEventListener("dragenter",(e) => {
e.preventDefault();
e.stopPropagation();
form.classList.add("drop_active");
},false
);
form.addEventListener("dragleave",(e) => {
e.preventDefault();
e.stopPropagation();
form.classList.remove("drop_active");
},false
);
form.addEventListener("dragover",(e) => {
e.preventDefault();
e.stopPropagation();
form.classList.add("drop_active");
},false
);
form.addEventListener("drop",(e) => {
e.preventDefault();
e.stopPropagation();
form.classList.remove("drop_active");
let draggedData = e.dataTransfer;
files = draggedData.files;
file = files[0];
formData = new FormData();
formData.append('file', file);
fetchRequest(file, formData);
},false
);
// End QR Code Reader
// QR Code Generator
const showQr = document.querySelector(".show_qr");
const userInput = document.getElementById("userInput");
const submitBtn = document.getElementById("submitBtn");
const cancelBtn = document.getElementById("cancelBtn");
const downloadBtn = document.getElementById("downloadBtn");
let QR_Code;
const inputFormatter = (value) => {
value = value.replace(/[^a-z0-9A-Z]+/g, "");
return value;
};
submitBtn.addEventListener("click", async () => {
showQr.innerHTML = "";
QR_Code = await new QRCode(showQr, {
text: userInput.value,
});
const src = showQr.firstChild.toDataURL("image/pmg");
downloadBtn.href = src;
let userValue = 'Untitled';
try {
userValue = new URL(userValue).hostname;
} catch (_) {
userValue = inputFormatter(userValue);
downloadBtn.download = `${userValue}CosaslearningQR`;
downloadBtn.classList.remove("hide");
submitBtn.classList.add("hide");
cancelBtn.classList.remove("hide");
submitBtn.disabled = false;
}
});
userInput.addEventListener("input", () => {
if (userInput.value.trim().length < 1) {
submitBtn.disabled = true;
downloadBtn.href = "";
downloadBtn.classList.add("hide");
} else {
submitBtn.disabled = false;
}
});
downloadBtn.addEventListener("click", () => {
window.onload();
});
cancelBtn.addEventListener("click", () => {
window.onload();
});
window.onload = () => {
showQr.innerHTML = "";
userInput.value = "";
downloadBtn.classList.add("hide");
cancelBtn.classList.add("hide");
submitBtn.disabled = true;
submitBtn.classList.remove("hide");
};
// End QR Code Generator
// Toast Notification
let toastBox = document.getElementById('toastBox');
function showToast(){
let toast = document.createElement('div');
toast.classList.add('toast');
toast.innerHTML = '<i class="fa-solid fa-circle-check"></i> Copied!';
toastBox.appendChild(toast);
setTimeout(()=>{
toast.remove();
},3000);
}
// End Toast Notification