-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchat_messages.html
More file actions
317 lines (282 loc) · 8.95 KB
/
chat_messages.html
File metadata and controls
317 lines (282 loc) · 8.95 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
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body {
margin: 0 auto;
max-width: 800px;
padding: 0 20px;
}
.container {
border: 2px solid #dedede;
background-color: #f1f1f1;
border-radius: 5px;
padding: 10px;
margin: 10px 0;
}
.darker {
border-color: #ccc;
background-color: #ddd;
}
.container::after {
content: "";
clear: both;
display: table;
}
.container img {
float: left;
max-width: 60px;
width: 100%;
margin-right: 20px;
border-radius: 50%;
}
.container img.right {
float: right;
margin-left: 20px;
margin-right:0;
}
.time-right {
float: right;
color: #aaa;
}
.time-left {
float: left;
color: #999;
}
.right{
float: right;
}
.left{
float: left;
}
p{
font-size: 12px;
}
span{
font-size: 10px;
}
/* Set a style for all buttons */
.btn {
border: 2px solid black;
background-color: white;
color: black;
padding: 14px 28px;
font-size: 16px;
cursor: pointer;
width:90%;
align-self:center;
}
.success {
border-color: #4CAF50;
color: green;
}
.success:hover {
background-color: #4CAF50;
color: white;
}
textarea{
width:90%;
align-self:center;
border-radius:5px;
padding:5px;
}
</style>
</head>
<body>
<h2 id="head"></h2>
<div id="chatbox">
<div id="msgbox">
<!-- <div class="container">
<img src="/w3images/bandmember.jpg" alt="Avatar" style="width:100%;">
<p>Hello. How are you today?</p>
<span class="time-right">11:00</span>
</div>
<div class="container darker">
<img src="/w3images/avatar_g2.jpg" alt="Avatar" class="right" style="width:100%;">
<p>Hey! I'm fine. Thanks for asking!</p>
<span class="time-left">11:01</span>
</div>
<div class="container">
<img src="/w3images/bandmember.jpg" alt="Avatar" style="width:100%;">
<p>Sweet! So, what do you wanna do today?</p>
<span class="time-right">11:02</span>
</div>
<div class="container darker">
<img src="/w3images/avatar_g2.jpg" alt="Avatar" style="width:100%;">
<p>Nah, I dunno. Play soccer.. or learn more coding perhaps?</p>
<span class="time-left">11:05</span>
</div> -->
</div>
<div id="typing" style="display: none;">
<textarea id="msg" placeholder="Your Message Here"></textarea>
<button id="submit" class="btn success">Send</button>
</div>
</div>
<p></p>
<script>
// array.includes(2)
window.thisuser = {username:"Ellie",avatar:"/home/theoelia/Pictures/Balloon_by_Matt_Benson.jpg"}
window.otheruser = {username:"Josefina",avatar:"/home/theoelia/Pictures/Flying_Whale_by_Shu_Le.jpg"}
window.msgbox = document.getElementById("msgbox");
window.ids =[];
window.head = document.getElementById("head");
head.innerHTML = "Chats with "+ otheruser['username'];
document.querySelector('#msg').focus();
document.querySelector('#msg').onkeyup = function(e) {
if (e.keyCode === 13) { // enter, return
document.querySelector('#submit').click();
}
};
document.querySelector('#submit').onclick = function(e) {
var query = document.querySelector('#msg').value;
if (query === ""){
alert("Enter a message")
}
else{
sendNewMsg(query,thisuser['username'],otheruser['username']);
document.querySelector('#msg').value = ""
}
}
message();
function receiveNewMsg(){
var url = "https://www.pywe.org/messageapi/Message/?username=Ellie&api_key=faa66b4640ed7482fc21007016db22174fe6d26dd0a0af33d7761af49a5b"
const http = new XMLHttpRequest();
http.open('GET', url);
http.setRequestHeader('Content-type', 'application/json');
// http.setRequestHeader('Authentication','id_token');
http.send();
http.onload = function() {
var resp = http.responseText;
var json_resp = JSON.parse(resp);
var msgs = json_resp['objects']
if (msgs.length > 0){
for (i = 0; i < msgs.length; i++){
if (ids.includes(msgs[i]['message_id'])){
}
else{
var msg = msgs[i]['content']
var msg_id = msgs[i]['message_id']
var time = msgs[i]['date_sent']
var username = msgs[i]['from_user']
if (username === thisuser['username']){
var user = thisuser;
}
else{
var user = otheruser;
}
// var avatar = "w3images/avatar_g2.jpg"
msgbox.innerHTML += constmsg(msg,time,user);
ids.push(msg_id);
window.scrollTo(0,document.querySelector("#chatbox").scrollHeight);
}
}
}
}
}
function sendNewMsg(msg,t_user,o_user){
// Adding message to the box
var d = new Date(); // for now
// d.getSeconds();
var pos = "time-right";
var color = "container"
var right;
var avatar_alt = "You";
var msg_align =' class="left"';
var avatar = thisuser['avatar'];
var time = d.getHours() + ":" + d.getMinutes();
msgbox.innerHTML += `<div class="`+color+`">
<img src="`+avatar+`" alt="`+avatar_alt+`"`+right+` style="width:100%;">
<p`+msg_align+`>`+msg+`</p>
<span class="`+pos+`">`+time+`</span>
</div>`
var container = msgbox;
window.scrollTo(0,document.querySelector("#chatbox").scrollHeight);
const obj = {content:msg,
from_user:t_user,
to_user:o_user
}
var url = "https://www.pywe.org/messageapi/Message/?username=Ellie&api_key=faa66b4640ed7482fc21007016db22174fe6d26dd0a0af33d7761af49a5b"
const http = new XMLHttpRequest();
http.open('POST', url);
http.setRequestHeader('Content-type', 'application/json');
// http.setRequestHeader('Authentication','id_token');
http.send(JSON.stringify(obj));
http.onload = function() {
var resp = http.responseText;
var json_resp = JSON.parse(resp);
console.log(resp)
msg_id = json_resp['message_id']
// add sent message id to the ids
if (ids.includes(msg_id)){
}else{
ids.push(msg_id)
}
console.log(ids)
}
}
function constmsg(msg,time,user){
if (user === thisuser){
var pos = "time-right";
var color = "container"
var right;
var avatar_alt = "You";
var msg_align =' class="left"';
}
else {
var pos = "time-left";
var color = "container darker";
var right = ' class="right"';
var avatar_alt = user['username'];
var msg_align = ' class="right"'
}
var avatar = user['avatar']
return `<div class="`+color+`">
<img src="`+avatar+`" alt="`+avatar_alt+`"`+right+` style="width:100%;">
<p`+msg_align+`>`+msg+`</p>
<span class="`+pos+`">`+time+`</span>
</div>`
}
function message(){
var state = false;
var url = "https://www.pywe.org/messageapi/Message/?username=Ellie&api_key=faa66b4640ed7482fc21007016db22174fe6d26dd0a0af33d7761af49a5b"
// console.log("called signUp function...")
const http = new XMLHttpRequest();
http.open('GET', url);
http.setRequestHeader('Content-type', 'application/json');
// http.setRequestHeader('Authentication','id_token');
// http.send(JSON.stringify(info));
http.send() // Make sure to stringify
http.onload = function() {
state = true;
var resp = http.responseText;
var json_resp = JSON.parse(resp);
// console.log(json_resp)
// You can now use the response for what you want
msgs = json_resp['objects']
console.log(msgs)
if (msgs.length > 0){
for (i = 0; i < msgs.length; i++){
var msg = msgs[i]['content']
var msg_id = msgs[i]['message_id']
var time = msgs[i]['date_sent']
var username = msgs[i]['from_user']
if (username === thisuser['username']){
var user = thisuser;
}c
else{
var user = otheruser;
}
// var avatar = "w3images/avatar_g2.jpg"
msgbox.innerHTML += constmsg(msg,time,user);
ids.push(msg_id);
}
}
var typing = document.getElementById("typing").style.display = "block";
window.scrollTo(0,document.querySelector("#chatbox").scrollHeight);
setInterval(receiveNewMsg,2000);
}
}
</script>
</body>
</html>