Allows using spacebar to play/pause YouTube videos/shorts
当前为
// ==UserScript==
// @name YouTube Spacebar Play/Pause
// @namespace YTspacebarxFIRKx
// @version 1.03
// @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/*
// @homepageURL https://greasyfork.org/it/scripts/493791-youtube-spacebar-play-pause
// @grant none
// ==/UserScript==
let lastVisibilityChangeTime = Date.now(); // Track the time of the last visibility change
let isShowDesktopAction = false; // Flag to track if the visibility change was caused by the "show desktop" action
let scriptEnabled = false; // Flag to control script enabling/disabling
// Event listener for visibility change
document.addEventListener('visibilitychange', function() {
if (document.visibilityState === 'hidden') {
lastVisibilityChangeTime = Date.now(); // Update the time of the last visibility change
isShowDesktopAction = true; // Assume "show desktop" action when the window becomes hidden
} else {
isShowDesktopAction = false; // Reset the flag otherwise
}
});
let cachedMode = "";
let videoClicked = false; // Flag to track if the video was clicked
document.addEventListener("keydown", function onEvent(e) {
e.stopPropagation(); // Stop event propagation
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;
}
// Disable the script initially
disableScript();
// 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') {
event.stopPropagation(); // Stop event propagation
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 (isShowDesktopAction) {
disableScript(); // Disable the script if the visibility change was caused by the "show desktop" action
isShowDesktopAction = false; // Reset the flag
}
});