Advertisement
Guest User

asdasdasda

a guest
Aug 20th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.39 KB | None | 0 0
  1. // ==UserScript==
  2. // @name Youtube Comments Sidebar
  3. // @namespace http://ronnyjohn.work
  4. // @version 1.0
  5. // @description Swaps the comments and related videos sections, so you can read the comments while watching the video.
  6. // @author Ronny John
  7. // @exclude http://www.youtube.com/embed/*
  8. // @exclude https://www.youtube.com/embed/*
  9. // @match http://www.youtube.com/*
  10. // @match https://www.youtube.com/*
  11. // @grant GM_addStyle
  12. // ==/UserScript==
  13.  
  14. (function() {
  15. 'use strict';
  16.  
  17. var mutationObserver = window.MutationObserver || window.WebKitMutationObserver;
  18. var watchModeObserver, sidebarAdObserver, page, sidebar, ads, related, relatedParent, relatedWrapper, comments, footer;
  19. var onResizeFunc = function() { setPositions(); setHeights(); console.log('resize'); };
  20. var onScrollFunc = function() { setHeights(); };
  21. var modificationDone = false;
  22.  
  23. if (typeof mutationObserver === 'undefined') return;
  24.  
  25. // a little style adjustment
  26. if (typeof(GM_addStyle) === typeof(Function)) {
  27. GM_addStyle('#watch7-sidebar-contents.sidebar-comments { position: fixed; z-index: 1; } #watch-discussion.sidebar-comments { position: fixed; padding: 0 6px; overflow: hidden; overflow-y: auto; z-index: 2; } #watch-discussion.sidebar-comments .comment-section-renderer-paginator { margin: 0; } #watch7-sidebar { -moz-transition: all 0s !important; -webkit-transition: all 0s !important; -o-transition: all 0s !important; transition: all 0s !important; }');
  28. }
  29.  
  30. initialize();
  31.  
  32. var contentObserver = createContentObserver();
  33. contentObserver.observe(document.getElementById('content'), {childList: true, subtree: true});
  34.  
  35. function initialize() {
  36. page = document.getElementById('page');
  37. sidebar = document.getElementById('watch7-sidebar-contents');
  38. related = document.getElementById('watch-related');
  39. comments = document.getElementById('watch-discussion');
  40. footer = document.getElementById('footer-container');
  41.  
  42. // if modification is already done, stop initialization (can be cached on browser navigate back and forward)
  43. if (related && comments && related.parentNode.parentNode == comments.parentNode) {
  44. return;
  45. }
  46.  
  47. removeListeners();
  48. if (watchModeObserver) watchModeObserver.disconnect();
  49. if (sidebarAdObserver) sidebarAdObserver.disconnect();
  50.  
  51. // skip if no video is shown or playlist is open
  52. if (!sidebar || !comments || !related || document.getElementById('watch-appbar-playlist')) {
  53. return;
  54. }
  55.  
  56. relatedParent = related.parentNode;
  57.  
  58. watchModeObserver = createWatchModeObserver();
  59. watchModeObserver.observe(page, {attributes: true});
  60.  
  61. // if stage mode is active, do not run modifications
  62. if (page.classList.contains('watch-stage-mode')) {
  63. return;
  64. }
  65.  
  66. runModifications();
  67.  
  68. // if ads are inserted in the sidebar, the position of the comments must be updated
  69. ads = document.getElementById('google_companion_ad_div');
  70. if (ads) {
  71. sidebarAdObserver = createSidebarAdObserver();
  72. sidebarAdObserver.observe(ads, {childList: true});
  73. }
  74. }
  75.  
  76. function runModifications() {
  77. // move ads and related
  78. relatedWrapper = document.createElement('div');
  79. relatedWrapper.className = comments.className;
  80. relatedWrapper.appendChild(related);
  81. comments.parentNode.insertBefore(relatedWrapper, comments);
  82.  
  83. // comments node can't be moved, because loading of replies is not working properly then
  84. sidebar.classList.add('sidebar-comments');
  85. comments.className = 'sidebar-comments';
  86.  
  87. window.addEventListener('resize', onResizeFunc);
  88. window.addEventListener('scroll', onScrollFunc);
  89.  
  90. onResizeFunc();
  91. modificationDone = true;
  92. }
  93.  
  94. function revertModifications() {
  95. sidebar.classList.remove('sidebar-comments');
  96. sidebar.style.width = 'auto';
  97. sidebar.style.height = 'auto';
  98. relatedParent.appendChild(related);
  99. comments.className = relatedWrapper.className;
  100. comments.style.width = 'auto';
  101. comments.style.height = 'auto';
  102. relatedWrapper.remove();
  103.  
  104. removeListeners();
  105.  
  106. modificationDone = false;
  107. }
  108.  
  109. function removeListeners() {
  110. window.removeEventListener('resize', onResizeFunc);
  111. window.removeEventListener('scroll', onScrollFunc);
  112. }
  113.  
  114. function setPositions() {
  115. var sidebarParentRect = sidebar.parentNode.getBoundingClientRect();
  116. sidebar.style.top = (sidebarParentRect.top + scrollY) + 'px';
  117. sidebar.style.left = sidebarParentRect.left + 'px';
  118. sidebar.style.width = (sidebarParentRect.right - sidebarParentRect.left) + 'px';
  119.  
  120. var containerRect = relatedParent.getBoundingClientRect();
  121. comments.style.top = containerRect.bottom + 'px';
  122. comments.style.left = containerRect.left + 'px';
  123. comments.style.width = (containerRect.right - containerRect.left) + 'px';
  124. }
  125.  
  126. function setHeights() {
  127. var sidebarRect = sidebar.getBoundingClientRect();
  128. var containerRect = relatedParent.getBoundingClientRect();
  129. var footerRect = footer.getBoundingClientRect();
  130. var bottom = document.documentElement.clientHeight;
  131.  
  132. if (footerRect.top < bottom) {
  133. bottom = footerRect.top;
  134. }
  135.  
  136. var commentsHeight = bottom - containerRect.bottom - 20;
  137. sidebar.style.height = (containerRect.bottom - sidebarRect.top + commentsHeight + 10) + 'px';
  138. comments.style.height = commentsHeight + 'px';
  139. }
  140.  
  141. function createContentObserver() {
  142. return new mutationObserver(function(mutations) {
  143. mutations.forEach(function(mutation) {
  144. if (mutation.addedNodes !== null) {
  145. for (var i=0; i < mutation.addedNodes.length; i++) {
  146. var node = mutation.addedNodes[i];
  147. if (node.id == 'watch7-container' || node.id == 'watch7-main-container') {
  148. initialize();
  149. break;
  150. }
  151. }
  152. }
  153. });
  154. });
  155. }
  156.  
  157. function createWatchModeObserver() {
  158. return new mutationObserver(function(mutations) {
  159. for (var i=0; i<mutations.length; i++) {
  160. if (mutations[i].attributeName === "class") {
  161. if (page.classList.contains('watch-stage-mode') && modificationDone) {
  162. revertModifications();
  163. } else if (!page.classList.contains('watch-stage-mode') && !modificationDone) {
  164. runModifications();
  165. }
  166. }
  167. }
  168. });
  169. }
  170.  
  171. function createSidebarAdObserver() {
  172. return new mutationObserver(function(mutations) {
  173. mutations.forEach(function(mutation) {
  174. if (mutation.addedNodes !== null) {
  175. onResizeFunc();
  176. }
  177. });
  178. });
  179. }
  180. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement