watch.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. 'use strict';
  2. function toggle_parent(target) {
  3. var body = target.parentNode.parentNode.children[1];
  4. if (body.style.display === 'none') {
  5. target.textContent = '[ − ]';
  6. body.style.display = '';
  7. } else {
  8. target.textContent = '[ + ]';
  9. body.style.display = 'none';
  10. }
  11. }
  12. function swap_comments(event) {
  13. var source = event.target.getAttribute('data-comments');
  14. if (source === 'youtube') {
  15. get_youtube_comments();
  16. } else if (source === 'reddit') {
  17. get_reddit_comments();
  18. }
  19. }
  20. var continue_button = document.getElementById('continue');
  21. if (continue_button) {
  22. continue_button.onclick = continue_autoplay;
  23. }
  24. function next_video() {
  25. var url = new URL('https://example.com/watch?v=' + video_data.next_video);
  26. if (video_data.params.autoplay || video_data.params.continue_autoplay)
  27. url.searchParams.set('autoplay', '1');
  28. if (video_data.params.listen !== video_data.preferences.listen)
  29. url.searchParams.set('listen', video_data.params.listen);
  30. if (video_data.params.speed !== video_data.preferences.speed)
  31. url.searchParams.set('speed', video_data.params.speed);
  32. if (video_data.params.local !== video_data.preferences.local)
  33. url.searchParams.set('local', video_data.params.local);
  34. url.searchParams.set('continue', '1');
  35. location.assign(url.pathname + url.search);
  36. }
  37. function continue_autoplay(event) {
  38. if (event.target.checked) {
  39. player.on('ended', next_video);
  40. } else {
  41. player.off('ended');
  42. }
  43. }
  44. function get_playlist(plid) {
  45. var playlist = document.getElementById('playlist');
  46. playlist.innerHTML = spinnerHTMLwithHR;
  47. var plid_url;
  48. if (plid.startsWith('RD')) {
  49. plid_url = '/api/v1/mixes/' + plid +
  50. '?continuation=' + video_data.id +
  51. '&format=html&hl=' + video_data.preferences.locale;
  52. } else {
  53. plid_url = '/api/v1/playlists/' + plid +
  54. '?index=' + video_data.index +
  55. '&continuation=' + video_data.id +
  56. '&format=html&hl=' + video_data.preferences.locale;
  57. }
  58. if (video_data.params.listen) {
  59. plid_url += '&listen=1'
  60. }
  61. helpers.xhr('GET', plid_url, {retries: 5, entity_name: 'playlist'}, {
  62. on200: function (response) {
  63. playlist.innerHTML = response.playlistHtml;
  64. if (!response.nextVideo) return;
  65. var nextVideo = document.getElementById(response.nextVideo);
  66. nextVideo.parentNode.parentNode.scrollTop = nextVideo.offsetTop;
  67. player.on('ended', function () {
  68. var url = new URL('https://example.com/watch?v=' + response.nextVideo);
  69. url.searchParams.set('list', plid);
  70. if (!plid.startsWith('RD'))
  71. url.searchParams.set('index', response.index);
  72. if (video_data.params.autoplay || video_data.params.continue_autoplay)
  73. url.searchParams.set('autoplay', '1');
  74. if (video_data.params.listen !== video_data.preferences.listen)
  75. url.searchParams.set('listen', video_data.params.listen);
  76. if (video_data.params.speed !== video_data.preferences.speed)
  77. url.searchParams.set('speed', video_data.params.speed);
  78. if (video_data.params.local !== video_data.preferences.local)
  79. url.searchParams.set('local', video_data.params.local);
  80. location.assign(url.pathname + url.search);
  81. });
  82. },
  83. onNon200: function (xhr) {
  84. playlist.innerHTML = '';
  85. document.getElementById('continue').style.display = '';
  86. },
  87. onError: function (xhr) {
  88. playlist.innerHTML = spinnerHTMLwithHR;
  89. },
  90. onTimeout: function (xhr) {
  91. playlist.innerHTML = spinnerHTMLwithHR;
  92. }
  93. });
  94. }
  95. function get_reddit_comments() {
  96. var comments = document.getElementById('comments');
  97. var fallback = comments.innerHTML;
  98. comments.innerHTML = spinnerHTML;
  99. var url = '/api/v1/comments/' + video_data.id +
  100. '?source=reddit&format=html' +
  101. '&hl=' + video_data.preferences.locale;
  102. var onNon200 = function (xhr) { comments.innerHTML = fallback; };
  103. if (video_data.params.comments[1] === 'youtube')
  104. onNon200 = function (xhr) {};
  105. helpers.xhr('GET', url, {retries: 5, entity_name: ''}, {
  106. on200: function (response) {
  107. comments.innerHTML = ' \
  108. <div> \
  109. <h3> \
  110. <a href="javascript:void(0)">[ − ]</a> \
  111. {title} \
  112. </h3> \
  113. <p> \
  114. <b> \
  115. <a href="javascript:void(0)" data-comments="youtube"> \
  116. {youtubeCommentsText} \
  117. </a> \
  118. </b> \
  119. </p> \
  120. <b> \
  121. <a rel="noopener" target="_blank" href="https://reddit.com{permalink}">{redditPermalinkText}</a> \
  122. </b> \
  123. </div> \
  124. <div>{contentHtml}</div> \
  125. <hr>'.supplant({
  126. title: response.title,
  127. youtubeCommentsText: video_data.youtube_comments_text,
  128. redditPermalinkText: video_data.reddit_permalink_text,
  129. permalink: response.permalink,
  130. contentHtml: response.contentHtml
  131. });
  132. comments.children[0].children[0].children[0].onclick = toggle_comments;
  133. comments.children[0].children[1].children[0].onclick = swap_comments;
  134. },
  135. onNon200: onNon200, // declared above
  136. });
  137. }
  138. if (video_data.play_next) {
  139. player.on('ended', function () {
  140. var url = new URL('https://example.com/watch?v=' + video_data.next_video);
  141. if (video_data.params.autoplay || video_data.params.continue_autoplay)
  142. url.searchParams.set('autoplay', '1');
  143. if (video_data.params.listen !== video_data.preferences.listen)
  144. url.searchParams.set('listen', video_data.params.listen);
  145. if (video_data.params.speed !== video_data.preferences.speed)
  146. url.searchParams.set('speed', video_data.params.speed);
  147. if (video_data.params.local !== video_data.preferences.local)
  148. url.searchParams.set('local', video_data.params.local);
  149. url.searchParams.set('continue', '1');
  150. location.assign(url.pathname + url.search);
  151. });
  152. }
  153. addEventListener('load', function (e) {
  154. if (video_data.plid)
  155. get_playlist(video_data.plid);
  156. if (video_data.params.comments[0] === 'youtube') {
  157. get_youtube_comments();
  158. } else if (video_data.params.comments[0] === 'reddit') {
  159. get_reddit_comments();
  160. } else if (video_data.params.comments[1] === 'youtube') {
  161. get_youtube_comments();
  162. } else if (video_data.params.comments[1] === 'reddit') {
  163. get_reddit_comments();
  164. } else {
  165. var comments = document.getElementById('comments');
  166. comments.innerHTML = '';
  167. }
  168. });