-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
53 lines (46 loc) · 2.1 KB
/
main.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
var encodeInput = document.getElementById('inputEncode');
var decodeInput = document.getElementById('inputDecode');
// run once when page loads
if (document.readyState === 'complete') {
init();
} else {
document.addEventListener('DOMContentLoaded', () => init());
}
// run after every additional navigation by swup
swup.on('contentReplaced', init);
function init() {
console.log('init called');
var encodeInput = document.getElementById('inputEncode');
var decodeInput = document.getElementById('inputDecode');
if (decodeInput) {
document.getElementById("output").innerHTML = "Please enter a value."
decodeInput.addEventListener('input', function () {
if (!decodeInput.value) return document.getElementById("output").innerHTML = "Please enter a value."
let result = ''
console.log(decodeInput.value);
const inputArray = decodeInput.value.match(/.{1,2}/g);
inputArray.forEach(chunk => {
if (!!!swappedAlphabet[chunk]) {
document.getElementById("output").innerHTML = `Couldn't find a value for this chunk: "${chunk}"`
return;
}
result += swappedAlphabet[chunk] || chunk
});
document.getElementById("output").innerHTML = result
console.log("result: " + result);
});
} else if (encodeInput) {
document.getElementById("output").innerHTML = "Please enter a value."
encodeInput.addEventListener('input', function () {
if (!encodeInput.value) return document.getElementById("output").innerHTML = "Please enter a value."
let result = ''
let input = encodeInput.value.split('')
input.forEach(letter => {
if (!!!alphabet[letter]) return console.log('Alfabe dosyasında ' + letter + ' harfi bulunamadı. Bunu decodelaman senin için sorun yaratabilir.')
result += alphabet[letter] || letter
})
document.getElementById("output").innerHTML = result
console.log(result)
})
}
}