-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
91 lines (82 loc) · 2.44 KB
/
Copy pathmain.js
File metadata and controls
91 lines (82 loc) · 2.44 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
// Diccionario para encriptar y desencriptar
const encrypt = text => {
return text
.replace(/e/g, "enter")
.replace(/i/g, "imes")
.replace(/a/g, "ai")
.replace(/o/g, "ober")
.replace(/u/g, "ufat")
}
const decrypt = text => {
return text
.replace(/enter/g, "e")
.replace(/imes/g, "i")
.replace(/ai/g, "a")
.replace(/ober/g, "o")
.replace(/ufat/g, "u")
}
// Expresion regular para validar el texto
const pattern = /[A-ZÁÉÍÓÚÜÑáéíóúüñ]/;
function encriptar(e) {
e.preventDefault();
// Metodos para encriptar y mostrar en pantalla
let text = document.getElementById('mensaje').value;
// Validar si el texto cumple con la expresion regular
if (pattern.test(text) == true) {
alterta();
sinResultado();
return;
}
// Validar si hay texto para encriptar
if (text != '') {
text = encrypt(text);
resultado(text);
} else {
sinResultado();
}
}
function desencriptar(e) {
e.preventDefault();
// Metodos para encriptar y mostrar en pantalla
let text = document.getElementById('mensaje').value;
// Validar si el texto cumple con la expresion regular
if (pattern.test(text) == true) {
alterta();
sinResultado();
return;
}
// Validar si hay texto para encriptar
if (text != '') {
text = decrypt(text);
resultado(text);
} else {
sinResultado();
}
}
// Funcion para copiar el mensaje al portapapeles
async function copiarPortapapeles() {
let text = document.getElementById('texto').innerHTML;
try {
await navigator.clipboard.writeText(text);
// mostrar mensaje de copiado
console.log("Texto copiado al portapapeles");
} catch (err) {
// mostrar mensaje de no copiado
console.error("Error al copiar texto al portapapeles:", err);
}
}
function resultado(text) {
document.getElementById('texto').innerHTML = text;
document.getElementById('vacio').classList.add('disable');
document.getElementById('resultado').classList.remove('disable');
}
function sinResultado() {
document.getElementById('vacio').classList.remove('disable');
document.getElementById('resultado').classList.add('disable');
}
function alterta() {
document.getElementById('alertText').classList.remove('disable');
setTimeout(function() {
document.getElementById('alertText').classList.add('disable');
}, 3000);
}