topic_plus_load.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. function topic_plus_load(topic_num, num) {
  2. var test = setInterval(function() {
  3. var url = "/api/thread/" + String(topic_num) + "?num=" + num + "&render=1";
  4. var p_data = document.getElementById("plus_topic");
  5. var n_data = '';
  6. var n_num = 1;
  7. var url_2 = "/api/markup";
  8. var xhr = new XMLHttpRequest();
  9. xhr.open("GET", url, true);
  10. xhr.send(null);
  11. var xhr_2 = new XMLHttpRequest();
  12. xhr_2.open("GET", url_2, true);
  13. xhr_2.send(null);
  14. xhr.onreadystatechange = function() {
  15. if(this.readyState === 4 && this.status === 200 && this.responseText !== '{}\n') {
  16. t_data = JSON.parse(this.responseText);
  17. for(key in t_data) {
  18. n_data += t_data[key]['data'];
  19. n_num = key;
  20. }
  21. p_data.innerHTML += n_data;
  22. // https://programmingsummaries.tistory.com/379
  23. var options = {
  24. body: '#' + n_num
  25. }
  26. var notification = new Notification("openNAMU", options);
  27. setTimeout(function () {
  28. notification.close();
  29. }, 5000);
  30. xhr_2.onreadystatechange = function() {
  31. if(xhr_2.readyState === 4 && xhr_2.status === 200) {
  32. markup = JSON.parse(xhr_2.responseText)['markup'];
  33. if(markup === 'markdown') {
  34. render_markdown();
  35. } else {
  36. for(var key in t_data) {
  37. render_html('topic_' + String(key) + '-');
  38. }
  39. }
  40. }
  41. }
  42. topic_plus_load(topic_num, String(Number(num) + 1));
  43. clearInterval(test);
  44. }
  45. }
  46. }, 2000);
  47. }