User:Lady Furude/chat.js

/* FucodeLogger.js is an advanced script for logging the chat and private messages. * Contains several features, such as copying the logs or downloading them with a * special chosen filename. In other words, whatever you could wish for. * Authors: Lady Furude, KockaAdmiralac * * Thanks to KockaAdmiralac for help with finding appropriate methods, giving the * object that solved the difficulty of logging the messages, for testing the code with * me in his personal wiki, allowing to keep it there and suggesting Download feature. * Thanks to Speedit for help with making a button and creating an object~ * * If you've got a suggestion or found a bug, contact Lady Furude! */

$(function {   "use strict";    // This array is going to contain the messages of the main chat    var loggedMsg = [];    function getTime(c) {        var year = c.getFullYear;        var month = String(c.getMonth + 1).padStart(2, 0);        var day = c.getDate.toString.padStart(2, 0);        var hour = c.getHours.toString.padStart(2, 0);        var minute = c.getMinutes.toString.padStart(2, 0);        var second = c.getSeconds.toString.padStart(2, 0);        return "[" + year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second + "] ";    }    // Logging the main chat    // Warning: Doesn't include messages sent before user's arrival.    ["chat:add", "join", "part", "logout", "kick", "ban"].forEach(function(e) { mainRoom.socket.bind(e, function(a) {           var loggedTime = getTime(new Date);            var data = JSON.parse(a.data);            var logged;            var joinLogged = loggedTime + data.attrs.name + " " + "has joined the chat";            var partLogged = loggedTime + data.attrs.name + " " + "has left the chat";            var logoutLogged = loggedTime + data.attrs.name + " " + "has closed the chat window";            var kickLogged = loggedTime + data.attrs.kickedUserName + " " + "has been kicked by" + " " + data.attrs.moderatorName;            var banLogged = loggedTime + data.attrs.kickedUserName + " " + "has been banned by" + " " + data.attrs.moderatorName;            if (e == "chat:add") {                data.attrs.text.split("\n").forEach(function(line) { loggedMsg.push(loggedTime + "<" + data.attrs.name + "> " + line); });           }            else if (e == "join") logged = joinLogged;            else if (e == "part") logged = partLogged;            else if (e == "logout") logged = logoutLogged;            else if (e == "kick") logged = kickLogged;            else if (e == "ban") logged = banLogged;            if (e !== "chat:add") loggedMsg.push(logged);        }); }, this);   var FucodeLogger = {        logged: [],        logActiveRoom: function {            if (mainRoom.activeRoom == "main" || mainRoom.activeRoom === null) {                // Pushing all that was logged by IIFE above                for (var e = 0; e < loggedMsg.length; e++) {                    this.logged.push(loggedMsg[e]);                }            } else {                // Otherwise, logging messages of a private room and format them                /* var msgObj = window.mainRoom.chats.privates[mainRoom.activeRoom].model.chats._byCid;                var msg = Object.keys(msgObj);                msg.forEach($.proxy(function(a) {                    console.log(this);                    msgObj[a]._previousAttributes.text.split("\n").forEach(function(line) { this.logged.push(getTime(new Date(msgObj[a]._previousAttributes.timeStamp)) + "<" + msgObj[a]._previousAttributes.name + "> " + line); }, this);               }, this)); */                var msgObj = window.mainRoom.chats.privates[mainRoom.activeRoom].model.chats._byCid;                for (var i in msgObj) {                    msgObj[i]._previousAttributes.text.split("\n").forEach(function(line) { this.logged.push(getTime(new Date(msgObj[i]._previousAttributes.timeStamp)) + "<" + msgObj[i]._previousAttributes.name + "> " + line); }, this);               }            }        },        createText: function {            // Creating text for a future modal            var preContent = "";            for (var c = 0; c < this.logged.length; c++) {                // Placing the last message of logs                if (c + 1 == this.logged.length) preContent += this.logged[c];                else { preContent += this.logged[c] + "\n"; }            }            return $(" ", { id: "FucodeLoggerContent", text: preContent, });       },        copyLogs: function {            document.querySelector("#FucodeLoggerCopy").addEventListener("click", function { // Double-click: the copy feature works at the second click, for some reason. $("#FucodeLoggerCopy").click; var range = document.createRange; range.selectNode(document.querySelector("#FucodeLoggerContent")); window.getSelection.addRange(range); try { document.execCommand("copy"); if (!document.getElementById("FucodeLoggerCopied")) { $(".modalContent").append($(" ", { id: "FucodeLoggerCopied", text: "Text is copied!" }));                   }                } catch(err) { // If it's not copied, look into console console.log("Whoops, unable to copy.\nMaybe switch to a normal browser?.."); }               window.getSelection.removeAllRanges; });       },        clickToDownloadLogs: function {            $("#FucodeLoggerExecuteDl").click(function { if (!document.getElementById("FucodeLoggerLinkDl")) { $("#FucodeLoggerExecuteDl").text(""); $("#FucodeLoggerExecuteDl").append($("", { id: "FucodeLoggerLinkDl", download: (function {                           var filename = $("#FucodeLoggerSetFilename").val + ".txt";                            return filename;                        }), href: URL.createObjectURL(new Blob([$("#FucodeLoggerContent").text], {type: 'octet/stream'})), text: "Download logs" }).css({ color: "#FFF", "text-decoration": "none" }));               }            });        },        downloadLogs: function {            $("#FucodeLoggerDl").click($.proxy(function {                if (!document.getElementById("FucodeLoggerSetDownload")) {                    $(".modalContent").append($(" ", {                        id: "FucodeLoggerSetDownload"                    }));                    $("#FucodeLoggerSetDownload").append([$(" ", {                        id: "FucodeLoggerFilenameNotif",                        text: "Type the filename:"                    }), $(" ", {                        id: "FucodeLoggerSetFilename",                        type: "text"                    }), $(" ", {                        id: "FucodeLoggerExecuteDl",                        name: "setFilename",                        text: "Set the filename"                    })]);                    // Allowing an user to press Enter instead of clicking the button $("#FucodeLoggerSetFilename").keyup(function(event) {                       if (event.keyCode === 13) $("#FucodeLoggerExecuteDl").click;                    }); this.clickToDownloadLogs; }           }, this)); },       createModal: function { $.showCustomModal("The result of logging the chat", $(" ").append([this.createText, $(" ", {                   id: "FucodeLoggerCopy",                    text: "Copy"                }), $(" ", {                   id: "FucodeLoggerDl",                    text: "Download"                }) ]).html, {               id: "FucodeLoggerResult",            }).css({                width: "750px",                overflow: "auto",                height: "550px",                "margin-left": "-388px"            }); this.copyLogs; this.downloadLogs; },   };

// Creating a button mw.hook("dev.chat").add(function(chat) {       FucodeLogger.button = new chat.Button({ name: "ChatLog", attr: { text: "Log", click: function { FucodeLogger.logged = []; FucodeLogger.logActiveRoom; FucodeLogger.createModal; }           }        });    });

// An incredible library made by Speedit, thanks! importArticle({       type: "script",        article: "u:dev:Chat-js.js"    }); });