Animelon shortcuts

Adds several shortcuts to Animelon

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey, Greasemonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्क्रिप्ट व्यवस्थापक एक्स्टेंशन इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्क्रिप्ट व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्टाईल व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

// ==UserScript==
// @name         Animelon shortcuts
// @namespace    https://github.com/sahlaysta/
// @version      0.2
// @description  Adds several shortcuts to Animelon
// @author       sahlaysta
// @match        https://*.animelon.com/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=animelon.com
// @grant        none
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    //adds a shortcut operation
    function addShortcutFunction(keyCodes, shortcutFn) {
        document.addEventListener('keydown', event => {
            if (!event.altKey && !event.ctrlKey && !event.isComposing
                && !/^(?:input|textarea|select|button)$/i.test(event.target.tagName)
                && keyCodes.includes(event.keyCode)) {
                shortcutFn();
                event.preventDefault();
            }
        });
    }

    //returns the angularjs ng controller for the video player
    function getVideoController() {
        return window.angular.element(document.getElementById('video-player-container')).scope();
    }

    // Spacebar and K to pause/unpause
    addShortcutFunction([32, 75], () => {
        const ngCtrl = getVideoController();
        ngCtrl.playerValues.triggers.play(!(ngCtrl.playerValues.playing));
    });

    // M to mute/unmute
    addShortcutFunction([77], () => {
        const ngCtrl = getVideoController();
        ngCtrl.playerValues.triggers.volume(ngCtrl.playerValues.storage.volume === 0 ? 1 : 0);
    });

    // P to show/unshow the Discord panel
    addShortcutFunction([80], () => {
        document.getElementsByClassName('discordButton')[0].click();
    });

    // J to jump to previous dialogue
    addShortcutFunction([74], () => {
        const ngCtrl = getVideoController();
        ngCtrl.playerValues.triggers.backward();
    });

    // L to jump to next dialogue
    addShortcutFunction([76], () => {
        const ngCtrl = getVideoController();
        ngCtrl.playerValues.triggers.forward();
    });

})();