blonski / Twitch Hotkeys

// ==UserScript==
// @name          Twitch Hotkeys
// @author		  Flipperbw
// @license       MIT
// @version       1.3
// @include       /^https?://(www\.|beta\.)?twitch\.tv/
// @require       http://code.jquery.com/jquery-latest.min.js
// ==/UserScript==

// /^https?://(www|beta)\.twitch\.tv\/[a-zA-Z0-9]\w{3,24}\/[a-z]/\d+/
// Does not work for fullscreen due to browsers sucking

var offset = 5;

var ks = {
    "106": "j",
    "107": "k",
    "108": "l"
};

var current_secs = 0;
var new_secs = 0;
var player;
var player_status;

$(document).on("keypress", function (e) {
    var trgt = e.target;
    if (trgt && trgt.className && trgt.className.indexOf('chat_input') == -1 && trgt.getAttribute('data-a-target') != 'chat-input') {
        var k = ks[e.keyCode];
        if ((k == "j") || (k == "l") || (k == "k")) {
            player = $('#player object')[0];

            if (player) {
                if (k == "j") {
                    current_secs = player.getVideoTime();
                    new_secs = Math.max(current_secs - offset, 0.1);
                    player.videoSeek(new_secs);
                }
                else if (k == "l") {
                    current_secs = player.getVideoTime();
                    new_secs = current_secs + offset;
                    player.videoSeek(new_secs);
                }
                else if (k == "k") {
                    player_status = player.isPaused();
                    if (!player_status) {
                        player.playVideo();
                    }
                    else {
                        player.pauseVideo();
                    }
                }
            }
            else {
                player = $('.player-video video')[0];
                if (player) {
                    if (k == "j") {
                        current_secs = player.currentTime;
                        new_secs = Math.max(current_secs - offset, 0.1);
                        player.currentTime = new_secs;
                    }
                    else if (k == "l") {
                        current_secs = player.currentTime;
                        new_secs = current_secs + offset;
                        player.currentTime = new_secs;
                    }
                    else if (k == "k") {
                        player_status = player.paused;
                        if (player_status) {
                            player.play();
                        }
                        else {
                            player.pause();
                        }
                    }
                }
            }
        }
    }
});