-
Notifications
You must be signed in to change notification settings - Fork 0
/
cipher.js
75 lines (64 loc) · 2.88 KB
/
cipher.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
// Event listeners for Encrypt, Decrypt and Copy buttons
document.getElementById('encrypt').addEventListener('click', function() {
let inputText = document.getElementById('inputText').value;
let lowercaseText = validateAndConvert(inputText);
let encryptedText = encrypt(lowercaseText);
displayOutput(encryptedText);
});
document.getElementById('decrypt').addEventListener('click', function() {
let inputText = document.getElementById('inputText').value;
let lowercaseText = validateAndConvert(inputText);
let decryptedText = decrypt(lowercaseText);
displayOutput(decryptedText);
});
document.getElementById('copyText').addEventListener('click', function() {
let resultText = document.getElementById('resultText').innerText;
copyToClipboard(resultText);
});
document.getElementById('logoReset').addEventListener('click', function() {
// clear the content of the textarea
document.getElementById('inputText').value = '';
// reload the page
location.reload();
});
// Validate and convert input to lowercase
function validateAndConvert(text) {
let warningMessage = document.getElementById('warningMessage');
if (text !== text.toLowerCase()) {
warningMessage.innerText = "only lowercase letters are allowed, your input has been converted to lowercase :)";
warningMessage.style.display = 'block'; // Ensure the message is visible
} else {
warningMessage.innerText = ""; // Clear the message if no uppercase is found
warningMessage.style.display = 'none'; // Hide the message if unnecessary
}
return text.toLowerCase();
}
// Encrypt function using regex with callback
function encrypt(text) {
const encryptions = { 'e': 'enter', 'i': 'imes', 'a': 'ai', 'o': 'ober', 'u': 'ufat' };
return text.replace(/[eioua]/g, match => encryptions[match]);
}
// Decrypt function using regex with callback
function decrypt(text) {
const decryptions = { 'enter': 'e', 'imes': 'i', 'ai': 'a', 'ober': 'o', 'ufat': 'u' };
return text.replace(/enter|imes|ai|ober|ufat/g, match => decryptions[match]);
}
// Update the UI to show the result of the encryption or decryption
function displayOutput(text) {
let defaultMessage = document.getElementById('defaultMessage');
let resultContainer = document.getElementById('resultContainer');
let resultTextDiv = document.getElementById('resultText');
// Hide the default message and show the result container
defaultMessage.style.display = 'none';
resultTextDiv.innerText = text;
resultContainer.style.display = 'flex';
}
// Copy the result text to the clipboard when the user clicks the "copy" button
function copyToClipboard(text) {
const textArea = document.createElement('textarea');
textArea.value = text;
document.body.appendChild(textArea);
textArea.select();
document.execCommand('copy');
document.body.removeChild(textArea);
}