-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsupport.js
More file actions
105 lines (105 loc) · 3.63 KB
/
support.js
File metadata and controls
105 lines (105 loc) · 3.63 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
function openchatbox() {
document.getElementById("chat_button").style.display = "none";
document.getElementById("chatbox").style.display = "flex";
chatstart();
}
function closechatbox() {
document.getElementById("chatbox").style.display = 'none';
document.getElementById("chat_button").style.display = 'flex';
}
function chatstart() {
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("get", "http://51.15.59.130:46260/start", true);
xmlhttp.send(null);
xmlhttp.onreadystatechange = processchatstart;
}
function processchatstart() {
if (this.readyState == 4) {
if (this.status == 200) {
getsupportinfo();
}
else {
window.alert("Error " + xmlhttp.statusText);
}
}
}
function getsupportinfo() {
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("get", "http://51.15.59.130:46260/support", true);
xmlhttp.send(null);
xmlhttp.onreadystatechange = processsupportinfo;
}
function processsupportinfo() {
if (this.readyState == 4) {
if (this.status == 200) {
jsonData = JSON.parse(this.responseText);
document.getElementById("supportname").innerHTML = jsonData.support.first +" "+ jsonData.support.last;
document.getElementById("supportimg").src = jsonData.support.picture;
setInterval(receivemsg(), 500);
}
else {
window.alert("Error " + this.statusText);
}
}
}
function submitmsg(){
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("post", "http://51.15.59.130:46260/send", true);
xmlhttp.setRequestHeader('Content-Type', 'application/json');
var msg = document.getElementById("chatinput").value;
document.getElementById("chatinput").value = null;
xmlhttp.send("{\"message\":\"" + escape(msg) + "\"}");
createmessage(true, msg, new Date( new Date().getTime() ) );
}
var msgs = [];
function receivemsg(){
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("get", "http://51.15.59.130:46260/fetch", true);
xmlhttp.send(null);
xmlhttp.onreadystatechange = function(){
if (this.readyState == 4) {
if (this.status == 200) {
if (this.responseText === null)
return;
else
var msg = JSON.parse(this.responseText);
msgs.push(msg.responses[0].message);
var mydate = new Date(msg.responses[0].date);
createmessage(false, msg.responses[0].message, mydate);
}
else {
window.alert("Error " + this.statusText);
}
}
};
}
function createmessage(sender, msg, mydate){
var newmsgdiv = document.createElement("div");
var msgdiv = document.createElement("div");
var userphoto = document.createElement("img");
var msgdate = document.createElement("div");
userphoto.id = "supportimg";
newmsgdiv.style.display = "flex";
newmsgdiv.style.flexdirection = "row-reverse";
newmsgdiv.style.borderTop = "1em";
msgdiv.innerHTML = msg;
msgdiv.className = "chatmessage";
if (sender === true)
{
userphoto.src = "supportprofilepic.png";
newmsgdiv.appendChild(userphoto);
newmsgdiv.appendChild(msgdiv);
}
else
{
userphoto.src = jsonData.support.picture;
newmsgdiv.appendChild(msgdiv);
newmsgdiv.appendChild(userphoto);
}
msgdate.innerHTML = mydate.toLocaleString();
msgdate.style.fontSize = "0.8em";
msgdate.style.textAlign = "right";
msgdiv.appendChild(msgdate);
var parent = document.getElementById("chatarea");
parent.appendChild(newmsgdiv);
}