Video Seek Forward Backward 3 Seconds

Use ctrl + \ and ctrl + ] to seek video forward and backward 3 seconds. To seek forward use ctrl + \. To seek backward use ctrl + ]. You can modify the key and the seek time to your desire easily in the script.

  1. // ==UserScript==
  2. // @name Video Seek Forward Backward 3 Seconds
  3. // @namespace https://github.com/Dadangdut33/Video-Seek-Forward-Backward-3-Seconds
  4. // @version 1.1
  5. // @description Use ctrl + \ and ctrl + ] to seek video forward and backward 3 seconds. To seek forward use ctrl + \. To seek backward use ctrl + ]. You can modify the key and the seek time to your desire easily in the script.
  6. // @author Dadangdut33
  7. // @license Unlicense
  8. // @include *
  9. // @run-at document-load
  10. // ==/UserScript==
  11.  
  12. (async function () {
  13. ("use strict");
  14.  
  15. // * use https://keycode.info/ to find the keycode
  16. // * Default key is:
  17. // * ctrl + ] to seek backward
  18. // * ctrl + \ to seek forward
  19.  
  20. let foundVideo = false,
  21. tries = 0,
  22. videos,
  23. checkLimit = 10, // Set the number of tries to find the video element
  24. seekBackwardFor = 3, // Set the time to seek backward in seconds
  25. seekForwardFor = 3, // Set the time to seek forward in seconds
  26. seekBackwardKeyCode = 221, // Set the key code to seek backward (ctrl + seekBackwardKeyCode)
  27. seekForwardKeyCode = 220; // Set the key code to seek forward (ctrl + seekForwardKeyCode)
  28.  
  29. // Check for video element
  30. let checkInterval = setInterval(function () {
  31. if (foundVideo) {
  32. clearInterval(checkInterval);
  33. return;
  34. }
  35.  
  36. console.log(`[${new Date()}] Checking for video element`);
  37. videos = document.getElementsByTagName("video");
  38.  
  39. if (videos.length > 0) {
  40. foundVideo = true;
  41.  
  42. // Store the video element in an array
  43. const videoItem = [];
  44. for (let i = 0; i < videos.length; i++) {
  45. videoItem.push(videos.item(i));
  46. }
  47.  
  48. // Loop through the video element array
  49. videoItem.forEach(function (video) {
  50. // add event listener to the window with key event that binds to the video
  51. window.addEventListener("keydown", function (event) {
  52. if (event.ctrlKey && event.keyCode === seekBackwardKeyCode) {
  53. video.currentTime -= seekBackwardFor; // time to seek backward
  54. }
  55.  
  56. if (event.ctrlKey && event.keyCode === seekForwardKeyCode) {
  57. video.currentTime += seekForwardFor; // time to seek forward
  58. }
  59. });
  60. });
  61.  
  62. return; // stop if found
  63. }
  64.  
  65. tries++;
  66.  
  67. if (tries < checkLimit) {
  68. console.log(`[${new Date()}] No video found, trying again in 5 seconds`);
  69. } else {
  70. console.log(`[${new Date()}] No video found, giving up`);
  71. clearInterval(checkInterval);
  72. }
  73. }, 5000);
  74. })();