-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
150 lines (134 loc) · 3.56 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
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
const messagebox = document.querySelector(".message-box");
const encrypt_mssge = document.querySelector(".encrypted_message");
const encryptKey = document.querySelector(".encryptKey input");
const decryptKey = document.querySelector(".decryptKey input");
const phone1 = document.querySelector(".chats__phone1");
const phone2 = document.querySelector(".chats__phone2");
const phone3 = document.querySelector(".chats__phone3");
const b1=document.querySelector(".send-button");
var encryptionKey=0
var decryptionKey=0
let messageText = [];
var meste=[]
encryptKey.addEventListener('click', function() {
encryptionKey= encryptKey.valueAsNumber;
});
encryptKey.addEventListener('keyup', function() {
encryptionKey = encryptKey.valueAsNumber;
});
decryptKey.addEventListener('click', function() {
decryptionKey = decryptKey.valueAsNumber;
});
decryptKey.addEventListener('keyup', function() {
decryptionKey = decryptKey.valueAsNumber;
});
const bobSend = mssge => {
// console.log(mssge)
let BobCht = document.createElement("div");
let BobMssg = document.createElement("div");
BobCht.classList.add("Bob", "chat", "chat-sent");
BobMssg.classList.add("Bob", "message-sent");
BobMssg.innerText = mssge;
BobCht.appendChild(BobMssg);
phone1.appendChild(BobCht);
}
const hckrReceive = mssge => {
let text = '';
mssge.map(letter => {
text += letter;
});
let hckrCht = document.createElement("div");
let hckrMssg = document.createElement("div");
hckrCht.classList.add("Bob", "chat", "chat-received");
hckrMssg.classList.add("Bob", "message-received");
hckrMssg.innerText = text;
hckrCht.appendChild(hckrMssg);
phone2.appendChild(hckrCht);
// console.log(text);
}
const AshlyReceive = mssge => {
let AshCht = document.createElement("div");
let AshMssg = document.createElement("div");
AshCht.classList.add("Ashley","chat", "chat-received")
AshMssg.classList.add("Ashley", "message-received");
AshMssg.innerText = mssge;
AshCht.appendChild(AshMssg);
phone3.appendChild(AshCht);
}
function encrypt (messge = []) {
let encryptedText = [];
var l=messge.length;
for(let i=0;i<l;i++)
{
var ch=""
ch+=messge[i]
console.log(ch)
console.log(ch.charCodeAt(0))
let chr=String.fromCharCode(ch.charCodeAt(0)+encryptionKey)
console.log(ch.charCodeAt(0)+encryptionKey)
encryptedText.push(chr);
ch=""
}
return encryptedText;
}
function decrypt(messages = [], unlockKey){
let chr = "";
var l=messages.length;
for(let i=0;i<l;i++)
{
var ch=""
ch+=messages[i]
chr+=String.fromCharCode(ch.charCodeAt(0)-unlockKey)
ch=""
}
return chr;
}
const send = (mssge) => {
let enkeys = [];
console.log(mssge)
var s1=""
for(let i=0;i<mssge.length;i++)
{
s1+=mssge[i]
}
messageToSend = encrypt(mssge, encryptionKey);
enkeys = decrypt(messageToSend, decryptionKey);
// console.log(messageToSend)
bobSend(s1);
hckrReceive(messageToSend);
AshlyReceive(enkeys);
messagebox.value = "";
}
var m1;
messagebox.addEventListener('keyup', function() {
m1 = messagebox.value;
console.log(m1)
});
b1.addEventListener('click',function(){
for(let i=0;i<m1.length;i++)
{
meste.push(m1[i])
console.log(meste)
}
send(meste)
messageText=[]
meste=[]
})
messagebox.addEventListener('keydown', function(e) {
if(e.keyCode === 8){
messageText.pop()
}
else if(e.which=== 13){
send(messageText);
messageText = [];
}
else{
if(e.which===16){
messageText.push()
}
else{
messageText.push(e.key);
}
console.log(messageText)
}
});