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";    var loggedLines = [];    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 + "] ";    }    function logByObject(obj, el, arr) {        obj[el].attributes.text.split("\n").forEach(function(line) { arr.push(getTime(new Date(obj[el].attributes.timeStamp)) + "<" + obj[el].attributes.name + "> " + line); });   }    mw.hook("dev.chat.render").add(function { var sentMsgObj = window.mainRoom.model.chats._byCid; for (var i in sentMsgObj) { if (sentMsgObj[i].attributes.isInlineAlert !== true) { logByObject(sentMsgObj, i, loggedLines); }       }    });    ["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) { loggedLines.push(loggedTime + "<" + data.attrs.name + "> " + line); });           }            var loggedEvent = (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") loggedLines.push(loggedEvent);        }); });   var FucodeLogger = {        logged: [],        logActiveRoom: function {            if (mainRoom.activeRoom == "main" || mainRoom.activeRoom === null) {                for (var e = 0; e < loggedLines.length; e++) {                    this.logged.push(loggedLines[e]);                }            } else {                var msgObj = window.mainRoom.chats.privates[mainRoom.activeRoom].model.chats._byCid;                for (var i in msgObj) {                    logByObject(msgObj, i, this.logged);                }            }        },        createText: function {            var preContent = "";            for (var c = 0; c < this.logged.length; c++) {                preContent += this.logged[c];                if (c + 1 !== this.logged.length) preContent += "\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 (!$("FucodeLoggerCopied").exists) { $(".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 (!$("FucodeLoggerLinkDl").exists) { $("#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 (!$("FucodeLoggerSetDownload").exists) { $(".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:ChatDeveloperTools.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"    ] });