-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
147 lines (116 loc) · 3.53 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
handleEncrypt = () => {
action.decodeText(action.encryptText);
}
handleDecrypt = () => {
action.decodeText(action.decryptText);
}
handleCopyInput = () => {
action.copyText('textarea__input', 'textbox__input__copy');
}
handleCopyOutput = () => {
action.copyText('textarea__output', 'textbox__output__copy');
}
handleClearInput = () => {
action.clearText('textarea__input');
}
handleClearOutput = () => {
action.clearText('textarea__output');
}
handleConvertToValidText = () => {
action.convertToValidText('textarea__input');
}
const action = {
convertToValidText: (fieldId) => {
let textarea = document.getElementById(fieldId);
textarea.value = textarea.value.toLowerCase().normalize('NFD').replace(/[\u0300-\u036f\u005F]/g, '').replace(/[^\w\s]/g, '');
},
encryptText: (text) => {
const encryptKey = {
'e': 'enter',
'i': 'imes',
'a': 'ai',
'o': 'ober',
'u': 'ufat'
};
const encryptText = text.split('').map(char => encryptKey[char] || char).join('');
return encryptText;
},
decryptText: (text) => {
const decryptKey = {
'enter': 'e',
'imes': 'i',
'ai': 'a',
'ober': 'o',
'ufat': 'u'
};
const regex = new RegExp(Object.keys(decryptKey).join('|'), 'g');
const decryptText = text.replace(regex, (match) => decryptKey[match]);
return decryptText;
},
checkText: (text) => {
const regex = /^[a-z\d\s]+$/;
return regex.test(text);
},
decodeText: (operation) => {
let sendField = document.getElementById('textarea__input').value;
let receiveField = document.getElementById('textarea__output');
if (action.checkText(sendField.trim())) {
receiveField.value = operation(sendField.trim());
} else {
notification.invalidDecode();
}
},
copyText: (fieldId, buttonId) => {
const text = document.getElementById(fieldId);
if(text.value != '') {
text.select();
text.setSelectionRange(0, 99999);
navigator.clipboard.writeText(text.value);
notification.copy(buttonId);
}
},
clearText: (fieldId) => {
const textarea = document.getElementById(fieldId);
textarea.value = '';
if(textarea.id.includes('input')) {
textarea.focus();
}
},
}
const notification = {
copy: (buttonId) => {
let copyButton = document.getElementById(buttonId);
const originalTextColor = '#F6F5AE';
const originalText = 'Copiar';
const alertColor = '#16b13a';
const alertText = 'Copiado';
copyButton.innerHTML = alertText;
copyButton.style.border = `2px solid ${alertColor}`;
setTimeout(function() {
copyButton.innerHTML = originalText;
copyButton.style.border = `2px solid ${originalTextColor}`;
}, 500);
},
invalidDecode: () => {
let notifyField = document.querySelector('.textbox__warning__text');
let notifyIcon = document.querySelector('.textbox__warning__icon');
let count = 0;
const originalTextColor = '#F6F5AE';
const originalBackgroundColor = '#1A1423';
const alertColor = '#FF0000';
const interval = setInterval(() => {
if(count % 2 === 0) {
notifyField.style.color = alertColor;
notifyIcon.style.backgroundColor = alertColor;
} else {
notifyField.style.color = originalTextColor;
notifyIcon.style.backgroundColor = originalBackgroundColor;
}
if (++count === 20) {
clearInterval(interval);
notifyField.style.color = originalTextColor;
notifyIcon.style.backgroundColor = originalBackgroundColor;
}
}, 100);
},
}