Mousewheel and keyboard control video HTML5

Add mousewheel & keyboard control to html5 video player.

  1. // ==UserScript==
  2. // @name Mousewheel and keyboard control video HTML5
  3. // @name:en Mousewheel and keyboard control video HTML5
  4. // @namespace http://tampermonkey.net/
  5. // @version 0.2
  6. // @description Add mousewheel & keyboard control to html5 video player.
  7. // @description:en Add mousewheel & keyboard control to html5 video player.
  8. // @author HoangNam
  9. // @match *://*/*
  10. // @grant none
  11. // @license MIT2
  12. // @downloadURL
  13. // @updateURL
  14. // ==/UserScript==
  15.  
  16.  
  17.  
  18. // Thời gian tua tới/lùi (5 giây)
  19. const seekTime = 2;
  20.  
  21. function handleWheel(event, seekTime) {
  22. // Kiểm tra xem sự kiện có phải là sự kiện wheel hay không
  23. if (event.type === 'wheel') {
  24. // Lấy phần tử video hoặc audio đang được trỏ chuột
  25. const isOverMedia = document.querySelector('video, audio');
  26. const isOverPlayer = document.querySelector('div.player');
  27.  
  28.  
  29. // Kiểm tra xem có phải ứng dụng Facebook không
  30. //const isFacebookApp = window.location.hostname.includes('facebook.com');
  31. // Kiểm tra xem có phải ứng dụng dailymotion không
  32. // const isYoutubeApp = window.location.hostname.includes('youtube.com');
  33. // Kiểm tra xem con trỏ chuột có trên video hoặc audio hay không
  34. // if ((!isOverMedia || !isOverMedia.contains(event.target)) && !isFacebookApp && !isYoutubeApp)
  35. // Tua tới/lùi 5 giây
  36. if (event.deltaY > 0) {
  37. // Tua tới
  38. isOverMedia.currentTime = Math.max(0, isOverMedia.currentTime - seekTime);
  39. } else {
  40. // Tua lùi
  41. isOverMedia.currentTime = Math.min(isOverMedia.duration, isOverMedia.currentTime + seekTime);
  42. }
  43.  
  44. // Kiểm tra xem có phải đang cuộn trên các phần tử media không
  45. if (isOverPlayer) {
  46. // Nếu đúng thì ngăn chặn hành động cuộn
  47. event.preventDefault(), { passive: true };
  48. }
  49. if (isOverMedia && isOverMedia.contains(event.target))
  50. {
  51.  
  52. // Tắt hành động mặc định của chuột
  53. event.preventDefault();
  54. event.stopPropagation();
  55.  
  56. }
  57. }
  58. }
  59.  
  60.  
  61.  
  62.  
  63. // Thêm event listener cho sự kiện wheel với chế độ passive
  64. document.addEventListener('wheel', (event) => handleWheel(event, seekTime), { passive: false, capture: true });
  65.  
  66. window.addEventListener('load', function() {
  67. document.addEventListener('keydown', function(e) {
  68. console.log(e.keyCode);
  69. var player = document.getElementsByTagName('video')[0];
  70. if (!player) return;
  71.  
  72. switch (e.keyCode) {
  73. case 37:
  74. // Arrow Left lùi 5s
  75. player.currentTime -= e.ctrlKey ? 30 : 5;
  76. break;
  77. case 39:
  78. // Arrow Right tiến 5s
  79. player.currentTime += e.ctrlKey? 30 : 5;
  80. break;
  81. case 32:
  82. // Space tạm dừng
  83. player.paused ? player.play() : player.pause();
  84. break;
  85. }
  86. });
  87. });

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址