Reimu / AC-Restore-Chatlogs

// ==UserScript==
// @name         AC-Restore-Chatlogs
// @namespace    http://tampermonkey.net/
// @version      1.0.0
// @copyright    2021, Reimu(https://openuserjs.org/users/Reimu)
// @license      MIT
// @description  Stellt Chatverläufe nach Disconnects auf Anime Academy wieder her.
// @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 maxStoredMessages = 50;

    window.addEventListener('globalSocketReady', () => {

        window.socket.on('updateChatLines', (data) => {

            let currentRoom = window.location.href.split('=')[1];

            let roomData;

            if (!localStorage.hasOwnProperty('rooms')) {
                localStorage.setItem('rooms', '{}');
            }
            if (!JSON.parse(localStorage.getItem('rooms')).hasOwnProperty(currentRoom)) {
                roomData = JSON.parse(localStorage.getItem('rooms'));
                roomData[currentRoom] = {
                    messages: []
                };
                roomData = JSON.stringify(roomData);
                localStorage.setItem('rooms', roomData);
            }

            roomData = JSON.parse(localStorage.getItem('rooms'));
            if(data.user !== 'System')
                roomData[currentRoom].messages.push(data);
            if (roomData[currentRoom].messages.length > maxStoredMessages)
                roomData[currentRoom].messages.splice(0, 1);

            roomData = JSON.stringify(roomData);
            localStorage.setItem('rooms', roomData);
        });

        return restoreChatlogs();
    });

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

        let currentRoomChatlogs = undefined;

        if (JSON.parse(localStorage.getItem('rooms')))
            if (JSON.parse(localStorage.getItem('rooms'))[window.location.href.split('=')[1]])
                currentRoomChatlogs = JSON.parse(localStorage.getItem('rooms'))[window.location.href.split('=')[1]].messages;

        if (currentRoomChatlogs) {
            setTimeout(() => {
                for (const messageData of currentRoomChatlogs) {
                    if (messageData.user !== 'System') {
                        let message = {
                            hasPremium: messageData.hasPremium,
                            msg: messageData.chatLine,
                            festername: messageData.festername,
                            house: (messageData.house ? messageData.house : null),
                            color: undefined,
                            user: messageData.user + ': ',
                            timestamp: `${new Date(messageData.timestamp).toLocaleDateString('de-DE')} - ${new Date(messageData.timestamp).toLocaleTimeString('de-DE').slice(0, -3)} Uhr`
                        };

                        scope.chatmsgs.push(message);
                        document.getElementById('topbar').click();
                    }

                }
            }, 1500);
        }
    }


})();