comments.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. var video_data = JSON.parse(document.getElementById('video_data').textContent);
  2. var spinnerHTML = '<h3 style="text-align:center"><div class="loading"><i class="icon ion-ios-refresh"></i></div></h3>';
  3. var spinnerHTMLwithHR = spinnerHTML + '<hr>';
  4. String.prototype.supplant = function (o) {
  5. return this.replace(/{([^{}]*)}/g, function (a, b) {
  6. var r = o[b];
  7. return typeof r === 'string' || typeof r === 'number' ? r : a;
  8. });
  9. };
  10. function toggle_comments(event) {
  11. var target = event.target;
  12. var body = target.parentNode.parentNode.parentNode.children[1];
  13. if (body.style.display === 'none') {
  14. target.textContent = '[ − ]';
  15. body.style.display = '';
  16. } else {
  17. target.textContent = '[ + ]';
  18. body.style.display = 'none';
  19. }
  20. }
  21. function hide_youtube_replies(event) {
  22. var target = event.target;
  23. var sub_text = target.getAttribute('data-inner-text');
  24. var inner_text = target.getAttribute('data-sub-text');
  25. var body = target.parentNode.parentNode.children[1];
  26. body.style.display = 'none';
  27. target.textContent = sub_text;
  28. target.onclick = show_youtube_replies;
  29. target.setAttribute('data-inner-text', inner_text);
  30. target.setAttribute('data-sub-text', sub_text);
  31. }
  32. function show_youtube_replies(event) {
  33. var target = event.target;
  34. var sub_text = target.getAttribute('data-inner-text');
  35. var inner_text = target.getAttribute('data-sub-text');
  36. var body = target.parentNode.parentNode.children[1];
  37. body.style.display = '';
  38. target.textContent = sub_text;
  39. target.onclick = hide_youtube_replies;
  40. target.setAttribute('data-inner-text', inner_text);
  41. target.setAttribute('data-sub-text', sub_text);
  42. }
  43. function get_youtube_comments() {
  44. var comments = document.getElementById('comments');
  45. var fallback = comments.innerHTML;
  46. comments.innerHTML = spinnerHTML;
  47. var baseUrl = video_data.base_url || '/api/v1/comments/'+ video_data.id
  48. var url = baseUrl +
  49. '?format=html' +
  50. '&hl=' + video_data.preferences.locale +
  51. '&thin_mode=' + video_data.preferences.thin_mode;
  52. if (video_data.ucid) {
  53. url += '&ucid=' + video_data.ucid
  54. }
  55. var onNon200 = function (xhr) { comments.innerHTML = fallback; };
  56. if (video_data.params.comments[1] === 'youtube')
  57. onNon200 = function (xhr) {};
  58. helpers.xhr('GET', url, {retries: 5, entity_name: 'comments'}, {
  59. on200: function (response) {
  60. var commentInnerHtml = ' \
  61. <div> \
  62. <h3> \
  63. <a href="javascript:void(0)">[ − ]</a> \
  64. {commentsText} \
  65. </h3> \
  66. <b> \
  67. '
  68. if (video_data.support_reddit) {
  69. commentInnerHtml += ' <a href="javascript:void(0)" data-comments="reddit"> \
  70. {redditComments} \
  71. </a> \
  72. '
  73. }
  74. commentInnerHtml += ' </b> \
  75. </div> \
  76. <div>{contentHtml}</div> \
  77. <hr>'
  78. commentInnerHtml = commentInnerHtml.supplant({
  79. contentHtml: response.contentHtml,
  80. redditComments: video_data.reddit_comments_text,
  81. commentsText: video_data.comments_text.supplant({
  82. // toLocaleString correctly splits number with local thousands separator. e.g.:
  83. // '1,234,567.89' for user with English locale
  84. // '1 234 567,89' for user with Russian locale
  85. // '1.234.567,89' for user with Portuguese locale
  86. commentCount: response.commentCount.toLocaleString()
  87. })
  88. });
  89. comments.innerHTML = commentInnerHtml;
  90. comments.children[0].children[0].children[0].onclick = toggle_comments;
  91. if (video_data.support_reddit) {
  92. comments.children[0].children[1].children[0].onclick = swap_comments;
  93. }
  94. },
  95. onNon200: onNon200, // declared above
  96. onError: function (xhr) {
  97. comments.innerHTML = spinnerHTML;
  98. },
  99. onTimeout: function (xhr) {
  100. comments.innerHTML = spinnerHTML;
  101. }
  102. });
  103. }
  104. function get_youtube_replies(target, load_more, load_replies) {
  105. var continuation = target.getAttribute('data-continuation');
  106. var body = target.parentNode.parentNode;
  107. var fallback = body.innerHTML;
  108. body.innerHTML = spinnerHTML;
  109. var baseUrl = video_data.base_url || '/api/v1/comments/'+ video_data.id
  110. var url = baseUrl +
  111. '?format=html' +
  112. '&hl=' + video_data.preferences.locale +
  113. '&thin_mode=' + video_data.preferences.thin_mode +
  114. '&continuation=' + continuation;
  115. if (video_data.ucid) {
  116. url += '&ucid=' + video_data.ucid
  117. }
  118. if (load_replies) url += '&action=action_get_comment_replies';
  119. helpers.xhr('GET', url, {}, {
  120. on200: function (response) {
  121. if (load_more) {
  122. body = body.parentNode.parentNode;
  123. body.removeChild(body.lastElementChild);
  124. body.insertAdjacentHTML('beforeend', response.contentHtml);
  125. } else {
  126. body.removeChild(body.lastElementChild);
  127. var p = document.createElement('p');
  128. var a = document.createElement('a');
  129. p.appendChild(a);
  130. a.href = 'javascript:void(0)';
  131. a.onclick = hide_youtube_replies;
  132. a.setAttribute('data-sub-text', video_data.hide_replies_text);
  133. a.setAttribute('data-inner-text', video_data.show_replies_text);
  134. a.textContent = video_data.hide_replies_text;
  135. var div = document.createElement('div');
  136. div.innerHTML = response.contentHtml;
  137. body.appendChild(p);
  138. body.appendChild(div);
  139. }
  140. },
  141. onNon200: function (xhr) {
  142. body.innerHTML = fallback;
  143. },
  144. onTimeout: function (xhr) {
  145. console.warn('Pulling comments failed');
  146. body.innerHTML = fallback;
  147. }
  148. });
  149. }