YouTube Spacebar Play/Pause

Allows using spacebar to play/pause YouTube videos/shorts

目前为 2024-04-29 提交的版本。查看 最新版本

// ==UserScript==
// @name         YouTube Spacebar Play/Pause
// @namespace    nul
// @version      1.0
// @description  Allows using spacebar to play/pause YouTube videos/shorts
// @author       xFIRKx
// @match        http://*.youtube.com/*
// @match        https://*.youtube.com/*
// @exclude      https://*.youtube.com/embed/*
// @grant        none
// ==/UserScript==
let cachedMode = "";
let scriptEnabled = false; // Flag to control script enabling/disabling
let videoClicked = false; // Flag to track if the video was clicked

document.addEventListener("keydown", function onEvent(e) {
    if (!scriptEnabled || e.code !== "Space") return;

    let ae = document.activeElement;
    if (ae.tagName.toLowerCase() == "input" || ae.hasAttribute("contenteditable")) return;
    e.preventDefault();
    e.stopImmediatePropagation();

    if (document.location.hostname == "music.youtube.com") {
        document.querySelector("#play-pause-button").click();
    } else {
        let player = document.querySelector(".html5-video-player");
        if (player.classList.contains("paused-mode")) cachedMode = "paused-mode";
        if (player.classList.contains("playing-mode")) cachedMode = "playing-mode";
        if (player.classList.contains("ended-mode")) cachedMode = "ended-mode";

        setTimeout(() => {
            let player = document.querySelector(".html5-video-player");
            if (player.classList.contains(cachedMode)) {
                document.querySelector("button.ytp-play-button").click();
                cachedMode = "";
            }
        }, 200);
    }
});

// Function to disable the script
function disableScript() {
    scriptEnabled = false;
}

// Function to enable the script
function enableScript() {
    scriptEnabled = true;
}

// Event listener for window focus
window.addEventListener('focus', function() {
    if (!document.querySelector('video:hover')) enableScript(); // Enable the script after alt tabbing and unfocusing
});

// Event listener for window blur
window.addEventListener('blur', function() {
    disableScript(); // Disable the script when the window loses focus
});

// Event listener for clicking on video
document.addEventListener('click', function(event) {
    if (event.target.tagName.toLowerCase() === 'video') {
        videoClicked = true; // Set flag to true when clicking on the video
        disableScript(); // Disable the script after clicking on the video
    }
});

// Event listener for visibility change (switching tabs)
document.addEventListener('visibilitychange', function() {
    if (document.hidden) {
        disableScript(); // Disable the script when switching tabs
    } else if (videoClicked && !document.querySelector('video:hover')) {
        disableScript(); // Disable the script if the video was clicked and is not hovered after switching back to the tab
        videoClicked = false; // Reset the flag after disabling the script
    } else if (videoClicked && document.querySelector('video:hover')) {
        enableScript(); // Enable the script if the video was clicked and is hovered after switching back to the tab
    }
});

QingJ © 2025

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