-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
114 lines (105 loc) · 3.67 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
//Guardar datos insgresados de los textArea en constantes
const campo_texto = document.querySelector("#texto-usuario");
const campo_resultado = document.querySelector("#campo-resultado");
//Matriz para encriptar texto
const matriz_claves = [
["e","enter"], //indice 0
["i","imes"], //indice 1
["a","ai"], //indice 2
["o","ober"], //indice 3
["u","ufat"] //indice 4
];
//Verificar solo la entrada de letras minúsculas
campo_texto.addEventListener("input", () =>{
let cadena = campo_texto.value;
patron = /^[a-z," "]*$/;
patron_mayus=/[A-Z]/;
if(patron_mayus.test(cadena) ){
campo_texto.value = cadena.toLowerCase();
}else if(!patron.test(cadena)){
swal.fire({
title:'Solo se permiten letras en minúsculas'
});
cadena = cadena.substring(0,cadena.length-1);
campo_texto.value=cadena;
}
})
//Función para verificar si hay texto introducido y encriptar el texto
function btnEncriptar(){
const textoIngresado = campo_texto.value;
if(textoIngresado.trim() == ""){
swal.fire({
title:"Por favor ingrese un mensaje a encriptar"
});
return;
}else{
const texto = encriptar(campo_texto.value);
campo_resultado.value = texto;
// Limpiar el campo de texto
document.getElementById('texto-usuario').value = '';
//Mostrar resultado y ocultar mensajes iniciales
document.getElementById("mensajes-iniciales").style.display="none";
document.getElementById("resultado").style.display="block";
}
}
//Función para encriptar texto
function encriptar(frase){
for(let i=0;i<matriz_claves.length; i++){
if(frase.includes(matriz_claves[i][0])){
frase = frase.replaceAll(
matriz_claves[i][0],
matriz_claves[i][1]
)
}
}
return frase;
}
//Función para verificar si hay texto introducido y desencriptar texto
function btnDesencriptar(){
const textoIngresado = campo_texto.value;
if(textoIngresado.trim() == ""){
//alert('Por favor ingrese un mensaje a encriptar')
swal.fire({
title:"Por favor ingrese un mensaje a encriptar"
});
return;
}else{
const texto = desencriptar(campo_texto.value);
campo_resultado.value = texto;
// Limpiar el campo de texto
document.getElementById('texto-usuario').value = '';
document.getElementById("mensajes-iniciales").style.display="none";
document.getElementById("resultado").style.display="block";
}
}
//Función para desencriptar
function desencriptar(fraseEncriptada){
for(let i = matriz_claves.length-1; i>=0; i--){
if(fraseEncriptada.includes(matriz_claves[i][1])){
fraseEncriptada = fraseEncriptada.replaceAll(
matriz_claves [i][1],
matriz_claves [i][0]
)
}
}
return fraseEncriptada;
}
//Función para mostrar el resultado y ocultar mensajes iniciales
function mostrarElementos() {
var imagen = document.getElementById("muneco");
var botonCopiar = document.getElementById("btn-copy");
if (campo_texto.value.length = 0) {
imagen.style.display = "block";
botonCopiar.style.display = "block";
} else {
imagen.style.display = "none";
botonCopiar.style.display = "none";
}
}
//Función para copiar los resultados
function copiarAlPortapapeles(){
navigator.clipboard.writeText(campo_resultado.value);
swal.fire({
title:"El texto fue copiado exitosamente😎😎"
});
}