Youtube floating player

Youtube floating player.

目前为 2016-08-04 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name Youtube floating player
  3. // @description Youtube floating player.
  4. // @version 2.0
  5. // @author REVerdi
  6. // @namespace https://openuserjs.org/users/REVerdi
  7. // @copyright 2014+, REVerdi (https://openuserjs.org/users/REVerdi)
  8. // @license (CC) Attribution Non-Commercial Share Alike; http://creativecommons.org/licenses/by-nc-sa/3.0/
  9. // Por causa do SPF (Structured Page Fragments), não posso usar // @include http*://www.youtube.com/watch?*
  10. // porque se o 1° link no YouTube não for do tipo acima, esse script nunca será executado.
  11. // @include http*://www.youtube.com/*
  12. // @grant none
  13. // ==/UserScript==
  14.  
  15.  
  16. /*
  17. TESTADO APENAS NO FIREFOX
  18.  
  19. ONLY TESTED ON FIREFOX
  20. */
  21.  
  22.  
  23. /*
  24. O YouTube "quase" exatamente como eu queria!
  25. Eu não sou programador, então não joguem tomates podres em mim :)
  26.  
  27. YouTube "almost" exactly as I wanted!
  28. I'm not a programmer, so don't throw rotten tomatoes at me :)
  29. */
  30.  
  31.  
  32. // Based on the ideia of drhouse (http://userscripts.org/scripts/show/186872)
  33. // and contains source code written by tforbus:
  34. // https://chrome.google.com/webstore/detail/video-pinner/egfhbaheiflmihggjcfmnmchkijkcdpl
  35. // https://github.com/tforbus/youtube-fixed-video-bookmarklet
  36. // http://www.whattheforbus.com/youtube-bookmarklet
  37. // http://www.tristinforbus.com/
  38.  
  39.  
  40. (function(){
  41.  
  42.  
  43. "use strict";
  44.  
  45.  
  46. var _window;
  47. if (typeof unsafeWindow !== undefined){
  48. _window = unsafeWindow;
  49. }
  50. else {
  51. _window = window;
  52. }
  53.  
  54.  
  55. var bodyDivName = 'body';
  56. //var pageDivName = 'page';
  57.  
  58. //var headerDivName = 'yt-masthead';
  59. //var header;
  60. //var headerRect;
  61.  
  62. var placeholderPlayerDivName = 'placeholder-player';
  63. var placeHolderPlayer;
  64.  
  65. var playerDivName = 'player';
  66. var player;
  67. var playerRect;
  68.  
  69. var contentDivName = 'watch7-content';
  70. var content;
  71. var contentRect;
  72.  
  73. var sidebarDivName = 'watch7-sidebar';
  74. var sidebar;
  75. var sidebarRect;
  76.  
  77. var footerDivName = 'footer-container';
  78. var footer;
  79. //var footerRect;
  80.  
  81.  
  82. var isBodyMutationObserverAdded = 0;
  83. var isPageScrollOrResizeListenerAdded = 0;
  84.  
  85.  
  86. //if (document.getElementById('movie_player')) document.getElementById('movie_player').style.border = "thick solid blue";
  87. //if (document.getElementById('player')) document.getElementById('player').style.border = "thick solid red";
  88.  
  89.  
  90. function pageScrollOrResize(evt) {
  91. player = document.getElementById(playerDivName);
  92. placeHolderPlayer = document.getElementById(placeholderPlayerDivName);
  93. if ( placeHolderPlayer ) { //user IS on a video page
  94. placeHolderPlayer.firstElementChild.style.backgroundColor = 'transparent';
  95. //player = document.getElementById(playerDivName);
  96. content = document.getElementById(contentDivName);
  97. sidebar = document.getElementById(sidebarDivName);
  98. footer = document.getElementById(footerDivName);
  99. //footer.style.position = 'fixed'; //fixa o rodapé na base da página
  100. //footer.style.bottom = '0px'; //fix the footer at the base of page
  101. if ( _window.pageYOffset > 0 ) { //scroll > 0
  102. player.style.position = 'fixed';
  103. player.style.top = '60px';
  104. contentRect = content.getBoundingClientRect();
  105. player.style.left = contentRect.left+'px';
  106. //player.style.zIndex = 998;
  107. sidebar.style.position = 'absolute';
  108. //sidebar.style.top = player.clientHeight+'px';
  109. //sidebar.style.zIndex = 999;
  110. if ( /list/.test(document.location) === false ) { //user is NOT playing a playlist
  111. player.style.zIndex = 998;
  112. sidebar.style.zIndex = 999;
  113. }
  114. else { //user IS playing a playlist
  115. player.style.zIndex = 999;
  116. sidebar.style.zIndex = 998;
  117. sidebarRect = sidebar.getBoundingClientRect();
  118. playerRect = player.getBoundingClientRect();
  119. player.style.width = sidebarRect.right - playerRect.left + 'px'; //playlist width
  120. }
  121. //content.style.position = 'relative';
  122. //content.style.top = player.clientHeight + 'px';
  123. //content.style.zIndex = 997;
  124. }
  125. else { //scroll == 0
  126. player.style.position = '';
  127. player.style.top = '';
  128. player.style.left = '';
  129. //player.style.zIndex = '';
  130. sidebar.style.position = '';
  131. //sidebar.style.top = '';
  132. //sidebar.style.zIndex = '';
  133. player.style.zIndex = '';
  134. sidebar.style.zIndex = '';
  135. player.style.width = '';
  136. //content.style.position = '';
  137. //content.style.top = '';
  138. //content.style.zIndex = '';
  139. }
  140. }
  141. else { //user is NOT on a video page
  142. player.style.position = '';
  143. player.style.top = '';
  144. player.style.left = '';
  145. player.style.zIndex = '';
  146. player.style.width = '';
  147. }
  148. }
  149.  
  150.  
  151.  
  152.  
  153. var bodyMutationOberver = new MutationObserver(function(mutations) {
  154. mutations.forEach(function(mutation) {
  155. if ( isPageScrollOrResizeListenerAdded === 0 ) {
  156. document.addEventListener('scroll', pageScrollOrResize, false);
  157. _window.addEventListener('resize', pageScrollOrResize, false);
  158. isPageScrollOrResizeListenerAdded = 1;
  159. }
  160. });
  161. });
  162.  
  163. function addBodyMutationObserver() {
  164. var config = { attributes: true, characterData: true, childList: true };
  165. var target = document.getElementById(bodyDivName); //tem que ser 'body', porque 'page' só funciona quando a 1ª página NÃO for uma de vídeo
  166. if ( target !== null ) { //( target !== null ) ou apenas ( target )
  167. bodyMutationOberver.observe(target, config);
  168. isBodyMutationObserverAdded = 1;
  169. }
  170. }
  171.  
  172.  
  173.  
  174.  
  175. function entryPoint() {
  176. if( isBodyMutationObserverAdded === 0 ) {
  177. addBodyMutationObserver();
  178. }
  179. }
  180.  
  181. entryPoint();
  182.  
  183.  
  184. })();