Skip to content

Instantly share code, notes, and snippets.

@julfers
Last active January 17, 2022 04:35
Show Gist options
  • Save julfers/05f3631b34e8790c3b466c8c14a1108c to your computer and use it in GitHub Desktop.
Save julfers/05f3631b34e8790c3b466c8c14a1108c to your computer and use it in GitHub Desktop.
Force video pause on space
/*
Pressing space key should pause videos, but many streaming sites fail to
implement that correctly, so attempt to fix it. Highly experimental.
Copyright 2022 Josiah Ulfers
This work is free. You can redistribute it and/or modify it under the
terms of the Do What The Fuck You Want To Public License, Version 2,
as published by Sam Hocevar. See http://www.wtfpl.net/ for more details.
*/
// ==UserScript==
// @name Force video pause on space
// @namespace http://josiahulfers.com/force-video-pause
// @version 2
// @description Forces spacebar to pause and play videos in fullscreen
// @updateURL https://gist.githubusercontent.com/julfers/05f3631b34e8790c3b466c8c14a1108c/raw/force-video-pause.user.js
// ==/UserScript==
function blockFullscreenKey(key, fn) {
return function (keyEvent) {
if (keyEvent.key != ' ') return
let fse = document.fullscreenElement,
vid = fse && fse.querySelector('video')
if (vid) {
keyEvent.stopImmediatePropagation()
keyEvent.preventDefault()
fn && fn(vid)
}
}
}
function togglePause(video) {
if (video.paused) video.play()
else video.pause()
}
window.addEventListener('keydown', blockFullscreenKey(' '), true)
window.addEventListener('keypress', blockFullscreenKey(' '), true)
window.addEventListener('keyup', blockFullscreenKey(' ', togglePause), true)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment