-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
143 lines (137 loc) · 4.49 KB
/
app.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
// Get DOM elements
const inputValue = document.querySelector('#text-input');
const btnEncrypt = document.querySelector('#encrypt');
const btnDecrypt = document.querySelector('#decrypt');
const textContainer = document.querySelector('#output-container');
const outputHeader = document.querySelector('#output-header');
const outputText = document.querySelector('#output-text');
const outputBkg = document.querySelector('#output-bkg');
const outputActions = document.querySelector('#output-action');
// Encryption & Decryption keys
const encryptKeys = {
'a': 'ai',
'e': 'enter',
'i': 'imes',
'o': 'ober',
'u': 'ufat'
};
const decryptKeys = {
'ai': 'a',
'enter': 'e',
'imes': 'i',
'ober': 'o',
'ufat': 'u'
};
// Matching RegEx
const encryptExp = /[aeiou]/gi;
const decryptExp = /(ai|enter|imes|ober|ufat)/gi;
// Setup vars
const alertWarning = 'Aviso';
const copiedText = 'Texto copiado al porta papeles';
const copyTextError = 'Ocurrio un error al ejecutar la funcion appendCopyTextBtn';
const alertIconColor = '#0A3871';
let encryptedText;
let encryptError = 'Por favor ingresa el mensaje que deseas encriptar';
let decryptError = 'Por favor ingresa el mensaje que deseas desencriptar';
/////////*Functions*/////////////
// Encrypt function
function encryptText() {
let inputText = inputValue.value.toLowerCase();
encryptedText = inputText.replace(encryptExp, keys => encryptKeys[keys]);
}
// Decrypt function
function decryptText() {
let inputText = inputValue.value.toLowerCase();
encryptedText = inputText.replace(decryptExp, keys => decryptKeys[keys]);
}
// Hide output message function
function hideOutputMessage() {
outputHeader.classList.toggle('no-show');
outputText.classList.toggle('no-show');
outputBkg.classList.toggle('no-show');
}
// Append encrypted text function
function appendEncryptedText() {
const encrypted = document.createElement('p');
encrypted.setAttribute('id', 'output-encrypted');
encrypted.classList.toggle('output-encrypted');
encrypted.textContent = `${encryptedText}`;
textContainer.appendChild(encrypted);
}
// Append reset button
function appendResetTextBtn() {
const resetBtn = document.createElement('button');
resetBtn.setAttribute('id', 'reset-btn');
resetBtn.classList.toggle('submit-action__white');
resetBtn.textContent = 'Reiniciar';
outputActions.appendChild(resetBtn);
resetBtn.addEventListener('click', () => {
location.reload();
});
}
// Append copy text button
function appendCopyTextBtn() {
const copyTextBtn = document.createElement('button');
copyTextBtn.setAttribute('id', 'copy-btn');
copyTextBtn.classList.toggle('submit-action__white');
copyTextBtn.textContent = 'Copiar';
outputActions.appendChild(copyTextBtn);
copyTextBtn.addEventListener('click', async () => {
try {
await navigator.clipboard.writeText(encryptedText.trim());
inputAlert(alertWarning,copiedText,'success',alertIconColor,alertIconColor);
setTimeout (() => {
location.reload();
}, 2000);
} catch (error) {
alert(`Error fatal ${copyTextError}`);
}
})
}
// Disable encrypt button
function disableBtn() {
btnEncrypt.disabled = true;
btnDecrypt.disabled = true;
}
// Input alert
function inputAlert(title, text, icon, confirmButtonColor, iconColor) {
Swal.fire({
title,
text,
icon,
confirmButtonColor,
iconColor
});
}
///////////////*Event handlers*///////////////
/// Input check
btnEncrypt.addEventListener('click', (event) => {
if (inputValue.value === null || inputValue.value === undefined || inputValue.value.trim() === '') {
inputAlert(alertWarning, encryptError, 'warning', alertIconColor, alertIconColor);
event.stopImmediatePropagation();
}
});
btnDecrypt.addEventListener('click', (event) => {
if (inputValue.value === null || inputValue.value === undefined || inputValue.value.trim() === '') {
inputAlert(alertWarning, decryptError, 'warning', '#0A3871', '#0A3871');
event.stopImmediatePropagation();
}
});
/// Encrypt
btnEncrypt.addEventListener('click', () => {
encryptText();
hideOutputMessage();
appendEncryptedText();
appendResetTextBtn();
disableBtn();
appendCopyTextBtn();
});
/// Decrypt
btnDecrypt.addEventListener('click', () => {
decryptText();
hideOutputMessage();
appendEncryptedText();
appendResetTextBtn();
disableBtn();
appendCopyTextBtn();
});