Skip to content

Instantly share code, notes, and snippets.

@HPZ07
Last active April 11, 2024 19:37
Show Gist options
  • Star 16 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save HPZ07/26ae9f23bec793144f26d9deb6d932a5 to your computer and use it in GitHub Desktop.
Save HPZ07/26ae9f23bec793144f26d9deb6d932a5 to your computer and use it in GitHub Desktop.
Disable YouTube Spacebar Scrolling
// ==UserScript==
// @name Disable YouTube spacebar scrolling
// @namespace http://tampermonkey.net/
// @version 0.2
// @description Disables spacebar scrolling and forces it to pause the video instead
// @author HPZ07
// @match https://www.youtube.com/*
// @grant none
// ==/UserScript==
(function() {
'use strict';
window.addEventListener('keydown', function(e) {
if (e.keyCode === 32 && e.target === document.body && isWatchPage()) {
e.preventDefault();
var video = document.querySelector('video.html5-main-video');
if (video.currentTime > 0 && !video.paused && !video.ended) {
video.play();
} else {
video.pause();
}
}
});
function isWatchPage() {
return /^\/watch/.test(location.pathname);
}
})();
@HPZ07
Copy link
Author

HPZ07 commented May 7, 2023

To use this script, you need to have Tampermonkey or a similar user script manager installed in your browser. Once installed, simply copy and paste the code into a new user script and save it. The script should automatically run on YouTube pages and disable the spacebar scrolling behavior.

Note that this script may not work if YouTube changes its code in the future. If that happens, you may need to update the script accordingly..

@DTriplexx
Copy link

Thanks!

@thismoon
Copy link

thismoon commented May 7, 2023

Thank you it works!
maybe it should get disabled in the frontpage/channel pages since there's no video to pause there

@HPZ07
Copy link
Author

HPZ07 commented May 7, 2023

Thank you it works! maybe it should get disabled in the frontpage/channel pages since there's no video to pause there

It's great to know that it's working! Thanks for the suggestion.

@HPZ07
Copy link
Author

HPZ07 commented May 8, 2023

@thismoon
Copy link

thismoon commented May 10, 2023

looks like the bug got fixed (for me)
and having the userscript enabled makes pressing the spacebar pauses and continues the video quickly
maybe you should comment the whole thing? or make a small notification to tell the users to disable the userscript?

@alexwh
Copy link

alexwh commented Oct 28, 2023

doesn't seem to work anymore after alt tabbing away and back in

you can also remove the isWatchPage check by changing the match line to // @match https://www.youtube.com/watch*

@ask-compu
Copy link

To use this script, you need to have Tampermonkey or a similar user script manager installed in your browser. Once installed, simply copy and paste the code into a new user script and save it. The script should automatically run on YouTube pages and disable the spacebar scrolling behavior.

Note that this script may not work if YouTube changes its code in the future. If that happens, you may need to update the script accordingly..

just click the raw button on here with tampermonkey installed and it should have a tampermonkey popup with an install button

@f4T1H21
Copy link

f4T1H21 commented Nov 9, 2023

doesn't seem to work anymore after alt tabbing away and back in

Hey @alexwh I face the same problem, it doesn't work after alt tab and come back to youtube. Did you manage to solve this problem?

@davids74
Copy link

Is there a script that would add 'Copy Code' button to Github?

@HPZ07
Copy link
Author

HPZ07 commented Nov 14, 2023

This should resolve the Alt-Tab issue.

Fix YouTube's Alt-Tab Pause/Play Issue

@f4T1H21
@alexwh Thanks for the suggestion.

@FujiwaraChoki
Copy link

AMAZING! 👍

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