Advertisement
Kocayine

Untitled

Jul 11th, 2013
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.99 KB | None | 0 0
  1. // ==UserScript==
  2. // @name Youtube Set Default Playback Quality
  3. // @namespace http://userscripts.org/users/332207
  4. // @description Sets the default playback quality for Youtube videos. For example, play at 1080p or highest available quality.
  5. // @include *.youtube.com/*
  6. // @grant none
  7. // @version 1.2
  8. // ==/UserScript==
  9.  
  10. // Content inject so Chrome can access HTML5 player
  11. function contentEval(source) {
  12. if ('function' == typeof source) {
  13. source = '(' + source + ')();'
  14. }
  15. var script = document.createElement('script');
  16. script.setAttribute("type", "application/javascript");
  17. script.textContent = source;
  18. document.body.appendChild(script);
  19. document.body.removeChild(script);
  20. }
  21.  
  22. contentEval(function(){
  23. var YP = new Object();
  24.  
  25. // Quality options available from Youtube API
  26. YP.quality_options = ['highres', 'hd1080', 'hd720', 'large', 'medium', 'small', 'default'];
  27.  
  28. // Playback quality (must be one of the above)
  29. YP.quality = 'hd720';
  30.  
  31. // Number of times to check for player before giving up
  32. YP.max_attempts = 50;
  33.  
  34. // Initialize player, and make sure API is ready
  35. YP.init = function() {
  36. // Get player
  37. if (document.getElementById('movie_player')) {
  38. // Normal video player
  39. this.player = document.getElementById('movie_player');
  40. }
  41. else if (document.getElementById('movie_player-flash')) {
  42. // Channel video player
  43. this.player = document.getElementById('movie_player-flash');
  44. }
  45. else {
  46. return false;
  47. }
  48.  
  49. // Check for HTML5 player
  50. this.html5 = this.player.getElementsByTagName('video').length ? true : false;
  51.  
  52. // Make sure player API is ready
  53. if (typeof this.player.pauseVideo === 'undefined') {
  54. return false;
  55. }
  56.  
  57. // Pause to avoid flicker caused be loading a different quality
  58. this.player.pauseVideo();
  59.  
  60. // In Chrome Flash player, player.setQualityLevel() doesn't seem to work unless video has started playing (or is paused)
  61. // In Firefox HTML5 player, player.getPlayerState() returns -1 even if player is paused
  62. if (!this.html5 && this.player.getPlayerState() < 1) {
  63. return false;
  64. }
  65.  
  66. // Everything is good to go
  67. return true;
  68. };
  69.  
  70. // Set video quality to YP.quality or highest available
  71. YP.setQuality = function() {
  72. // Get available quality levels
  73. var levels = this.player.getAvailableQualityLevels();
  74. // Set playback quality
  75. if (levels.indexOf(this.quality) >= 0) {
  76. this.player.setPlaybackQuality(this.quality);
  77. }
  78. else {
  79. this.player.setPlaybackQuality(levels[0]);
  80. }
  81. // Play video
  82. this.player.playVideo();
  83. }
  84.  
  85. // Start execution
  86. YP.start = function(attempts) {
  87. // Initialize self (look for player)
  88. if (this.init()) {
  89. this.setQuality();
  90. return true;
  91. }
  92. // Give up (page has no player)
  93. if (attempts > this.max_attempts) {
  94. return false;
  95. }
  96. // Try again until initialize sucessful (maybe page still loading)
  97. setTimeout(function() {
  98. YP.start(++attempts);
  99. }, 200);
  100. }
  101.  
  102. // Main
  103. YP.start(0);
  104.  
  105. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement