Reimu / AC-Autoreconnect

// ==UserScript==
// @name         AC-Autoreconnect
// @namespace    http://tampermonkey.net/
// @version      1.0.0
// @copyright    2021, Reimu(https://openuserjs.org/users/Reimu)
// @license      MIT
// @description  Stellt nach Disconnects automatisch die Verbindung mit dem Chat wieder her und bewegt euren Avatar an die vorherhige Position.
// @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';

    window.onbeforeunload = null;

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

    if (disconnected) {
        localStorage.setItem('disconnected', false);
        window.addEventListener('globalSocketReady', () => {
            setTimeout(() => {
                window.socket.emit('moveAvatar', JSON.parse(localStorage.getItem('avatarPosition')));
            }, 1500);
        });
    }

    window.addEventListener('socketEmit', (e) => {
        if (e.detail.eventName === 'moveAvatar') {
            localStorage.setItem('avatarPosition', JSON.stringify(e.detail.args[0]));
        }
        if (e.detail.eventName === 'disconnect' && (e.detail.args[0] === 'transport error' || e.detail.args[0] === 'io client disconnect')) {
            localStorage.setItem('disconnected', true);
            location.reload();
        }
    });


})();