您需要先安装一个扩展,例如 篡改猴、Greasemonkey 或 暴力猴,之后才能安装此脚本。
您需要先安装一个扩展,例如 篡改猴 或 暴力猴,之后才能安装此脚本。
您需要先安装一个扩展,例如 篡改猴 或 暴力猴,之后才能安装此脚本。
您需要先安装一个扩展,例如 篡改猴 或 Userscripts ,之后才能安装此脚本。
您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。
您需要先安装用户脚本管理器扩展后才能安装此脚本。
Allows using spacebar to play/pause YouTube videos/shorts
当前为
// ==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 } });