aboutsummaryrefslogtreecommitdiffstats
path: root/youtube/static/js/hotkeys.js
blob: 2cd233692fc781e5351bfda94ca33ed74c569b14 (plain)
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
function onKeyDown(e) {
    if (['INPUT', 'TEXTAREA'].includes(document.activeElement.tagName)) return false;

    // console.log(e);
    let v = QId("js-video-player");
    let c = e.key.toLowerCase();
    if (e.ctrlKey) return;
    else if (c == "k") {
        v.paused ? v.play() : v.pause();
    }
    else if (c == "arrowleft") {
        e.preventDefault();
        v.currentTime = v.currentTime - 5;
    }
    else if (c == "arrowright") {
        e.preventDefault();
        v.currentTime = v.currentTime + 5;
    }
    else if (c == "j") {
        e.preventDefault();
        v.currentTime = v.currentTime - 10;
    }
    else if (c == "l") {
        e.preventDefault();
        v.currentTime = v.currentTime + 10;
    }
    else if (c == "f") {
        e.preventDefault();
        if (document.fullscreenElement && document.fullscreenElement.nodeName == 'VIDEO') {document.exitFullscreen();}
        else {v.requestFullscreen()};
    }
    else if (c == "m") {
        if (v.muted == false) {v.muted = true;}
        else {v.muted = false;}
    }
    else if (c == "c") {
        e.preventDefault();
        let tt = getActiveTranscriptTrack();
        if (tt == null) return;
        if (tt.mode == "showing") tt.mode = "disabled";
        else tt.mode = "showing";
    }
    else if (c == "t") {
        let ts = Math.floor(QId("js-video-player").currentTime);
        copyTextToClipboard(`https://youtu.be/${data.video_id}?t=${ts}`);
    }
}

window.addEventListener('DOMContentLoaded', function() {
    document.addEventListener('keydown', onKeyDown);
});