-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathscript.js
More file actions
332 lines (302 loc) · 7.79 KB
/
script.js
File metadata and controls
332 lines (302 loc) · 7.79 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
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
// invert Map (key:value -> value:key)
const invertMap = map => {
const invertedMap = {};
for (const [key, value] of Object.entries(map)) {
if (!invertedMap.hasOwnProperty(value)) {
invertedMap[value] = key;
}
}
return invertedMap;
}
document.addEventListener('DOMContentLoaded', () => {
const tabs = document.querySelectorAll('.tab-button');
const contents = document.querySelectorAll('.tab-content');
tabs.forEach((tab) => {
tab.addEventListener('click', () => {
tabs.forEach((t) => t.classList.remove('active'));
tab.classList.add('active');
const target = tab.getAttribute('data-tab');
// GA Tracking: Tab switch
if (typeof gtag === 'function') {
gtag('event', 'tab_switch', {
'tab_name': target
});
}
contents.forEach((content) => {
if (content.id === target) {
content.classList.add('active');
} else {
content.classList.remove('active');
}
});
});
});
function isUpperCase(char) {
return char >= 'A' && char <= 'Z';
}
const capitalIndicator = 'ه';
const toFaMapper = {
a: 'ش',
b: 'ل',
c: 'ض',
d: 'ب',
e: 'ع',
f: 'گ',
g: 'و',
h: 'ظ',
i: 'س',
j: 'ژ',
k: 'ک',
l: 'م',
m: 'ن',
n: 'پ',
o: 'غ',
p: 'ح',
q: 'ز',
r: 'ط',
s: 'ر',
t: 'ق',
u: 'ث',
v: 'ف',
w: 'ی',
x: 'د',
y: 'ذ',
z: 'خ',
0: '۴',
1: '۹',
2: '۲',
3: '۱',
4: '۵',
5: '۰',
6: '۳',
7: '۶',
8: '۸',
9: '۷',
' ': ' ',
':': ':',
'/': 'ت',
'!': '!',
'@': '@',
'#': '#',
$: '$',
'%': '%',
'^': '^',
'&': 'ا',
'*': '*',
'(': '(',
')': ')',
'-': '-',
_: '_',
'+': '+',
'?': '?',
'=': 'چ',
'#': '#',
$: '$',
'%': 'ص',
'.': '.',
',': ',',
';': ';',
':': ':',
'|': '|',
'~': '~',
'`': '`',
};
const toEnMapper = invertMap(toFaMapper);
// Encode
const encodeCopyButton = document.getElementById('encode-copy');
const encodeInput = document.getElementById('encode-input');
const encodeOutput = document.getElementById('encode-output');
encodeCopyButton.disabled = true;
encodeInput.addEventListener('input', () => {
try {
const encoded = encodeInput.value
.split('')
.map((letter) => {
const prefix = isUpperCase(letter) ? capitalIndicator : '';
return prefix + toFaMapper[letter.toLowerCase()];
})
.join('');
encodeOutput.textContent = encoded;
// GA Tracking: Encode action
if (typeof gtag === 'function' && encoded) {
gtag('event', 'encode_config', {
'char_count': encodeInput.value.length
});
}
} catch (e) {
encodeOutput.textContent = 'Error encoding input: ' + e.message;
}
encodeCopyButton.disabled = !encodeOutput.textContent;
});
encodeCopyButton.addEventListener('click', () => {
navigator.clipboard
.writeText(encodeOutput.textContent)
.then(() => {
encodeCopyButton.textContent = 'کپی شد !';
// GA Tracking: Copy Encode
if (typeof gtag === 'function') {
gtag('event', 'copy_output', {
'type': 'encode'
});
}
setTimeout(() => {
encodeCopyButton.textContent = 'کپی';
}, 3000);
})
.catch(() => {
encodeCopyButton.textContent = 'Error while copying.';
// GA Tracking: Copy Error
if (typeof gtag === 'function') {
gtag('event', 'copy_error', {
'type': 'encode'
});
}
});
});
// Decode
const decodeCopyButton = document.getElementById('decode-copy');
const decodeInput = document.getElementById('decode-input');
const decodeOutput = document.getElementById('decode-output');
decodeCopyButton.disabled = true;
decodeInput.addEventListener('input', () => {
try {
let input = decodeInput.value;
let isNextCapital = false;
const decoded = input
.split('')
.map((letter) => {
if (letter === capitalIndicator) {
isNextCapital = true;
return '';
}
const res = isNextCapital
? toEnMapper[letter].toUpperCase()
: toEnMapper[letter];
isNextCapital = false;
return res;
})
.join('');
decodeOutput.textContent = decoded;
// GA Tracking: Decode action
if (typeof gtag === 'function' && decoded) {
gtag('event', 'decode_config', {
'char_count': decodeInput.value.length
});
}
} catch (e) {
decodeOutput.textContent = 'Error decoding input';
}
decodeCopyButton.disabled = !decodeOutput.textContent;
});
decodeCopyButton.addEventListener('click', () => {
navigator.clipboard
.writeText(decodeOutput.textContent)
.then(() => {
decodeCopyButton.textContent = 'کپی شد !';
// GA Tracking: Copy Decode
if (typeof gtag === 'function') {
gtag('event', 'copy_output', {
'type': 'decode'
});
}
setTimeout(() => {
decodeCopyButton.textContent = 'کپی';
}, 3000);
})
.catch(() => {
encodeCopyButton.textContent = 'Error while copying.';
// GA Tracking: Copy Error
if (typeof gtag === 'function') {
gtag('event', 'copy_error', {
'type': 'decode'
});
}
});
});
// PWA Install Button Logic
const installBtn = document.getElementById('install-btn');
let deferredPrompt;
window.addEventListener('beforeinstallprompt', (e) => {
e.preventDefault();
deferredPrompt = e;
installBtn.hidden = false;
// GA Tracking: Install prompt available
if (typeof gtag === 'function') {
gtag('event', 'pwa_prompt_available');
}
});
installBtn.addEventListener('click', async () => {
if (!deferredPrompt) return;
installBtn.disabled = true;
deferredPrompt.prompt();
const { outcome } = await deferredPrompt.userChoice;
if (outcome === 'accepted') {
installBtn.textContent = 'برنامه نصب شد!';
// GA Tracking: Install accepted via button
if (typeof gtag === 'function') {
gtag('event', 'pwa_install_accepted');
}
setTimeout(() => {
installBtn.hidden = true;
}, 3000);
} else {
installBtn.textContent = 'نصب برنامه توسط کاربر رد شد!';
installBtn.hidden = false;
// GA Tracking: Install dismissed
if (typeof gtag === 'function') {
gtag('event', 'pwa_install_dismissed');
}
}
deferredPrompt = null;
});
// iOS install banner behavior (dismiss persistence)
const iosBanner = document.getElementById('ios-install-banner');
const iosClose = document.getElementById('ios-install-close');
const IOS_BANNER_DISMISSED_KEY = 'iosBannerDismissed';
if (iosClose) {
iosClose.addEventListener('click', () => {
if (iosBanner) iosBanner.hidden = true;
// GA Tracking: iOS banner dismissed
if (typeof gtag === 'function') {
gtag('event', 'ios_install_banner_dismissed');
}
try {
localStorage.setItem(IOS_BANNER_DISMISSED_KEY, '1');
} catch (e) {
// ignore storage errors
}
});
}
// iOS detection and banner show logic
const isIos = /iPhone|iPad|iPod/.test(navigator.userAgent);
const isInStandaloneMode = ('standalone' in window.navigator) && (window.navigator.standalone);
// Try to show the banner only if: on iOS, not in standalone mode, and not previously dismissed
if (isIos && !isInStandaloneMode) {
try {
const dismissed = localStorage.getItem(IOS_BANNER_DISMISSED_KEY);
if (!dismissed && iosBanner) {
iosBanner.hidden = false;
// GA Tracking: iOS banner shown
if (typeof gtag === 'function') {
gtag('event', 'ios_install_banner_shown');
}
}
} catch (e) {
// localStorage may be unavailable; still show banner
if (iosBanner) iosBanner.hidden = false;
// GA Tracking: iOS banner shown (despite storage error)
if (typeof gtag === 'function') {
gtag('event', 'ios_install_banner_shown', { 'storage_error': true });
}
}
}
// Track actual installation (generic for PWA)
window.addEventListener('appinstalled', () => {
if (typeof gtag === 'function') {
gtag('event', 'pwa_installed');
}
});
});
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('sw.js');
}