Reimu / AC-Resume-YouTube-Video

// ==UserScript==
// @name         AC-Resume-YouTube-Video
// @namespace    http://tampermonkey.net/
// @version      1.0.0
// @copyright    2021, Reimu(https://openuserjs.org/users/Reimu)
// @license      MIT
// @description  Spielt bei Disconnects das Video dort weiter wo es abgrebrochen wurde.
// @author       Slash aka. Reimu or Nick S.
// @include      https://www.anime.academy/chat?room=*
// @include      https://anime.academy/chat?room=*
// @include      anime.academy/chat?room=*
// @icon         https://www.google.com/s2/favicons?domain=anime.academy
// @grant none
// ==/UserScript==


(function () {
    'use strict';

    let scope = angular.element(document.getElementById('topbar')).scope();

    let chatWindow = document.getElementById('graphicWindow');

    let videoInterrupted = JSON.parse(localStorage.getItem('videoInterrupted'));

    if (videoInterrupted) {
        localStorage.setItem('videoInterrupted', false);
        window.addEventListener('globalSocketReady', () => {
            setTimeout(() => {
                scope.youtubeVideoUrl = JSON.parse(localStorage.getItem('currentVideoData')).videoUrl;
                document.getElementById('topbar').click();
                scope.$on('youtube.player.ready', ($event, player) => {
                    chatWindow.style.display = 'none';
                    scope.showYoutubeArea = true;
                    scope.youtubePlayer.playVideo();
                    scope.youtubePlayer.seekTo(JSON.parse(localStorage.getItem('currentVideoData')).time);
                    scope.animatedBg = false;
                    scope.nightVersion = false;
                });
                scope.$on('youtube.player.ended', ($event, player) => {
                    chatWindow.style.display = '';
                });
                scope.$on('youtube.player.queued', ($event, player) => {
                    chatWindow.style.display = '';
                });
                window.socket.on('pauseYoutube', () => {
                    chatWindow.style.display = '';
                });
                window.socket.on('stopYoutube', () => {
                    chatWindow.style.display = '';
                });
                window.socket.on('submitYoutubeVideo', () => {
                    chatWindow.style.display = '';
                });
            }, 1500);
        });
    }

    window.addEventListener('socketEmit', (e) => {
        if (e.detail.eventName === 'disconnect' || e.detail.args[0] === 'transport error') {
            if (scope.youtubePlayer.currentState === 'playing') {
                let currentVideoData = {
                    time: scope.youtubePlayer.getCurrentTime(),
                    videoUrl: scope.youtubeVideoUrl
                }
                localStorage.setItem('currentVideoData', JSON.stringify(currentVideoData));
                localStorage.setItem('videoInterrupted', true);
            }
        }
    });

})();