topic_plus_load.js 1.9 KB

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