-
Notifications
You must be signed in to change notification settings - Fork 0
/
qr-widget.js
294 lines (256 loc) · 7.09 KB
/
qr-widget.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
function addModalDialogStyles(border_radius, text_color, background_color) {
const styleEl = document.createElement("style");
styleEl.innerHTML = `
.top-left {
position: fixed;
top: 30px;
left: 30px;
display: flex;
justify-content: flex-start;
align-items: flex-start;
width: 306px;
height: 534px;
}
.top-right {
position: fixed;
top: 30px;
right: 30px;
display: flex;
justify-content: flex-end;
align-items: flex-start;
width: 306px;
height: 534px;
}
.bottom-left {
position: fixed;
bottom: 30px;
left: 30px;
display: flex;
justify-content: flex-start;
align-items: flex-end;
width: 306px;
height: 534px;
}
.bottom-right {
position: fixed;
bottom: 30px;
right: 30px;
display: flex;
justify-content: flex-end;
align-items: flex-end;
width: 306px;
height: 534px;
}
.modal {
position: relative;
background: #FFFFFF;
box-shadow: 1px 1px 5px rgba(0, 0, 0, 0.25);
border-radius: 10px;
width: 100%;
height: 100%;
}
@keyframes fadeIn {
0% {
opacity: 0;
}
100% {
opacity: 1;
}
}
.fade-in {
animation-name: fadeIn;
animation-duration: 0.5s;
animation-timing-function: ease-in;
animation-fill-mode: forwards;
}
@keyframes fadeOut {
from {
opacity: 1;
}
to {
opacity: 0;
}
}
.fade-out {
animation-name: fadeOut;
animation-duration: 0.5s;
animation-timing-function: ease-out;
animation-fill-mode: forwards;
}
#myModal {
position: absolute;
width: 306px;
height: 465px;
left: 0px;
top: 0px;
background: ${background_color};
box-shadow: 1px 1px 5px rgba(0, 0, 0, 0.25);
border-radius: ${border_radius}px;
display: block;
}
.modal-content {
padding: 20px;
}
.close {
font-size: 26px;
position: absolute;
top: 10px;
right: 10px;
cursor: pointer;
}
.qr-code-section {
position: absolute;
width: 252px;
height: 69px;
left: 30px;
right: 30px;
top: 85px;
font-family: 'Inter', sans-serif;
font-style: normal;
font-weight: 400;
font-size: 16px;
line-height: 145%;
color: ${text_color};
}
.qr-code-image {
max-width: 100%;
height: auto;
margin-top: 10px;
}
.qr-code-text {
margin-top: 0;
margin-bottom: 15px;
}
.discount-section {
position: absolute;
height: 56px;
left: 30px;
right: 30px;
top: 30px;
font-family: 'Inter', sans-serif;
font-style: normal;
font-weight: 700;
font-size: 20px;
line-height: 24px;
color: ${text_color};
}
.discount-text {
margin: 0;
}
`;
document.head.appendChild(styleEl);
}
function createCookie(name, value, days) {
if (!getCookie(name)) {
const expires = new Date();
expires.setTime(expires.getTime() + days * 24 * 60 * 60 * 1000);
const cookieValue =
encodeURIComponent(value) +
"; expires=" +
expires.toUTCString() +
"; path=/";
document.cookie = name + "=" + cookieValue;
}
}
function getCookie(name) {
const cookieName = name + "=";
const cookieArray = document.cookie.split(";");
for (let i = 0; i < cookieArray.length; i++) {
let cookie = cookieArray[i];
while (cookie.charAt(0) === " ") {
cookie = cookie.sDubstring(1);
}
if (cookie.indexOf(cookieName) === 0) {
return cookie.substring(cookieName.length, cookie.length);
}
}
return null;
}
function showModal({
heading,
content,
banner_position,
weburl,
border_radius,
text_color,
background_color,
}) {
if (getCookie("qr-widget") == 'closed') {
return;
}
var plateform;
// Check if the user is using Android
if (navigator.userAgent.match(/Android/i)) {
plateform = "Android";
}
// Check if the user is using iOS
else if (navigator.userAgent.match(/iPhone|iPad|iPod/i)) {
plateform = "iOS";
}
// Check if the user is using Windows
else if (navigator.userAgent.match(/Windows/i)) {
plateform = "Windows";
}
if (plateform) {
// document.getElementById("platefrom-name").innerText = plateform;
}
addModalDialogStyles(border_radius, text_color, background_color);
// Create the parent container
const modalContainer = document.createElement("div");
modalContainer.classList.add(banner_position);
// Create the modal
const modal = document.createElement("div");
modal.id = "myModal";
modal.classList.add("modal", "fade-in");
// Create the modal content
const modalContent = document.createElement("div");
modalContent.classList.add("modal-content");
// Create the close button
const closeButton = document.createElement("span");
closeButton.classList.add("close");
closeButton.innerHTML = "×";
// Close the modal
closeButton.onclick = function () {
document.getElementsByClassName("modal")[0].classList.add("fade-out");
createCookie("qr-widget", "closed", 7);
setTimeout(() => {
document.getElementById("myModal").style.display = "none";
}, 500);
};
// Create the discount section
const discountSection = document.createElement("div");
discountSection.classList.add("discount-section");
// Create the discount text
const discountText = document.createElement("p");
discountText.classList.add("discount-text");
discountText.innerHTML = heading;
// Create the QR code section
const qrCodeSection = document.createElement("div");
qrCodeSection.classList.add("qr-code-section");
// Create the QR code text
const qrCodeText = document.createElement("p");
qrCodeText.classList.add("qr-code-text");
qrCodeText.innerHTML = content;
// Create the horizontal line
const hr = document.createElement("hr");
// Create the QR code image
const qrCodeImage = document.createElement("img");
qrCodeImage.src =
"https://chart.googleapis.com/chart?cht=qr&chl=" +
weburl +
"&chs=266x266&chld=L|0";
qrCodeImage.alt = "QR Code";
qrCodeImage.classList.add("qr-code-image");
// Append the elements to their respective parents
discountSection.appendChild(discountText);
qrCodeSection.appendChild(qrCodeText);
qrCodeSection.appendChild(hr);
qrCodeSection.appendChild(qrCodeImage);
modalContent.appendChild(closeButton);
modalContent.appendChild(discountSection);
modalContent.appendChild(qrCodeSection);
modal.appendChild(modalContent);
modalContainer.appendChild(modal);
// Append the modalContainer to the body or any other desired parent element
document.body.appendChild(modalContainer);
}