-
Notifications
You must be signed in to change notification settings - Fork 4
/
AddYouTubeShortcuts.user.js
85 lines (79 loc) · 2.57 KB
/
AddYouTubeShortcuts.user.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// ==UserScript==
// @name AddYouTubeShortcuts
// @version 1.0.3
// @description Add YouTube keyboard shortcuts to any video
// @author kevduc
// @namespace https://kevduc.github.io/
// @homepageURL https://github.com/kevduc/userscripts
// @downloadURL https://raw.githubusercontent.com/kevduc/userscripts/master/AddYouTubeShortcuts.user.js
// @updateURL https://raw.githubusercontent.com/kevduc/userscripts/master/AddYouTubeShortcuts.user.js
// @supportURL https://github.com/kevduc/userscripts/issues
// @include *
// @exclude https://www.youtube.com/*
// remove the line above if you want to run the script on YouTube
// @grant none
// @run-at document-end
// @icon https://www.google.com/s2/favicons?sz=128&domain=youtube.com
// ==/UserScript==
;(function () {
'use strict'
const notificationStack = document.createElement('div')
notificationStack.style = `
position: fixed;
bottom: 0;
right: 0;
padding: 0.5rem;
`
document.body.appendChild(notificationStack)
const notify = (message, timeout) => {
const transitionDelay = 500
const div = document.createElement('div')
div.style = `
background-color: #222;
border: 2px solid #ddd;
padding: 0.5rem;
font-size: smaller;
font-family: sans-serif;
color: #ddd;
margin: 0.5rem 0 0 auto;
min-width: 7ch;
width: fit-content;
border-radius: 0.5rem;
opacity: 1;
transition: opacity ${transitionDelay}ms ease-in;
text-align: center;
`
div.innerText = message
notificationStack.appendChild(div)
setTimeout(() => {
div.style.opacity = '0'
setTimeout(() => div.remove(), transitionDelay)
}, timeout)
}
const closestSibling = (element, query) => {
const parent = element.parentElement
if (parent === null) return null
const sibling = parent.querySelector(query)
if (sibling !== null) return sibling
return closestSibling(parent, query)
}
document.addEventListener('keydown', (e) => {
const video = closestSibling(document.activeElement, 'video')
if (video === null) return
const notifySpeed = (speed) => notify(`${speed}x`, 1500)
switch (e.key) {
case '>': {
const newRate = (video.playbackRate += 0.25)
notifySpeed(newRate)
break
}
case '<': {
const newRate = (video.playbackRate -= 0.25)
notifySpeed(newRate)
break
}
default:
break
}
})
})()