Skip to content

Instantly share code, notes, and snippets.

@HPZ07
Last active May 26, 2024 08:39
Show Gist options
  • Save HPZ07/a21cefbe93086588d90810a5e1f0ac07 to your computer and use it in GitHub Desktop.
Save HPZ07/a21cefbe93086588d90810a5e1f0ac07 to your computer and use it in GitHub Desktop.
Fix YouTube's Alt-Tab Pause/Play Issue
// ==UserScript==
// @name Fix YouTube's Alt-Tab Pause/Play Issue
// @namespace http://tampermonkey.net/
// @version 0.4
// @description Fix YouTube's Alt-Tab Pause/Play Issue
// @author HPZ07
// @match https://www.youtube.com/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=youtube.com
// @grant none
// ==/UserScript==
(function() {
'use strict';
window.addEventListener('keyup', keyHandler);
window.addEventListener('keydown', keyDownHandler);
const nonTriggerableElements = ['search', 'contenteditable-root', 'gsfi ytd-searchbox', 'ytd-searchbox', 'style-scope yt-formatted-string'];
let video = document.querySelector('video');
let videoState;
function canTriggerSpaceAction() {
return !nonTriggerableElements.some(element => {
return document.activeElement.matches(`.${element}`) || document.activeElement.id === element;
});
}
function keyHandler(e) {
if (!canTriggerSpaceAction() || e.code !== 'Space' || !isWatchPage) return;
video = document.querySelector('video');
if (videoState === "play") video.play();;
if (videoState === "pause") video.pause();
}
function keyDownHandler(e) {
if(!isWatchPage) return;
videoState = video.paused ? "play" : "pause";
keyHandler(e);
}
function isWatchPage() {
return /^\/watch/.test(location.pathname);
}
})();
@ask-compu
Copy link

it worked before but it stopped working, tampermonkey doesn't load it on youtube watch pages anymore for some reason despite it being enabled

@HPZ07
Copy link
Author

HPZ07 commented Nov 20, 2023

@ask-compu fixed

@ask-compu
Copy link

@ask-compu fixed

i found the problem, version 0.2 didn't fix it
the fix was to change the @match line to

// @match        *://*.youtube.com/watch*

@HPZ07
Copy link
Author

HPZ07 commented Nov 21, 2023

Thanks! I've updated the @match as suggested.

the fix was to change the @match line to

// @match        *://*.youtube.com/watch*

@GarlyleTM
Copy link

I needed this script so much. Not sure what changed on YouTube, but this started to annoy me to no end.

@ask-compu
Copy link

Thanks! I've updated the @match as suggested.

the fix was to change the @match line to

// @match        *://*.youtube.com/watch*

nevermind, tampermonkey is again not loading the script on youtube watch pages

@ask-compu
Copy link

weird, if i change it to

// @match        *://*.youtube.com/watc*

it suddenly starts working again

@HPZ07
Copy link
Author

HPZ07 commented Nov 30, 2023

nevermind, tampermonkey is again not loading the script on youtube watch pages

It works on my end simply by refreshing the watch page.

@HPZ07
Copy link
Author

HPZ07 commented Nov 30, 2023

0.4 should fix the problem(hopefully). Using a wildcard character on YouTube (e.g., watch*) may not be ideal due to YouTube's asynchronous content loading.

function isWatchPage() {
     return /^\/watch/.test(location.pathname);
}

@VelizarBG
Copy link

If you open a video in a new tab and the first thing you do is Alt+Tab, it won't register the first space but will after that.

@bucarest
Copy link

The space bar doesn't work correctly on embedded videos. You should exclude them with this line:

// @exclude https://www.youtube.com/embed/*

@csaben
Copy link

csaben commented Mar 19, 2024

Awesome script! Thanks @HPZ07 !

@Orlean54
Copy link

Big Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment