Video Player Auto Focus (Generic)

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

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Video Player Auto Focus (Generic)
// @namespace    http://tampermonkey.net/
// @version      0.4
// @description  Sets focus to the first video player found on any page on page load for immediate keyboard control.
// @author       Your Name
// @match        *://*/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    function focusFirstVideoPlayer() {
        // Find the first video element on the page
        const videoElement = document.querySelector('video');

        if (videoElement) {
            // Attempt to focus the video element
            videoElement.focus();
            console.log('Video Auto Focus: Fokus auf das erste <video> Element gesetzt.');
        } else {
            console.log('Video Auto Focus: Kein <video> Element auf der Seite gefunden.');
        }
    }

    // Use a MutationObserver to wait for a video element to be added to the DOM
    const observer = new MutationObserver((mutations, obs) => {
        const videoElement = document.querySelector('video');

        if (videoElement) {
            focusFirstVideoPlayer();
            obs.disconnect(); // Stop observing once an element is found and focused
        }
    });

    // Start observing the body for changes
    observer.observe(document.body, {
        childList: true,
        subtree: true
    });

    // Also try to focus after the window is fully loaded as a fallback
    // This might be useful for pages where the video is present on initial load
    window.addEventListener('load', () => {
        // Give a small delay to ensure elements are fully ready
        setTimeout(focusFirstVideoPlayer, 500);
    });

    // Also try focusing on DOMContentLoaded in case the video is available early
     window.addEventListener('DOMContentLoaded', () => {
        // Give a small delay
        setTimeout(focusFirstVideoPlayer, 100);
    });

})();