Video Player Auto Focus (Generic)

Sets focus to the first video player found on any page on page load for immediate keyboard control.

  1. // ==UserScript==
  2. // @name Video Player Auto Focus (Generic)
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.4
  5. // @description Sets focus to the first video player found on any page on page load for immediate keyboard control.
  6. // @author Your Name
  7. // @match *://*/*
  8. // @grant none
  9. // ==/UserScript==
  10.  
  11. (function() {
  12. 'use strict';
  13.  
  14. function focusFirstVideoPlayer() {
  15. // Find the first video element on the page
  16. const videoElement = document.querySelector('video');
  17.  
  18. if (videoElement) {
  19. // Attempt to focus the video element
  20. videoElement.focus();
  21. console.log('Video Auto Focus: Fokus auf das erste <video> Element gesetzt.');
  22. } else {
  23. console.log('Video Auto Focus: Kein <video> Element auf der Seite gefunden.');
  24. }
  25. }
  26.  
  27. // Use a MutationObserver to wait for a video element to be added to the DOM
  28. const observer = new MutationObserver((mutations, obs) => {
  29. const videoElement = document.querySelector('video');
  30.  
  31. if (videoElement) {
  32. focusFirstVideoPlayer();
  33. obs.disconnect(); // Stop observing once an element is found and focused
  34. }
  35. });
  36.  
  37. // Start observing the body for changes
  38. observer.observe(document.body, {
  39. childList: true,
  40. subtree: true
  41. });
  42.  
  43. // Also try to focus after the window is fully loaded as a fallback
  44. // This might be useful for pages where the video is present on initial load
  45. window.addEventListener('load', () => {
  46. // Give a small delay to ensure elements are fully ready
  47. setTimeout(focusFirstVideoPlayer, 500);
  48. });
  49.  
  50. // Also try focusing on DOMContentLoaded in case the video is available early
  51. window.addEventListener('DOMContentLoaded', () => {
  52. // Give a small delay
  53. setTimeout(focusFirstVideoPlayer, 100);
  54. });
  55.  
  56. })();