Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@cesalazar
Last active June 6, 2020 23:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cesalazar/692fe1b285c0cf2635b980dfc0d0f20c to your computer and use it in GitHub Desktop.
Save cesalazar/692fe1b285c0cf2635b980dfc0d0f20c to your computer and use it in GitHub Desktop.
Re-enable (some) keyboard controls, after disabling YT defaults using uBlock Origin. This prevents time scrubing when switching tabs using Alt+1-9.
// ==UserScript==
// @name Re-enable YouTube keyboard controls
// @namespace com.cesalazar.ytkbctrl
// @version 0.8
// @description Re-enable (some) keyboard controls, after disabling YT defaults using uBlock Origin.
// @author Carlos E. Salazar
// @match https://www.youtube.com/*
// @grant none
// ==/UserScript==
// Prevent seeking time with the keys 1-9 by adding this line in uBlock Origin, under `My filters`:
// youtube.com##+js(addEventListener-defuser.js, keydown)
(function() {
'use strict'
const simulateClick = (el) => el && el.dispatchEvent(new MouseEvent('click'))
const targetIsInput = ({ target }) => target && target.nodeName === 'INPUT'
const video = document.querySelector('video')
// Specific to the control buttons of the player
const getButton = (name) => document.querySelector(`.ytp-${name}-button`)
const mappings = {
KeyF: getButton('fullscreen'),
KeyI: getButton('miniplayer'),
KeyK: getButton('play'),
KeyM: getButton('mute'),
KeyN: getButton('next'),
KeyP: getButton('prev'),
KeyT: getButton('size'),
Space: getButton('play'),
}
// Little dance to toggle the miniplayer on and off
let isMiniplayerActive = false
const toggleMiniplayerButton = () => {
mappings.KeyI = getButton(
isMiniplayerActive ? 'miniplayer-expand-watch-page' : 'miniplayer'
)
isMiniplayerActive = !isMiniplayerActive
}
document.addEventListener('keypress', (event) => {
if (targetIsInput(event)) return
event.preventDefault()
const { code } = event
if (code === 'KeyI') toggleMiniplayerButton()
return simulateClick(mappings[code])
})
document.addEventListener('keyup', (event) => {
if (targetIsInput(event)) return
event.preventDefault()
switch (event.code) {
case 'ArrowLeft':
video.currentTime -= 1
break
case 'ArrowRight':
video.currentTime += 1
break
case 'KeyH':
video.currentTime -= 10
break
case 'KeyL':
video.currentTime += 10
break
}
})
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment