Advertisement
Chuiby

Resize YT To Window Size (Scrollbar fix rev.)

Jul 30th, 2013
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name            Resize YT To Window Size
  3. // @description     Moves the video to the top of the website and resizes it to the screen size.
  4. // @author          Chris H (Zren / Shade)
  5. // @icon            http://youtube.com/favicon.ico
  6. // @homepageURL     http://userscripts.org/scripts/show/153699
  7. // @downloadURL     http://userscripts.org/scripts/source/153699.user.js
  8. // @updateURL       http://userscripts.org/scripts/source/153699.meta.js
  9. // @namespace       http://xshade.ca
  10. // @version         1.11
  11. // @include         http*://*.youtube.com/watch?*
  12. // @include         http*://youtube.com/watch?*
  13. // ==/UserScript==
  14.  
  15. /*
  16. ** Youtube Layout lookup
  17. **
  18.  
  19. [? - 20 March 2013]
  20. #watch7-video-container (#watch7-playlist-container is injected above this element)
  21.   #watch7-video
  22.     #watch-player ?
  23.       embed#movie_player ?
  24.      
  25. [20 March 2013]
  26.   #watch7-video-container --renamed--> #watch7-container (renamed back then?)
  27.  
  28. #player (#watch7-playlist-container is injected above this element)
  29.   #player-api
  30.     embed#movie_player
  31.   #watch7-creator-bar
  32.  
  33. [2 April 2013]
  34.   #watch7-playlist-container --renamed--> #playlist
  35.  
  36. [3 May 2013]
  37.   .watch-playlist-collapsed and .watch-medium now attach to #player
  38.  
  39. #watch7-container .watch-playlist
  40.   #player .watch-playlist-collapsed .watch-medium (moved)
  41.     #playlist (Moved to inside #player)
  42.       #watch7-playlist-scrollfloater
  43.         #watch7-playlist-bar
  44.       #watch7-playlist-data
  45.         #watch7-playlist-bar
  46. #watch7-main-container
  47.   ...
  48.    
  49. */
  50.  
  51. (function () {
  52.     "use strict";
  53.  
  54.     //--- Constants
  55.     var scriptShortName = 'ytwp'; // YT Window Player
  56.     var injectedStyleId = scriptShortName + '-style';
  57.     var scriptBodyClassId = scriptShortName + '-window-player';
  58.     var scriptBodyClassSelector = 'body.' + scriptBodyClassId;
  59.    
  60.     var transitionProperties = ["transition", "-ms-transition", "-moz-transition", "-webkit-transition", "-o-transition"];
  61.    
  62.     //--- Utils
  63.     function isStringType(obj) {
  64.         return typeof obj === 'string';
  65.     }
  66.    
  67.     function isArrayType(obj) {
  68.         return obj instanceof Array;
  69.     }
  70.    
  71.     function isObjectType(obj) {
  72.         return typeof obj === 'object';
  73.     }
  74.    
  75.     function isUndefined(obj) {
  76.         return typeof obj === 'undefined';
  77.     }
  78.    
  79.     function buildCSS(selector, styles) {
  80.         var s = "";
  81.         for (var key in styles) {
  82.             s += "\t" + key + ": " + styles[key] + ";\n";
  83.         }
  84.         return selector + " {\n" + s + "}\n";
  85.     }
  86.    
  87.    
  88.     function appendStyle(selector, k, v) {
  89.         var newStyle;
  90.         if (!isUndefined(k) && !isUndefined(v) && isStringType(k)) { // v can be any type (as we stringify it).
  91.             // appendStyle('#blarg', 'display', 'none');
  92.             var d = {};
  93.             d[k] = v;
  94.             newStyle = buildCSS(selector, d);
  95.         } else if (!isUndefined(k) && isUndefined(v) && isObjectType(k)) {
  96.             // appendStyle('#blarg', {'display': 'none'});
  97.             newStyle = buildCSS(selector, k);
  98.         } else {
  99.             // Invalid Arguments
  100.             console.log('Illegal arguments', selector, k, v);
  101.             return;
  102.         }
  103.        
  104.         var styleElement = document.getElementById(injectedStyleId);
  105.         if (!styleElement) {
  106.             styleElement = document.createElement('style');
  107.             styleElement.type = 'text/css';
  108.             styleElement.id = injectedStyleId;
  109.             document.getElementsByTagName('head')[0].appendChild(styleElement);
  110.         }
  111.         styleElement.appendChild(document.createTextNode(newStyle));
  112.     }
  113.    
  114.     function buildVenderPropertyDict(propertyNames, value) {
  115.         var d = {};
  116.         for (var i in propertyNames)
  117.             d[propertyNames[i]] = value;
  118.         return d;
  119.     }
  120.    
  121.     //---
  122.    
  123.     function moveVideoContainer() {
  124.         //--- Video Container
  125.         var videoContainer = document.getElementById("player-api");
  126.        
  127.         // Make sure YT hasn't changed or on a page without a video player.
  128.         if (!videoContainer) return 0;
  129.        
  130.         // Move the video to the top of page.
  131.         var body = document.body;
  132.         body.insertBefore(videoContainer, body.firstChild);
  133.  
  134.         return 1;
  135.     }
  136.    
  137.     function resizeVideoPlayer() {
  138.         //--- Video Player
  139.        
  140.         //
  141.         var d = buildVenderPropertyDict(transitionProperties, 'left 0s linear, padding-left 0s linear');
  142.        
  143.         appendStyle(scriptBodyClassSelector + ' #player', d);
  144.        
  145.         //
  146.         var d = buildVenderPropertyDict(transitionProperties, 'width 0s linear, left 0s linear');
  147.         appendStyle(scriptBodyClassSelector + ' #player-api', d);
  148.        
  149.         // !important is mainly for simplicity, but is needed to override the !important styling when the Guide is open due to:
  150.         // .sidebar-collapsed #watch7-video, .sidebar-collapsed #watch7-main, .sidebar-collapsed .watch7-playlist { width: 945px!important; }
  151.         appendStyle(
  152.             scriptBodyClassSelector + ' #player-api, ' + scriptBodyClassSelector + ' #movie_player',
  153.             {
  154.                 'width': '100% !important',
  155.                 'height': '100% !important'
  156.             }
  157.         );
  158.        
  159.            
  160.         //--- Sidebar
  161.        
  162.         // Remove the transition delay as you can see it moving on page load.
  163.         var d = buildVenderPropertyDict(transitionProperties, 'margin-top 0s linear, padding-top 0s linear');
  164.        
  165.         // Override sidebar position. It changes according to the video player's size.
  166.         // Small video player state has: margin-top: -390px; which overlaps the video.
  167.         d['margin-top'] = '15px !important'; // Large video player has 15px padding-top.
  168.         appendStyle(scriptBodyClassSelector + ' #watch7-sidebar', d);
  169.        
  170.         //--- Fix Other Possible Style Issues
  171.  
  172.         //--- Whitespace Leftover From Moving The Video
  173.         appendStyle(scriptBodyClassSelector + ' #page.watch', 'padding-top', '0');
  174.        
  175.         //--- Video Manager (When viewing own videos)
  176.         appendStyle('#body-container', 'clear', 'both'); // FF needs this
  177.         appendStyle('#watch7-creator-bar', {
  178.             'position': 'relative', // Needed in order to use z-index
  179.             'z-index': '2', // #guide.zIndex + 1
  180.         });
  181.        
  182.         //--- Playlist Bar
  183.         // Set the playlist bar width when large to the same as when small (so it expands over the sidebar).
  184.         appendStyle(scriptBodyClassSelector + ' #watch7-playlist-data .watch7-playlist-bar', 'width', '945px !important');
  185.         appendStyle(scriptBodyClassSelector + ' #watch7-playlist-bar-toggle-button', 'display', 'inline');
  186.        
  187.         //--- Playlist Bar: Youtube Center Overrides (Temporary)
  188.         // Fix padding on the playlist bar when using 'Center Page'.
  189.         appendStyle(scriptBodyClassSelector + '.ytcenter-site-watch.ytcenter-site-center .watch7-playlist', 'padding-left', '0');
  190.         appendStyle(scriptBodyClassSelector + '.ytcenter-site-watch.ytcenter-site-center.guide-collapsed .watch7-playlist', 'padding-left', '0 !important');
  191.        
  192.         //--- Playlist Bar: Sidebar
  193.         // Override sidebar position at element level. It changes according to the video player's size.
  194.         //#watch7-playlist-tray-container { height: 363px; }
  195.         //#watch7-playlist-tray { border-bottom: 27px solid #1B1B1B; }
  196.         // Needs to be !important as it needs to override when on a non-playlist page (which requires !important).
  197.         appendStyle(scriptBodyClassSelector + ' #watch7-container.watch-playlist #player.watch-playlist-collapsed+#watch7-main-container #watch7-sidebar', 'margin-top', '0px !important');
  198.         appendStyle(scriptBodyClassSelector + ' #watch7-container.watch-playlist #player:not(.watch-playlist-collapsed)+#watch7-main-container #watch7-sidebar', 'margin-top', '390px !important');
  199.         appendStyle(scriptBodyClassSelector + ' #watch7-container.watch-playlist #player.watch-medium:not(.watch-playlist-collapsed)+#watch7-main-container #watch7-sidebar', 'margin-top', '480px !important');
  200.        
  201.         fixScrollbars(); // Added content
  202.        
  203.         return 1;
  204.     }
  205.    
  206.     function addBodyClass() {
  207.         // Insert CSS Into the body so people can style around the effects of this script.
  208.         document.body.classList.add(scriptBodyClassId);
  209.        
  210.         return 1;
  211.     }
  212.    
  213.     function main() {
  214.         oldVerticalPosition = window.pageYOffset;
  215.         moveVideoContainer()
  216.             && resizeVideoPlayer()
  217.             && addBodyClass() // Only add class if found & moved the player.
  218.             && initScrollbarFix(); // Added content
  219.     }
  220.    
  221.     // Added content below (START)
  222.     var oldVerticalPosition;
  223.     function initScrollbarFix() {
  224.         oldVerticalPosition = -1;
  225.         setInterval(fixScrollbars, 1000);
  226.         return 1;
  227.     }
  228.    
  229.     function fixScrollbars() {
  230.         if(oldVerticalPosition != window.pageYOffset) {
  231.             oldVerticalPosition = window.pageYOffset;
  232.             appendStyle('body', 'overflow-x', window.pageYOffset == 0 ? 'hidden' : 'visible');
  233.             //appendStyle('body', 'overflow-y', window.pageYOffset == 0 ? 'hidden' : 'visible'); // Unforunately, it (partially) locks up the vertical scrolling
  234.         }
  235.     }
  236.     // Added content (END)
  237.    
  238.     main();
  239.    
  240. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement