This repository has been archived by the owner on Apr 13, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
script.js
161 lines (119 loc) · 4.1 KB
/
script.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
151
152
153
154
155
156
157
158
159
160
const APPNAME = "MOTT Chat"
// Get notification access
window.onload = function() {
if (Notification.permission === 'granted') {
console.log("We have notifications!")
} else {
Notification.requestPermission().then(function (result) {
console.log(result);
if (Notification.permission === 'granted') {
var notification = new Notification('MOTT Chat', { body: 'Notifications are now enabled. Prepare to be updated!'});
setTimeout(notification.close.bind(notification), 4000);
}
});
}
}
// Some API-related functions for sending data to the server
const sendMessage = function (name, message) {
(async () => {
const data = {
'name': name,
'message': message
};
console.log(data)
class HTTPError extends Error { };
let response = await fetch('post.php', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify(data)
});
if (!response.ok) {
throw new HTTPError('Fetch error:', response.statusText);
}
let text = await response.text();
})();
}
// Variables for state-saving
let chatCache = document.getElementById('messages').innerHTML;
// Chat updater that is in between DOM and API
const updateChat = function () {
(async () => {
class HTTPError extends Error { }
let response = await fetch('getchat.php', {
method: 'GET',
headers: {
'content-type': 'application/text'
}
});
if (!response.ok) {
throw new HTTPError('Fetch error:', response.statusText);
}
chat = await response.text();
if (chat.replace(/\s/g,'') == chatCache.replace(/\s/g,'')) {
} else {
document.getElementById('messages').innerHTML = chat;
let chatEl = document.createElement('html');
chatEl.innerHTML = chat;
chatEl = chatEl.getElementsByClassName('message');
let theLength = chatEl.length;
lastName = chatEl[theLength - 2].getElementsByTagName('name')[0].innerText;
let lastMessage = chatEl[theLength - 2].getElementsByTagName('message')[0].innerText;
var notification = new Notification(lastName, { body: lastMessage, icon: 'chat.png ' });
setTimeout(notification.close.bind(notification), 5000);
chatCache = chat;
localizeTime();
}
}
)();
}
// The DOM stuff that interacts with the web stuff
setInterval(updateChat, 2500);
const message = function () {
let name = document.getElementById('name').value;
let message = document.getElementById('message').value;
if (message == '') {
return
}
sendMessage(
name,
message
);
document.getElementById('message').value = '';
}
// Other DOM stuff
const localizeTime = function () {
let chatroomEl = document.getElementById('messages');
let messages = chatroomEl.getElementsByClassName('message');
console.log(messages)
let container = document.createElement('div');
for (let i = 0; i < messages.length-1; i++) {
m = messages[i].innerHTML;
let stringTime = m.split('<date>')[1].split('</date>')[0];
let time = new Date(stringTime);
time = convertUTCDateToLocalDate(time).toLocaleString();
console.log(time);
messageEl = document.createElement('div');
messageEl.className = 'message';
messageEl.innerHTML = m.split(stringTime).join(time);
container.appendChild(messageEl)
}
document.getElementById('messages').innerHTML = container.innerHTML;
}
// Copied from https://stackoverflow.com/questions/6525538/convert-utc-date-time-to-local-date-time/18330682#18330682
function convertUTCDateToLocalDate(date) {
var newDate = new Date(date.getTime()+date.getTimezoneOffset()*60*1000);
var offset = date.getTimezoneOffset() / 60;
var hours = date.getHours();
newDate.setHours(hours - offset);
return newDate;
}
// Manage keypresses
const node = document.getElementById('message');
node.addEventListener("keyup", function(event) {
if (event.key === "Enter") {
message();
}
});