-
Notifications
You must be signed in to change notification settings - Fork 2
/
correctPause.js
60 lines (43 loc) · 1.62 KB
/
correctPause.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
console.log("Correct Pause v1.8.1 - Started Successfully");
window.onkeydown = keyDownHandler;
window.onkeyup = keyUpHandler;
let latestAction;
// Is it embed type of video? (like https://www.youtube.com/embed/ftl3ZCr_Cn4)
let isEmbed = /^.*:\/\/www\.youtube\.com\/embed.*$/.test(window.location.href);
// Elements on which if focused pressing space won't play/pause the video
let forbiddenElements = ['ytd-searchbox', 'contenteditable-root', 'gsfi ytd-searchbox', 'style-scope yt-formatted-string', 'search', 'style-scope tp-yt-paper-input']
// Determines if play/pause is allowed (e.g: when typing in search or comments)
function isSpaceAllowed() {
// This line makes spacebar work properly on embed videos
if (isEmbed && document.activeElement.classList.contains('html5-video-player')) return false;
for (let i = 0; i < forbiddenElements.length; i++) {
let element = forbiddenElements[i];
if (document.activeElement.className === element || document.activeElement.id === element)
return false;
}
return true;
}
function keyDownHandler(e) {
if (!isSpaceAllowed()) return;
let vid = document.querySelector('video');
const key = e.code;
if (key != 'Space') return;
if (vid.paused || vid.ended) play(vid);
else pause(vid);
}
function keyUpHandler(e) {
if (!isSpaceAllowed()) return;
let vid = document.querySelector('video');
const key = e.code;
if (key != 'Space') return;
if (latestAction === 'play') play(vid);
if (latestAction === 'pause') pause(vid);
}
function play(video) {
latestAction = 'play';
video.play();
}
function pause(video) {
latestAction = 'pause';
video.pause();
}