Delete main.js
Browse files
main.js
DELETED
|
@@ -1,58 +0,0 @@
|
|
| 1 |
-
// Variables
|
| 2 |
-
var messages = document.querySelector('.message-list')
|
| 3 |
-
var btn = document.querySelector('.btn')
|
| 4 |
-
var input = document.querySelector('input')
|
| 5 |
-
|
| 6 |
-
// Button/Enter Key
|
| 7 |
-
btn.addEventListener('click', sendMessage)
|
| 8 |
-
input.addEventListener('keyup', function(e){ if(e.keyCode == 13) sendMessage() })
|
| 9 |
-
|
| 10 |
-
function loadHistory(){
|
| 11 |
-
fetch('/history')
|
| 12 |
-
.then(response => response.json())
|
| 13 |
-
.then(data => {
|
| 14 |
-
for(let i = 0; i < data.length; i++){
|
| 15 |
-
|
| 16 |
-
if(data[i].FROM == 'User') {
|
| 17 |
-
writeLine('<span>User</span><br><br> ${data[i].content}','primary')
|
| 18 |
-
}
|
| 19 |
-
else {
|
| 20 |
-
writeLine('<span>oshinoko</span><br><br> ${data[i].content}','secondary')
|
| 21 |
-
}
|
| 22 |
-
}
|
| 23 |
-
|
| 24 |
-
})
|
| 25 |
-
.catch(error => console.error('Error:', error));
|
| 26 |
-
}
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
loadHistory()
|
| 31 |
-
// Messenger Functions
|
| 32 |
-
function sendMessage(){
|
| 33 |
-
var msg = input.value;
|
| 34 |
-
writeLine(`<span>User</span><br><br> ${msg}`,'primary')
|
| 35 |
-
|
| 36 |
-
input.value = ''
|
| 37 |
-
fetch('/chat', {
|
| 38 |
-
method: 'POST',
|
| 39 |
-
headers: {
|
| 40 |
-
'Content-Type': 'application/json'
|
| 41 |
-
},
|
| 42 |
-
body: JSON.stringify({'message': msg})
|
| 43 |
-
})
|
| 44 |
-
.then(response => response.json())
|
| 45 |
-
.then(data => console.log(data, 'secondary'))
|
| 46 |
-
.catch(error => console.error('Error:', error));
|
| 47 |
-
|
| 48 |
-
}
|
| 49 |
-
function addMessage(msg,typeMessage ='primary'){
|
| 50 |
-
writeLine(`<span>${msg.FROM}</span><br><br> ${msg.MESSAGE}`,typeMessage)
|
| 51 |
-
}
|
| 52 |
-
function writeLine(text,typeMessage){
|
| 53 |
-
var message = document.createElement('li')
|
| 54 |
-
message.classList.add('message-item', 'item-'+typeMessage)
|
| 55 |
-
message.innerHTML = 'Miguel says: ' + text
|
| 56 |
-
messages.appendChild(message)
|
| 57 |
-
messages.scrollTop = messages.scrollHeight;
|
| 58 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|