-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathyoutube-space.user.js
82 lines (67 loc) · 1.98 KB
/
youtube-space.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
// ==UserScript==
// @name YouTube space to pause / play
// @namespace https://github.com/lilydjwg/userscripts
// @description Use space key to pause / play video
// @match https://www.youtube.com/*
// @version 0.3
// @grant window.onurlchange
// ==/UserScript==
(function() {
'use strict'
const run = function() {
console.log('ytspace: start')
if(location.href.indexOf('https://www.youtube.com/watch?') != 0) {
console.log('ytspace: not watch, returning')
return;
}
const v = document.querySelector('video.video-stream.html5-main-video')
if(!v) {
console.log('ytspace: no video, retry after 500ms')
setTimeout(run, 500)
return
}
console.log('ytspace: video element', v)
let last_pause = 0
let last_play = 0
v.addEventListener('pause', (e) => {
console.log('ytspace: paused')
last_pause = e.timeStamp
})
v.addEventListener('play', (e) => {
console.log('ytspace: played')
last_play = e.timeStamp
})
document.addEventListener('keyup', (e) => {
// https://developer.mozilla.org/en-US/docs/Web/API/Element/keyup_event
// console.log('keyup', e)
if (e.isComposing || e.keyCode === 229) {
return
}
if (e.keyCode === 0x20) {
if (['TEXTAREA', 'INPUT'].includes(e.target.tagName) || e.target.contentEditable === "true") {
// console.log('skip editable element', e.target, e.target.tagName, e.target.contentEditable)
return
}
// console.log('setTimeout')
setTimeout(function() { // run after YT's own
if (v.paused && Math.abs(e.timeStamp - last_pause) > 200) {
console.log('ytspace: play')
v.play()
} else if (Math.abs(e.timeStamp - last_play) > 200) {
console.log('ytspace: pause')
v.pause()
}
}, 100)
}
}, {
passive: true,
})
}
run()
if(window.onurlchange === null) {
// feature is supported
window.addEventListener('urlchange', (info) => {
run()
})
}
})()