-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
129 lines (115 loc) · 3.92 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
/*
https://github.com/cesargh/encriptador-de-texto
v1.0 | 2024-07-01
License: None (Public Domain)
*/
const encryptionKeys = [
{ char: 'e', word: 'enter' },
{ char: 'i', word: 'imes' },
{ char: 'a', word: 'ai' },
{ char: 'o', word: 'ober' },
{ char: 'u', word: 'ufat' }
];
const taInput = document.getElementById("textareaInput");
const taOutput = document.getElementById("textareaOutput");
const pgStatusInput = document.getElementById("paragraphStatusInput");
const pgStatusOutput = document.getElementById("paragraphStatusOutput");
const pgStatusBlank = "\u00A0";
let callbackTimeout = null;
function isEmptyText(text) {
return (text.trim().length === 0);
}
function isValidText(text) {
const restriction = /^[a-zñç ]+$/;
return restriction.test(text);;
}
function replaceCharacters(sortForward, inputText) {
const sortedKeys = sortForward ? encryptionKeys : encryptionKeys.slice().reverse();
let outputText = inputText;
sortedKeys.forEach(({ char, word }) => {
const searchKey = sortForward ? char : word;
const replaceKey = sortForward ? word : char;
const searchRule = new RegExp(searchKey, 'g');
outputText = outputText.replace(searchRule, replaceKey);
});
return outputText;
}
function delayCallback(callbackFunction) {
clearTimeout(callbackTimeout);
callbackTimeout = setTimeout(callbackFunction, 500);
}
function synchTextareaOutput(isWriting) {
taOutput.value = "";
taOutput.className = "textarea__default textarea__output__" + (isWriting ? "writing" : "waiting");
pgStatusOutput.innerText = pgStatusBlank;
if (isWriting) {
pgStatusInput.innerText = "Sólo letras minúsculas sin acentos.";
}
}
function onclickButtonEncrypt(sortForward) {
let outputText = "";
try {
const inputText = taInput.value;
if (isEmptyText(inputText)) {
pgStatusInput.innerText = "No hay texto que procesar...";
}
else if (! isValidText(inputText)) {
pgStatusInput.innerText = "Texto inválido. Sólo se aceptan letras minúsculas sin acentos.";
}
else {
outputText = replaceCharacters(sortForward, inputText);
}
}
catch(excep) {
console.log(excep);
pgStatusInput.innerText = `Error al ${sortForward ? "encriptar" : "desencriptar"}. Detalles en la consola.`;
}
if (isEmptyText(outputText)) {
synchTextareaOutput(false);
}
else {
taOutput.value = outputText;
taOutput.className = "textarea__default";
pgStatusOutput.innerText = pgStatusBlank;
pgStatusInput.innerText = `Texto ${sortForward ? "encriptado" : "desencriptado"} con éxito.`;
}
}
async function onclickButtonPaste() {
synchTextareaOutput(false);
try {
const clipText = await navigator.clipboard.readText();
if (typeof clipText === "string") {
if (isEmptyText(clipText)) {
pgStatusInput.innerText = "Nada que pegar...";
}
else {
taInput.value = clipText;
pgStatusInput.innerText = "Texto pegado desde el portapapeles.";
}
}
else {
pgStatusInput.innerText = "El portapapeles no contiene un texto.";
}
}
catch(excep) {
console.log(excep);
pgStatusInput.innerText = "Error al pegar. Detalles en la consola.";
}
}
async function onclickButtonCopy() {
pgStatusInput.innerText = pgStatusBlank;
try {
const outputText = taOutput.value;
if (isEmptyText(outputText)) {
pgStatusOutput.innerText = "Nada que copiar...";
}
else {
await navigator.clipboard.writeText(outputText);
pgStatusOutput.innerText = "Texto copiado al portapapeles.";
}
}
catch(excep) {
console.log(excep);
pgStatusOutput.innerText = "Error al copiar. Detalles en la consola.";
}
}