-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
126 lines (109 loc) Β· 3.51 KB
/
index.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
const regex1 = new RegExp('1', 'g')
const regex0 = new RegExp('0', 'g')
const regexEmoji = new RegExp('π', 'g')
const regexDot = new RegExp('\\.', 'g')
const regexGrin = new RegExp(':grin:','g')
const regexRickroll = new RegExp(/youtu\.{0,1}be.+dQw4w9WgXcQ$/gm)
const rickrollLink = "https://youtu.be/dQw4w9WgXcQ"
function mysterizeString(inputstr)
{
return inputstr.replace(regex1, 'π').replace(regex0, '.')
}
function demysterizeString(inputstr)
{
return inputstr.replace(regexEmoji, '1').replace(regexDot, '0')
}
function encrypt(value)
{
let binaryResult = text2bin(value)
return mysterizeString(binaryResult)
}
function decrypt(value)
{
let binary = demysterizeString(value)
return bin2text(binary)
}
function dec2bin(a) {
return Number(parseInt(a, 10)).toString(2)
}
function bin2dec(a) {
return parseInt(a, 2)
}
function bin2hex(a) {
return Number(parseInt(a, 2)).toString(16)
}
function hex2bin(a) {
return Number(parseInt(a, 16)).toString(2)
}
function buildOctet(a) {
for (var d = 8 - a.length, b = 0; b < d; b += 1)a = "0" + a;
return a
}
function text2bin(a) {
for (var d = "", b = [], c = a.length, b = "", e = 0; e < c; e += 1){
let val = a.charAt(e)
if(val === '\ud83d'){
let nextVal = a.charAt(e + 1)
if(nextVal === '\ude01')
{
val += nextVal
e++
}
}
b = encodeURIComponent(val).split("%"), 2 < b.length ? (b = b[1] + b[2], d += hex2bin(b)) : d += buildOctet(dec2bin(a.charCodeAt(e)));
}
return d
}
function bin2text(a) {
for (var d = a.length >> 3, b = [], c = 0; c < d; c += 1){
b[c] = a.substr(c << 3, 8);
}
a = b.length;
d = "";
for (c = 0; c < a; c += 1){
if ("110" === b[c].substr(0, 3)) {
try {
d += decodeURIComponent("%" + bin2hex(b[c]) + "%" + bin2hex(b[c + 1]))
} catch (e) {
continue
}
c += 1
}
else if(b[c] == "11110000" && b[c + 1] == "10011111"){
d += 'π'
c++
}
else{
d += String.fromCharCode(bin2dec(b[c]));
}
}
return d
};
function checkRickRoll(text) {
if (text.match(regexRickroll))
window.open(rickrollLink,"_blank");
}
document.querySelector("#decryptBtn").addEventListener("click",()=>{
let decryptedText = decrypt(document.querySelector("#textToDecrypt").value);
let resultField = document.querySelector("#textToEncrypt");
if (!resultField.classList.contains("active")) resultField.classList.add("active")
resultField.focus();
checkRickRoll(decryptedText);
resultField.value = decryptedText;
})
document.querySelector("#encryptBtn").addEventListener("click",()=>{
let encryptedText = encrypt(document.querySelector("#textToEncrypt").value);
let resultField = document.querySelector("#textToDecrypt");
if (!resultField.classList.contains("active")) resultField.classList.add("active")
resultField.focus();
resultField.value = encryptedText
})
document.querySelector("#textToDecrypt").addEventListener("input",()=>{
let field = document.querySelector("#textToDecrypt");
field.value = field.value.replace(regexGrin,"π");
})
document.querySelectorAll(".copy-result-btn").forEach(btn => btn.addEventListener("click", (event) => {
let inputId = event.target.getAttribute("textAreaId")
let input = document.querySelector('#' + inputId)
navigator.clipboard.writeText(input.value)
}))