A Youtube bug disables the space key shortcut, which often happens when you change windows and return to Youtube, often with the ALT + TAB shortcut. This script ensures that the spacebar PAUSE / PLAY shortcut works correctly.
当前为
// ==UserScript==
// @name Youtube Spacebar Pause/Play Fix
// @namespace http://tampermonkey.net/
// @version 1.0
// @description A Youtube bug disables the space key shortcut, which often happens when you change windows and return to Youtube, often with the ALT + TAB shortcut. This script ensures that the spacebar PAUSE / PLAY shortcut works correctly.
// @author waszner and Mimo
// @match https://www.youtube.com/watch*
// @match http://www.youtube.com/watch*
// @match https://youtube.com/watch*
// @match http://youtube.com/watch*
// @icon https://www.google.com/s2/favicons?sz=64&domain=youtube.com
// @grant none
// @license MIT
// ==/UserScript==
(function() {
'use strict';
let spacePressed = false;
let currentTime = 0;
let isVideoPlaying = false;
let wasVideoPlaying = false;
const YoutubeCommentFieldSelector = '#contenteditable-root'; //css selector of youtube comments
const KPressDelay = 200; // how much time will the program wait before pressing K (probably ping dependent)
// Add event listener
document.addEventListener('keydown', pressKWithSpacebar, true);
// Function to press "K" with the Spacebar when the video's play/pause state didn't change
function pressKWithSpacebar(e) {
//preventing playing/pausing the video while typing coments or replies
if (e.target.matches(YoutubeCommentFieldSelector)){
return;
}else{
if (e.key === ' ' && spacePressed === false) {
spacePressed = true;
wasVideoPlaying = isVideoPlaying;
setTimeout(simulateKKeyPress, KPressDelay);
}
}
}
// Listen to the video's play and pause events to update the variables
const video = document.querySelector('video');
//These prevent external play/pause (pressing K, clicking on the video etc) from messing up the program's run
video.addEventListener('play', function() {
isVideoPlaying = true;
if (spacePressed) { //Prevents the "double tap" of spaceBar when the video IS in focus and pressing the space bar did play/pause it
spacePressed = false; // Reset the Spacebar press time if video started playing
}
});
video.addEventListener('pause', function() {
isVideoPlaying = false;
if (spacePressed) { //Prevents the "double tap" of spaceBar when the video IS in focus and pressing the space bar did play/pause it
spacePressed = false; // Reset the Spacebar press time if video paused
}
});
// Function to simulate pressing "K" if the conditions are met
function simulateKKeyPress() {
if (spacePressed && (wasVideoPlaying === isVideoPlaying)) {
// Send an "K" keypress to simulate pressing "K"
const nKeyPress = new KeyboardEvent('keydown', { key: 'K', code: 'KeyK', which: 75, keyCode: 75, charCode: 75});
document.dispatchEvent(nKeyPress);
}
}
})();