YuoTubeQuickDo

hot key: '+','-' adjust speed; 'I' rotate left; 'O' rotate right; 'U' mirror; 'R' reset

  1. // ==UserScript==
  2. // @name YuoTubeQuickDo
  3. // @namespace https://gist.github.com/jeayu
  4. // @version 0.0.1
  5. // @description hot key: '+','-' adjust speed; 'I' rotate left; 'O' rotate right; 'U' mirror; 'R' reset
  6. // @author jeayu
  7. // @license MIT
  8. // @match *://www.youtube.com/watch?*
  9. // ==/UserScript==
  10.  
  11. (function () {
  12. 'use strict';
  13. const q = (query) => document.querySelectorAll(query);
  14. const qI = (query) => document.getElementById(query);
  15. const qT = (query) => document.getElementsByTagName(query);
  16. const YuoTubeQuickDo = {
  17. keyCode: {
  18. 'enter': 13,
  19. 'esc': 27,
  20. '=+': 187,
  21. '-_': 189,
  22. '+': 107,
  23. '-': 109,
  24. '0': 48,
  25. '1': 49,
  26. '2': 50,
  27. '3': 51,
  28. '4': 52,
  29. '5': 53,
  30. '6': 54,
  31. '7': 55,
  32. '8': 56,
  33. '9': 57,
  34. 'a': 65,
  35. 'b': 66,
  36. 'c': 67,
  37. 'd': 68,
  38. 'e': 69,
  39. 'f': 70,
  40. 'g': 71,
  41. 'h': 72,
  42. 'i': 73,
  43. 'j': 74,
  44. 'k': 75,
  45. 'l': 76,
  46. 'm': 77,
  47. 'n': 78,
  48. 'o': 79,
  49. 'p': 80,
  50. 'q': 81,
  51. 'r': 82,
  52. 's': 83,
  53. 't': 84,
  54. 'u': 85,
  55. 'v': 86,
  56. 'w': 87,
  57. 'x': 88,
  58. 'y': 89,
  59. 'z': 90,
  60. },
  61. config: {
  62. quickDo: {
  63. 'addSpeed': '=+',
  64. 'subSpeed': '-_',
  65. 'mirror': 'u',
  66. 'rotateRight': 'o',
  67. 'rotateLeft': 'i',
  68. 'reset': 'r',
  69. },
  70. },
  71. getKeyCode: function (type) {
  72. return this.keyCode[this.config.quickDo[type]];
  73. },
  74. bindKeydown: function () {
  75. document.addEventListener("keydown", e => {
  76. if (q('input:focus, textarea:focus').length <= 0) {
  77. if (!e.ctrlKey && !e.shiftKey && !e.altKey) {
  78. this.keyHandler(e.keyCode);
  79. }
  80. }
  81. }, true);
  82. },
  83. keyHandler: function (keyCode) {
  84. const h5Player = qT('video')[0];
  85. if (keyCode === this.getKeyCode('addSpeed') && h5Player.playbackRate < 4) {
  86. h5Player.playbackRate += 0.25;
  87. } else if (keyCode === this.getKeyCode('subSpeed') && h5Player.playbackRate > 0.5) {
  88. h5Player.playbackRate -= 0.25;
  89. } else if (keyCode === this.getKeyCode('rotateRight')) {
  90. this.h5PlayerRotate(1);
  91. } else if (keyCode === this.getKeyCode('rotateLeft')) {
  92. this.h5PlayerRotate(-1);
  93. } else if (keyCode === this.getKeyCode('mirror')) {
  94. if (this.getTransformCss(h5Player) != 'none') {
  95. this.setH5PlayerRransform('');
  96. } else {
  97. this.setH5PlayerRransform('rotateY(180deg)');
  98. }
  99. } else if (keyCode === this.getKeyCode('reset')) {
  100. h5Player.playbackRate = 1;
  101. this.setH5PlayerRransform('');
  102. }
  103. },
  104. h5PlayerRotate: function (flag) {
  105. const h5Player = qT('video')[0];
  106. const deg = this.rotationDeg(h5Player) + 90 * flag;
  107. let transform = `rotate(${deg}deg)`;
  108. if (deg == 0 || deg == 180 * flag) {
  109. transform += ` scale(1)`;
  110. } else {
  111. transform += ` scale(${h5Player.videoHeight / h5Player.videoWidth})`;
  112. }
  113. this.setH5PlayerRransform(transform);
  114. },
  115. setH5PlayerRransform: function (transform) {
  116. qT('video')[0].style.transform = transform;
  117. },
  118. getTransformCss: function (e) {
  119. return getComputedStyle(e).transform;
  120. },
  121. rotationDeg: function (e) {
  122. const transformCss = this.getTransformCss(e);
  123. let matrix = transformCss.match('matrix\\((.*)\\)');
  124. if (matrix) {
  125. matrix = matrix[1].split(',');
  126. if (matrix) {
  127. const rad = Math.atan2(matrix[1], matrix[0]);
  128. return parseFloat((rad * 180 / Math.PI).toFixed(1));
  129. }
  130. }
  131. return 0;
  132. },
  133. init: function () {
  134. this.bindKeydown();
  135. }
  136. };
  137. YuoTubeQuickDo.init();
  138. })();