-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencriptador.js
140 lines (125 loc) · 3.8 KB
/
encriptador.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
let textoInicial = "";
let textoFinal = "";
function capturarTexto(){
textoInicial = document.getElementById('encriptador__texto').value;
}
function estaVacio(cadenaDeCaracteres){
aux = cadenaDeCaracteres;
return aux.trim().length === 0;
}
function validarTextoInicial(){
capturarTexto();
if(estaVacio(textoInicial)){
alert("Ingrese el texto que desea encriptar o desencriptar.");
}
}
function encriptarTexto(){
validarTextoInicial();
for (let i = 0; i < textoInicial.length; i++) {
let letra = textoInicial[i];
switch (letra) {
case 'e':
textoFinal += 'enter';
break;
case 'i':
textoFinal += 'imes';
break;
case 'a':
textoFinal += 'ai';
break;
case 'o':
textoFinal += 'ober';
break;
case 'u':
textoFinal += 'ufat';
break;
default:
textoFinal += letra;
}
}
mostrarResultado(textoFinal);
reestablecerValoresIniciales();
}
function desencriptarTexto(){
validarTextoInicial();
textoFinal = "";
let i = 0;
while (i < textoInicial.length) {
if (textoInicial.substr(i, 5) === 'enter') {
textoFinal += 'e';
i += 5;
} else if (textoInicial.substr(i, 4) === 'imes') {
textoFinal += 'i';
i += 4;
} else if (textoInicial.substr(i, 2) === 'ai') {
textoFinal += 'a';
i += 2;
} else if (textoInicial.substr(i, 4) === 'ober') {
textoFinal += 'o';
i += 4;
} else if (textoInicial.substr(i, 4) === 'ufat') {
textoFinal += 'u';
i += 4;
} else {
textoFinal += textoInicial[i];
i++;
}
}
mostrarResultado(textoFinal);
reestablecerValoresIniciales();
}
function mostrarResultado(texto){
document.getElementById('resultado__obtenido').innerText = texto;
ocultarObjetos();
}
function reestablecerValoresIniciales(){
textoFinal = "";
textoInicial = "";
}
function copiarTexto() {
const textArea = document.getElementById('resultado__obtenido');
if (textArea) {
textArea.select(); // Selecciona el texto dentro del textarea
try {
document.execCommand('copy'); // Copia el texto seleccionado al portapapeles
alert('Texto copiado al portapapeles.');
} catch (err) {
alert('No se pudo copiar el texto.');
}
} else {
console.error('No se encontró el textarea.');
}
}
function mostrarObjetos(){
mostrarObjeto("resultado__imagen");
mostrarObjeto("resultado__vacio");
mostrarObjeto("resultado__texto");
ocultarObjeto("resultado__copiar");
ocultarObjeto("resultado__obtenido");
}
function ocultarObjetos(){
ocultarObjeto("resultado__imagen");
ocultarObjeto("resultado__vacio");
ocultarObjeto("resultado__texto");
mostrarObjeto("resultado__copiar");
mostrarObjeto("resultado__obtenido");
}
function mostrarObjeto(id) {
const elemento = document.getElementById(id);
if (elemento) {
elemento.hidden = false;
} else {
console.error(`Elemento con ID ${id} no encontrado.`);
}
}
function ocultarObjeto(id) {
const elemento = document.getElementById(id);
if (elemento) {
elemento.style.display = 'none';
} else {
console.error(`Elemento con ID ${id} no encontrado.`);
}
}
document.getElementById('encriptador__actuar').addEventListener('click', encriptarTexto);
document.getElementById('encriptador__revertir').addEventListener('click', desencriptarTexto);
document.getElementById('resultado__copiar').addEventListener('click', copiarTexto);