Greasy Fork 还支持 简体中文。

Youtube 720p

A simple as possible script to automatically change video quality to 720p (or higher if you want).

  1. // ==UserScript==
  2. // @name Youtube 720p
  3. // @namespace sevanteri
  4. // @description A simple as possible script to automatically change video quality to 720p (or higher if you want).
  5. // @version 1.1
  6. // @grant none
  7. // @include *.youtube.com*
  8. // ==/UserScript==
  9. // Author: https://keybase.io/sevanteri
  10. // Date: 2015-07-15
  11. // License: GNU General Public License v3 (GPL)
  12.  
  13. // contentEval (http://wiki.greasespot.net/Content_Script_Injection)
  14. (function (source) {
  15. // Check for function input.
  16. if ('function' == typeof source) {
  17. // Execute this function with no arguments, by adding parentheses.
  18. // One set around the function, required for valid syntax, and a
  19. // second empty set calls the surrounded function.
  20. source = '(' + source + ')();'
  21. }
  22. // Create a script node holding this source code.
  23.  
  24. var script = document.createElement('script');
  25. script.setAttribute('type', 'application/javascript');
  26. script.textContent = source;
  27. // Insert the script node into the page, so it will run, and immediately
  28. // remove it to clean up.
  29. document.body.appendChild(script);
  30. document.body.removeChild(script);
  31. }) (function () {
  32. // wanted quality
  33. var quality = 'hd720';
  34. // get player id
  35. var yt = window.ytplayer;
  36. if (!yt) return;
  37. var yt = yt.config.attrs.id;
  38. var w = window;
  39. var d = document;
  40. var t = null;
  41. // player element
  42. var p = null;
  43. var origReady = w.onYouTubePlayerReady || function () {};
  44.  
  45. var setQ = function (q) {
  46. if (p.getPlaybackQuality() != q) {
  47. p.setPlaybackQuality(q);
  48. }
  49. }
  50.  
  51. w.onYouTubePlayerReady = function () {
  52. p = d.getElementById(yt);
  53. if (p) {
  54. p.addEventListener('onStateChange', function (e) {
  55. //console.log(e);
  56. // When unstarted (-1) and buffering (3).
  57. // Unstarted was sent only for the first video, so buffering
  58. // state seemed like a good choice for other videos in the
  59. // playlist.
  60. if (e == - 1 || e == 3) {
  61. clearTimeout(t);
  62. setQ(quality);
  63. }
  64. });
  65. }
  66. origReady();
  67. };
  68. t = setTimeout(function () {
  69. p = d.getElementById(yt);
  70. setQ(quality);
  71. }, 200);
  72. });