-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
119 lines (105 loc) · 3.68 KB
/
script.js
File metadata and controls
119 lines (105 loc) · 3.68 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
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
const encodingFunctions = {
'base64': {
encode: (text) => btoa(text),
decode: (text) => atob(text)
},
'caesar': {
encode: (text) => caesarCipher(text),
decode: (text) => caesarCipher(text, true)
},
'reverse': {
encode: (text) => reverseAlphabet(text),
decode: (text) => reverseAlphabet(text)
}
};
function encodeText() {
const plainText = document.getElementById('plainTextEncoder').value;
const encodingScheme = document.getElementById('encodingSchemeEncoder').value;
try {
const encodedText = encodingFunctions[encodingScheme].encode(plainText);
document.getElementById('encodedTextEncoder').value = encodedText;
return;
} catch (error) {
console.error('Error encoding text:', error.message);
showErrorModal(error.message);
}
}
function decodeText() {
const encodedText = document.getElementById('encodedTextDecoder').value;
const encodingScheme = document.getElementById('encodingSchemeDecoder').value;
try {
const decodedText = encodingFunctions[encodingScheme].decode(encodedText);
document.getElementById('decodedTextDecoder').value = decodedText;
return;
} catch (error) {
console.error('Error decoding text:', error.message);
showErrorModal(error.message);
}
}
function clearInputs() {
document.getElementById('plainTextEncoder').value = '';
document.getElementById('encodedTextEncoder').value = '';
document.getElementById('encodedTextDecoder').value = '';
document.getElementById('decodedTextDecoder').value = '';
}
function caesarCipher(text, decrypt = false, shift = 3) {
const alphabet = 'abcdefghijklmnopqrstuvwxyz';
let result = '';
for (let i = 0; i < text.length; i++) {
let char = text[i].toLowerCase();
let index = alphabet.indexOf(char);
if (index === -1) {
if (/[a-zA-Z]/.test(text[i])) {
result += char;
} else {
result += text[i];
}
} else {
index = (decrypt ? index - shift : index + shift + 26) % 26;
result += text[i] === text[i].toUpperCase() ? alphabet[index].toUpperCase() : alphabet[index];
}
}
return result;
}
function reverseAlphabet(text) {
const alphabet = 'abcdefghijklmnopqrstuvwxyz';
const reversedAlphabet = alphabet.split('').reverse().join('');
let result = '';
for (let i = 0; i < text.length; i++) {
let char = text[i].toLowerCase();
let index = alphabet.indexOf(char);
if (index === -1) {
if (/[a-zA-Z]/.test(text[i])) {
result += char;
} else {
result += text[i];
}
} else {
result += text[i] === text[i].toUpperCase() ? reversedAlphabet[index].toUpperCase() : reversedAlphabet[index];
}
}
return result;
}
function showErrorModal(message) {
const modal = document.getElementById('errorModal');
const errorMessage = document.getElementById('errorMessage');
errorMessage.textContent = message;
modal.style.display = 'block';
const closeBtn = document.getElementsByClassName('close')[0];
closeBtn.onclick = function() {
modal.style.display = 'none';
};
window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = 'none';
}
};
}
function toggleInfoSection() {
const infoSection = document.getElementById('infoSection');
if (infoSection.style.display === 'none' || infoSection.style.display === '') {
infoSection.style.display = 'block';
return;
}
infoSection.style.display = 'none';
}