main.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. async function jsonPost(url, data) {
  2. let resp = await fetch(url, {
  3. method: 'POST',
  4. body: JSON.stringify(data)
  5. })
  6. return await resp.json()
  7. // return new Promise((resolve, reject) => {
  8. // var x = new XMLHttpRequest();
  9. // x.onerror = () => reject(new Error('jsonPost failed'))
  10. // //x.setRequestHeader('Content-Type', 'application/json');
  11. // x.open("POST", url, true);
  12. // x.send(JSON.stringify(data))
  13. // x.onreadystatechange = () => {
  14. // if (x.readyState == XMLHttpRequest.DONE && x.status == 200) {
  15. // resolve(JSON.parse(x.responseText))
  16. // }
  17. // else if (x.status != 200) {
  18. // reject(new Error('status is not 200'))
  19. // }
  20. // }
  21. // })
  22. }
  23. let messageIdSave = 0;
  24. async function getMessages(nextMessageId) {
  25. let youtubeRegExp = /http(?:s?):\/\/(?:www\.)?youtu(?:be\.com\/watch\?v=|\.be\/)([\w\-\_]*)(&(amp;)?‌​[\w\?‌​=]*)?/
  26. let imageRegExp = (/\.(gif|jpg|jpeg|tiff|png)$/i)
  27. let arrObj = await jsonPost("http://students.a-level.com.ua:10012", { func: 'getMessages', messageId: nextMessageId })
  28. arrObj.data.map(t => {
  29. let box = document.createElement('div')
  30. box.classList.add('box')
  31. let nick = document.createElement('span')
  32. nick.classList.add('nick')
  33. let message = document.createElement('p')
  34. message.classList.add('message')
  35. nick.innerText = t.nick + ':'
  36. let keyRegYoutube = t.message ? t.message.match(youtubeRegExp) : null
  37. if (keyRegYoutube) {
  38. message.innerHTML = `<iframe width="500vw" height="300px" src="https://www.youtube.com/embed/${keyRegYoutube[1]}" title="YouTube video player"
  39. frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
  40. allowfullscreen></iframe>`
  41. } else if (imageRegExp.test(t.message)) {
  42. message.innerHTML = `<img src="${t.message}" alt="" width="250px">`
  43. }
  44. else {
  45. message.innerText = t.message
  46. }
  47. box.append(nick)
  48. box.append(message)
  49. historyChat.prepend(box)
  50. })
  51. messageIdSave = arrObj.nextMessageId
  52. }
  53. async function sendMessage(nickName, message) {
  54. await jsonPost("http://students.a-level.com.ua:10012", { func: 'addMessage', nick: nickName, message: message })
  55. }
  56. async function sendAndCheck(nickName, message, nextMessageId) {
  57. await sendMessage(nickName, message)
  58. await getMessages(nextMessageId)
  59. }
  60. btn.onclick = () => {
  61. nick.value === '' ? nick.classList.add('error') : message.value === '' ? message.classList.add('error') : sendAndCheck(nick.value, message.value, messageIdSave)
  62. message.value = ''
  63. }
  64. nick.oninput = () => nick.classList.remove('error')
  65. message.oninput = () => message.classList.remove('error')
  66. async function checkLoop() {
  67. const delay = ms => new Promise(ok => setTimeout(() => ok(ms), ms))
  68. while (true) {
  69. await delay(3000)
  70. getMessages(messageIdSave)
  71. }
  72. }
  73. checkLoop()