🏠 Home 

LeftRightArrowForVideo

Add left/right arrow hotkey to backward/forward 5 seconds for chrome's default video player. For example, videos on twitter or personal site


安装此脚本?
  1. // ==UserScript==
  2. // @name LeftRightArrowForVideo
  3. // @namespace butaixianran
  4. // @version 0.1
  5. // @description Add left/right arrow hotkey to backward/forward 5 seconds for chrome's default video player. For example, videos on twitter or personal site
  6. // @author butaixianran
  7. // @homepage https://github.com/butaixianran/LeftRightArrowForVideo
  8. // @match *://*/*
  9. // @exclude *://*.youtube.com/*
  10. // @exclude *://vimeo.com/*
  11. // @exclude *://*.instagram.com/*
  12. // @exclude *://*.bilibili.com/*
  13. // @exclude *://*.acfun.cn/*
  14. // @grant none
  15. // ==/UserScript==
  16. (function() {
  17. 'use strict';
  18. //Execute when the whole page has loaded
  19. window.onload = function(){
  20. //Bind left and right hotkey
  21. document.onkeydown = function(e) {
  22. //console.log("Add Left and Right hotkey to video tag");
  23. //get all video tags
  24. let videos = document.querySelectorAll('video');
  25. //return if there is no video
  26. if(!videos) {
  27. console.log("can not find video");
  28. return;
  29. }
  30. if(!videos.length) {
  31. console.log("videos.length is 0");
  32. return;
  33. }
  34. //Find first video tag in viewport area.
  35. //For example, there gonna be a lot of videos on one twitter page,
  36. //we only need the first video in viewport area.
  37. //define current video
  38. let currentVideo = null;
  39. //for saving video position and size
  40. let videoRect = null;
  41. //loop on all videos
  42. for (let video of videos) {
  43. //get video position and size
  44. videoRect = video.getBoundingClientRect();
  45. //get video's left-top position and compare it to viewport size
  46. if(videoRect.top >= 0 && videoRect.top <= window.innerHeight) {
  47. //now, this video is the first one in viewport
  48. currentVideo = video;
  49. //don't need other videos
  50. break;
  51. }
  52. }
  53. let currentTime = currentVideo.currentTime;
  54. //console.log("currentTime: " + currentTime);
  55. //check key code
  56. switch(e.which) {
  57. case 37: // left
  58. //console.log("pressed Left");
  59. currentVideo.currentTime = ((currentTime-5)<0)?0:(currentTime-5);
  60. break;
  61. case 39: // right
  62. //console.log("pressed Right");
  63. currentVideo.currentTime = ((currentTime+5)>currentVideo.duration)?currentVideo.duration:(currentTime+5);
  64. break;
  65. default: return; // exit this handler for other keys
  66. }
  67. e.preventDefault(); // prevent the default action (scroll / move caret)
  68. //console.log("new time: " + currentVideo.currentTime);
  69. };
  70. };
  71. })();