Youtube Auto Resolution Selection

Auto selects the resolution (automatic highest resolution is default) for video quality

目前为 2017-05-18 提交的版本,查看 最新版本

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