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 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); });           }            var logged = (function { switch(e) { case "join": return joinLogged; case "part": return partLogged; case "logout": return logoutLogged; case "kick": return kickLogged; case "ban": return banLogged; }           });            if (e !== "chat:add") loggedMsg.push(logged);        }); });   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;                for (var i in msgObj) {                    // A little mistake here because it's a function inside a loop... rip                    msgObj[i].attributes.text.split("\n").forEach(function(line) { this.logged.push(getTime(new Date(msgObj[i].attributes.timeStamp)) + "<" + msgObj[i].attributes.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(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; }           }.bind(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"    }); });

// lags because it prevents removing .selected $("h1.public.wordmark").css({   color: "#EE537D",    "font-size": "12px",    "font-weight": "bold",    "padding-left": "15px" }).text("Community Central");

importArticles({   type: 'script',    articles: [        'u:dev:MediaWiki:ChatAwayButton/code.js',        'u:dev:MediaWiki:ChatBlockButton/code.2.js',        'u:dev:MediaWiki:ChatNotifications/code.js',        //I guess it has no bugs: 'u:dev:MediaWiki:ChatUserPageButton.js',        'u:dev:MediaWiki:EmoticonsWindow/code.js',        // Testing still: 'u:dev:MediaWiki:FucodeLogger.js',        'u:dev:MediaWiki:MobileChat.js'    ] });