HanFox

YouTube HD Lite (HanFox fix 2)

Jan 29th, 2012
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name          YouTube HD Lite (HanFox fix 2)
  3. // @namespace     http://userscripts.org/users/78916
  4. // @description   Automatically plays the best quality YouTube video format and removes ads, plus several optional features
  5. // @include       http://www.youtube.com/watch*
  6. // @version       2012-01-30
  7. // ==/UserScript==
  8.  
  9. // Optional Features
  10. var use1080p = true,
  11.     use720p = true,
  12.     wideVideo = true,
  13.     wideSize = "large", // sizes are "large" (width 1280 x height 720 (720p)) or "medium" (width 640 x 480 (480p) [requires wideVideo to be set to true]))
  14.     hideAnnotations = false,
  15.     scrollToVideo = true,
  16.     autoPlay = true,
  17.     hideControls = true;
  18.  
  19.    
  20. // for console debug messages
  21. var debug = false;
  22.  
  23.  
  24. // ensure proper Youtube URL
  25. if ( location.href.search( "watch#!" ) != -1 ) {
  26.     var url = location.href.split( "watch#!" );
  27.     url = url[0] + "watch?" + url[1];
  28.     window.open( url, "_self" );
  29. }
  30.  
  31.  
  32. if ( scrollToVideo ) {
  33.     document.getElementById( "watch-headline-title" ).scrollIntoView( true );
  34. }
  35.  
  36. if ( wideVideo ) {
  37.     // sidebar must be moved down
  38.     var A = document.getElementById( "content" );
  39.     if ( A.className.indexOf( "watch-wide" ) == -1 ) {
  40.         A.className += " watch-wide";
  41.     }
  42.     // video
  43.     A = document.getElementById( "watch-video" );
  44.     if ( A.className.indexOf( "wide" ) == -1 ) {
  45.         A.className += " wide";
  46.     }
  47.     // expand the video container
  48.     if ( A.className.indexOf( wideSize ) == -1 ) {
  49.     A.className += " " + wideSize;
  50.     }
  51. }
  52.  
  53.  
  54. var player = document.getElementById( "movie_player" ),
  55.     myPlayer = player.cloneNode( true ),
  56.     //unsafeWindow.yt.GetConfig( "PLAYER_CONFIG") no longer exists so cannot use it to fetch fmt_list
  57.     //config = unsafeWindow.yt.getConfig( "PLAYER_CONFIG" ),
  58.     flashvars = myPlayer.getAttribute( "flashvars" );
  59.  
  60. if ( debug ) GM_log( "flashvars unmodified: " + flashvars );
  61.  
  62.  
  63. function setFlashvar( field, newVal )
  64. {
  65.     var delimited = "&" + field;
  66.     if ( flashvars.indexOf( delimited ) == -1 ) {
  67.         // field not found, so append it
  68.         flashvars += delimited + "=" + newVal;
  69.     }
  70.     else {
  71.         // modify existing field
  72.         var tmp = flashvars.split( delimited );
  73.         var tmp2 = tmp[1].indexOf( "&" );
  74.         if ( tmp2 != -1 ) {
  75.             flashvars = tmp[0] + delimited + "=" + newVal + tmp[1].substr( tmp2 );
  76.         }
  77.         else {
  78.             flashvars = tmp[0] + delimited + "=" + newVal;
  79.         }
  80.     }
  81. }
  82.    
  83.  
  84. /**********************************************
  85. //     2010.12.22 list of video formats
  86. // fmt=5    240p          vq=small     flv
  87. // fmt=18   240p or 360p  vq=medium    mp4
  88. // fmt=34   240p or 360p  vq=medium    flv
  89. // fmt=35   480p          vq=large     flv
  90. // fmt=22   720p          vq=hd720     mp4
  91. // fmt=37  1080p          vq=hd1080    mp4
  92. ***********************************************/
  93.  
  94. //messy hack to pull the fmt_list out of the flashvars embedded in the flash config in source
  95. var start = flashvars.search("fmt_list=");
  96. var end = flashvars.indexOf("&", start);
  97. var len = end - start;
  98. var fmt_list = flashvars.substr(start, len);
  99.  
  100. if ( debug ) GM_log( " fmt_list: " + fmt_list );
  101.  
  102. // now must set the "vq" var so that the proper format is loaded
  103. // replace all old if fmt_list.searches as structure has changed due to new fetch
  104. //if ( use1080p && ( fmt_list.search( /37(\/[^,]+){4}/ ) != -1 )) {
  105. if ( use1080p && ( fmt_list.search("37%2F") != -1 )) {
  106.     setFlashvar( "vq", "hd1080" );
  107. }
  108. //else if ( use720p && ( fmt_list.search( /22(\/[^,]+){4}/ ) != -1 )) {
  109. else if ( use720p && ( fmt_list.search("22%2F") != -1 )) {
  110.     setFlashvar( "vq", "hd720" );
  111. }
  112. //else if ( fmt_list.search( /35(\/[^,]+){4}/ ) != -1 ) {
  113. else if ( use720p && ( fmt_list.search("35%2F") != -1 )) {
  114.     setFlashvar( "vq", "large" );
  115. }
  116. else {
  117.     setFlashvar( "vq", "medium" );
  118. }
  119.  
  120.  
  121. setFlashvar( "enablejsapi", "1" );
  122.  
  123. if ( autoPlay ) {
  124.     setFlashvar( "autoplay", "1" );
  125. }
  126. else {
  127.     setFlashvar( "autoplay", "0" );
  128. }    
  129.  
  130. setFlashvar( "watermark", "" );
  131.  
  132. setFlashvar( "invideo", "false" );
  133.  
  134. if ( hideAnnotations ) {
  135.     setFlashvar( "iv_load_policy", "3" );
  136. }
  137.  
  138. if ( hideControls ) {
  139.     setFlashvar( "autohide", "1" );
  140. }
  141. else {
  142.     setFlashvar( "autohide", "0" );
  143. }
  144.  
  145. if ( debug ) GM_log( "flashvars final: " + flashvars );
  146.  
  147. myPlayer.setAttribute( "flashvars", flashvars );
  148.  
  149. player.parentNode.replaceChild( myPlayer, player );
Advertisement
Add Comment
Please, Sign In to add comment