main.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. var myObj = 0;
  2. var button = document.getElementById("button");
  3. var result = document.getElementById('result');
  4. var emoji = document.querySelectorAll(".emoji-item");
  5. var canvasIcon = document.querySelector(".draw img");
  6. var canvasBar = document.querySelector(".canvas");
  7. var main = document.querySelector("main");
  8. var loadingFirst = true;
  9. for (var i = 0; i < emoji.length; i++) {
  10. emoji[i].onclick = function(){
  11. var xmlhttp = new XMLHttpRequest();
  12. xmlhttp.open("POST", "http://students.a-level.com.ua:10012");
  13. xmlhttp.onreadystatechange = function(){
  14. if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
  15. console.log(xmlhttp.responseText);
  16. }
  17. }
  18. xmlhttp.send(JSON.stringify({ func: 'addMessage', nick: "p", message: this.getAttribute("src")}))
  19. }
  20. }
  21. var cansvasIsVisible = false;
  22. canvasIcon.onclick = function(){
  23. if(!cansvasIsVisible){
  24. canvasBar.style.visibility = "visible";
  25. cansvasIsVisible = true;
  26. main.style.marginLeft = "39%";
  27. }
  28. else{
  29. canvasBar.style.visibility = "hidden";
  30. cansvasIsVisible = false;
  31. main.style.margin = "0 auto";
  32. }
  33. }
  34. //
  35. button.onclick = function(){
  36. var xmlhttp = new XMLHttpRequest();
  37. var myNick = document.getElementById('input1').value;
  38. var myMessage = document.getElementById('input2').value;
  39. xmlhttp.open("POST", "http://students.a-level.com.ua:10012");
  40. xmlhttp.onreadystatechange = function(){
  41. if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
  42. console.log(xmlhttp.responseText);
  43. }
  44. }
  45. xmlhttp.send(JSON.stringify({ func: 'addMessage', nick: myNick, message: myMessage}))
  46. }
  47. //
  48. setInterval(function(){
  49. var xmlhttp = new XMLHttpRequest();
  50. xmlhttp.open("POST", "http://students.a-level.com.ua:10012");
  51. //xmlhttp.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
  52. xmlhttp.onreadystatechange = function(){
  53. if(xmlhttp.readyState == 3 && loadingFirst){
  54. loadingFirst = false;
  55. for (var i = 0; i < document.querySelectorAll(".loading").length; i++) {
  56. document.querySelectorAll(".loading")[i].remove();
  57. }
  58. debugger;
  59. var loading = document.createElement("img");
  60. loading.className = "loading";
  61. loading.setAttribute("src","img/ajax-loader.gif");
  62. result.appendChild(loading);
  63. }
  64. if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
  65. for (var i = 0; i < document.querySelectorAll(".loading").length; i++) {
  66. document.querySelectorAll(".loading")[i].remove();
  67. }
  68. var obj = JSON.parse(xmlhttp.responseText).data;
  69. var resObj = Object.keys(obj);
  70. myObj += obj.length;
  71. for (var i = 0; i < obj.length; i++) {
  72. if(typeof obj[i].message != "object" && obj[i].message && obj[i].message.indexOf('youtube') > -1){
  73. var iframe = document.createElement("iframe");
  74. var str = "https://www.youtube.com/embed/";
  75. var index1 = obj[i].message.indexOf('=') + 1;
  76. var span = document.createElement("span");
  77. if(obj[i].message.indexOf('&') > -1){
  78. var index2 = obj[i].message.indexOf('&');
  79. str2 = obj[i].message.slice(index1,index2);
  80. }
  81. else{
  82. str2 = obj[i].message.slice(index1);
  83. }
  84. var div = document.createElement("div");
  85. iframe.setAttribute("src",str + str2);
  86. iframe.style.width = "85%";
  87. iframe.style.height = "350px";
  88. span.innerHTML = obj[i].nick;
  89. div.appendChild(span);
  90. div.appendChild(iframe);
  91. result.appendChild(div);
  92. }
  93. else if(typeof obj[i].message != "object" && obj[i].message && /(https?:\/\/.*\.(?:png|jpg))/i.test(obj[i].message)){ // obj[i].message.indexOf('.jpg') > -1
  94. var img = document.createElement("img");
  95. img.setAttribute("src",obj[i].message);
  96. img.setAttribute("style","width: 85%");
  97. var div = document.createElement("div");
  98. var span = document.createElement("span");
  99. span.innerHTML = obj[i].nick;
  100. div.appendChild(span);
  101. div.appendChild(img);
  102. result.appendChild(div);
  103. }
  104. else if(typeof obj[i].message != "object" && obj[i].message && obj[i].message.indexOf('.gif') > -1){
  105. var div = document.createElement("div");
  106. result.innerHTML += "<div><span>" + obj[i].nick + "</span><br><img src='"+obj[i].message+"' alt='' /> </div><br>";
  107. }
  108. else{
  109. result.innerHTML += "<div><span>" + obj[i].nick + "</span><br> "+ obj[i].message + '</div>';
  110. }
  111. }
  112. }
  113. }
  114. xmlhttp.send(JSON.stringify({ func: 'getMessages', messageId: myObj}));
  115. }
  116. ,3000);