topic.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. "use strict";
  2. function opennamu_do_remove_blind_thread() {
  3. const style = document.querySelector('#opennamu_remove_blind');
  4. if(style !== null) {
  5. if(style.innerHTML !== "") {
  6. style.innerHTML = '';
  7. } else {
  8. style.innerHTML = `
  9. .opennamu_comment_blind_js {
  10. display: none;
  11. }
  12. `;
  13. }
  14. }
  15. }
  16. function opennamu_thread_blind() {
  17. let do_true = 0;
  18. for(let for_a = 0; for_a < document.getElementsByClassName("opennamu_blind_button").length; for_a++) {
  19. let id = document.getElementsByClassName("opennamu_blind_button")[for_a].id;
  20. id = id.replace(/^opennamu_blind_/, '');
  21. id = id.split('_');
  22. let checked = document.getElementsByClassName("opennamu_blind_button")[for_a].checked;
  23. if(checked) {
  24. fetch("/thread/" + id[0] + '/comment/' + id[1] + '/blind', { method : 'GET' });
  25. do_true = 1;
  26. }
  27. }
  28. if(do_true === 1) {
  29. history.go(0);
  30. }
  31. }
  32. function opennamu_get_thread_ui(user_id, date, data, code, color = '', blind = '', add_style = '', topic_num = '') {
  33. let color_b, class_b;
  34. if(blind == 'O') {
  35. color_b = data == '' ? 'opennamu_comment_blind' : 'opennamu_comment_blind_admin';
  36. class_b = 'opennamu_comment_blind_js';
  37. } else {
  38. color_b = 'opennamu_comment_blind_not';
  39. class_b = '';
  40. }
  41. let admin_check_box = ''
  42. if(topic_num != '') {
  43. admin_check_box = '<input type="checkbox" class="opennamu_blind_button" id="opennamu_blind_' + topic_num + '_' + code + '">';
  44. }
  45. return `
  46. <span class="` + class_b + `">
  47. <table class="opennamu_comment" style="` + add_style + `">
  48. <tr>
  49. <td class="opennamu_comment_color_` + color + `">
  50. ` + admin_check_box + `
  51. <a href="#thread_shortcut" id="` + code + `">#` + code + `</a>
  52. ` + user_id + `
  53. <span style="float: right;">` + date + `</span>
  54. </td>
  55. </tr>
  56. <tr>
  57. <td class="` + color_b + ` opennamu_comment_data_main" id="thread_` + code + `">
  58. ` + data + `
  59. </td>
  60. </tr>
  61. </table>
  62. <hr class="main_hr">
  63. </span>
  64. `;
  65. }
  66. function opennamu_get_new_thread(topic_num = "", thread_num = "") {
  67. let get_thread = setInterval(function() {
  68. if(!document.getElementById('opennamu_default_thread_render_' + thread_num)) {
  69. opennamu_get_thread(topic_num, "", thread_num);
  70. } else {
  71. opennamu_get_new_thread(topic_num, String(Number(thread_num) + 1));
  72. clearInterval(get_thread);
  73. }
  74. }, 3000);
  75. }
  76. function opennamu_get_thread(topic_num = "", do_type = "", thread_num = "") {
  77. let url, to_obj, color;
  78. if(do_type === "top") {
  79. url = "/api/thread/" + topic_num + "/top";
  80. to_obj = 'opennamu_top_thread';
  81. color = 'red';
  82. } else {
  83. if(thread_num === "") {
  84. url = "/api/thread/" + topic_num;
  85. } else {
  86. url = "/api/thread/" + topic_num + "/" + thread_num + "/" + thread_num;
  87. }
  88. to_obj = 'opennamu_main_thread';
  89. color = 'default';
  90. }
  91. fetch(url).then(function(res) {
  92. return res.json();
  93. }).then(function(data) {
  94. if(data["data"].length !== 0) {
  95. let end_data = '';
  96. let end_render = [];
  97. let lang = data["language"];
  98. data = data["data"];
  99. let first = '';
  100. for(let for_a = 0; for_a < data.length; for_a++) {
  101. if(first === '') {
  102. first = data[for_a]["ip"];
  103. }
  104. let real_color = color;
  105. if(color !== 'red') {
  106. if(data[for_a]["blind"] === '1') {
  107. real_color = 'blue';
  108. } else if(first === data[for_a]["ip"]) {
  109. real_color = 'green';
  110. } else {
  111. real_color = 'default';
  112. }
  113. }
  114. let date = '<a href="/thread/' + topic_num + '/comment/' + data[for_a]["id"] + '/tool">(' + lang["tool"] + ')</a> ' + data[for_a]["date"];
  115. let render_data = data[for_a]["data"] !== "" ? data[for_a]["data"] : "[br]";
  116. end_data += opennamu_get_thread_ui(
  117. data[for_a]["ip_render"],
  118. date,
  119. '<div class="opennamu_comment_scroll" id="opennamu_' + color + '_thread_render_' + data[for_a]["id"] + '">' + opennamu_xss_filter(render_data) + '</div>',
  120. data[for_a]["id"],
  121. real_color,
  122. data[for_a]["blind"],
  123. '',
  124. topic_num
  125. )
  126. end_render.push([
  127. render_data,
  128. data[for_a]["id"]
  129. ]);
  130. }
  131. if(do_type === "" && thread_num === "") {
  132. opennamu_get_new_thread(topic_num, String(Number(data[data.length - 1]["id"]) + 1));
  133. }
  134. document.getElementById(to_obj).innerHTML += end_data;
  135. for(let for_a = 0; for_a < end_render.length; for_a++) {
  136. let observer = new IntersectionObserver(entries => {
  137. entries.forEach(entry => {
  138. if(entry.isIntersecting) {
  139. opennamu_do_render(
  140. 'opennamu_' + color + '_thread_render_' + end_render[for_a][1],
  141. end_render[for_a][0],
  142. '',
  143. 'thread'
  144. );
  145. observer.unobserve(entry.target);
  146. }
  147. });
  148. });
  149. observer.observe(document.getElementById('opennamu_' + color + '_thread_render_' + end_render[for_a][1]));
  150. }
  151. }
  152. });
  153. }