-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscripts.js
132 lines (120 loc) · 3 KB
/
scripts.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
// Obtain HTML Objects
let encryptButton = document.querySelector('#encrypt-button');
let decryptButton = document.querySelector('#decrypt-button');
let userInput = document.querySelector('#user-input');
let resultMessage = document.querySelector('#result-message');
let copyButton = document.querySelector('#copy-button');
let warningMessage = document.querySelectorAll('.warning');
let resultBox = document.querySelector('#result');
//Variable declaration
let input;
const keys = {
'e': 'enter',
'i': 'imes',
'a': 'ai',
'o': 'ober',
'u': 'ufat'
}
// Functions
/**
* Encrypts the input by changing all vowels with the specific key value.
*
* @param {String} input
*/
function encrypt(input) {
let length = input.length;
let result=''
for (i = 0; i < length; i++){
result += Object.keys(keys).includes(input[i]) ? keys[input[i]] : input[i];
}
resultMessage.innerHTML = result;
copyButton.textContent = 'Copy';
}
/**
*Decrypts the input by removing the last elements of the keys, leaving only vowels
*
* @param {String} input
*/
function decrypt(input) {
let result = '';
while (input.length > 0) {
result += input[0];
if (Object.keys(keys).includes(input[0]) &&
input.indexOf(keys[input[0]])==0) {
input = input.slice(keys[input[0]].length-1);
}
input = input.slice(1);
}
resultMessage.innerHTML = result;
copyButton.textContent = 'Copy';
}
/**
* Checks if the input is in lowercase, contains only letters or specific symbols and is not empty
*
* @param {String} input
* @return {Bool} True if valid, else False
*/
function isValid(input) {
if (input.toLowerCase() === input &&
input != '' &&
/[a-z .,:]+/.test(input)) {
changeWarning(true)
return true;
}
changeWarning(false)
return false;
}
/**
* Changes the color of the little warning message if Input is not valid
*
* @param {String} test
*/
function changeWarning(test) {
if (test) {
for (const el of warningMessage) {
el.classList.remove('wrong');
}
}
else {
for (const el of warningMessage) {
el.classList.add('wrong');
}
}
}
/**
* Shows result the first time the user makes a query
*
*/
function showResultOnMobile() {
resultBox.classList.add('visible');
}
/**
* Asyncronous functions that copies the result in the clipboard
*
*/
const copyMessage = async () => {
try {
await navigator.clipboard.writeText(resultMessage.textContent);
copyButton.textContent = 'Copied';
} catch (err) {
copyButton.textContent = 'Error';
}
}
// Asign functions
encryptButton.onclick = () => {
input = userInput.value;
if (isValid(input)) {
encrypt(input);
showResultOnMobile();
}
}
decryptButton.onclick = () => {
input = userInput.value;
if (isValid(input)) {
decrypt(input);
showResultOnMobile();
}
}
copyButton.onclick = () => {
copyMessage()
}