视频长按D多倍速

YoutTube等视频网站长按2倍速

  1. // ==UserScript==
  2. // @name 视频长按D多倍速
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.4
  5. // @description YoutTube等视频网站长按2倍速
  6. // @author Aoki
  7. // @match *://www.youtube.com/*
  8. // @match *://cn.pornhub.com/*
  9. // @icon https://www.google.com/s2/favicons?sz=64&domain=youtube.com
  10. // @grant none
  11. // @license MIT
  12. // ==/UserScript==
  13.  
  14.  
  15. var isDKeyPressed = false;
  16. var videoDom;
  17.  
  18. async function Web() {
  19. try {
  20. var hostname = window.location.hostname;
  21.  
  22. if (hostname === 'www.youtube.com') {
  23. let YoutubeVideo = document.querySelectorAll(".video-stream, .html5-main-video")[0];
  24. videoDom = YoutubeVideo;
  25. } else if (hostname === 'cn.pornhub.com') {
  26. let Pornhub = document.querySelector('.mgp_videoWrapper');
  27. let PornhubVideo = Pornhub.getElementsByTagName("video")[0];
  28. videoDom = PornhubVideo;
  29. }
  30.  
  31. if (videoDom) {
  32. return;
  33. } else {
  34. throw new Error('Failed to get videoDom');
  35. }
  36. } catch (error) {
  37. console.error(error);
  38. throw error;
  39. }
  40. }
  41.  
  42. function handleKeyDown(event) {
  43. if (event.keyCode === 68 || event.keyCode === 100) {
  44. if (!isDKeyPressed) {
  45. videoDom.playbackRate = 3.5;
  46. isDKeyPressed = true;
  47. }
  48. }
  49. }
  50.  
  51. function handleKeyUp(event) {
  52. if (event.keyCode === 68 || event.keyCode === 100) {
  53. videoDom.playbackRate = 1;
  54. isDKeyPressed = false;
  55. }
  56. }
  57.  
  58. function removeEventListeners() {
  59. document.removeEventListener('keydown', handleKeyDown);
  60. document.removeEventListener('keyup', handleKeyUp);
  61. document.removeEventListener('click', Web);
  62. }
  63.  
  64. window.onload = function() {
  65. (async () => {
  66. try {
  67. await Web();
  68. document.addEventListener('keydown', handleKeyDown);
  69. document.addEventListener('keyup', handleKeyUp);
  70. } catch (error) {
  71. console.error(error);
  72. }
  73. })();
  74. window.addEventListener('beforeunload', removeEventListeners);
  75. };