User:Lady Furude/chat.js

$(function {   "use strict";    // This array is going to contain all the messages    var loggedMsg = [];    // Function for outputting time parameters    function getTime(c) {        var time = c;        var year = c.getFullYear;        var month = c.getMonth + 1;        var day = c.getDate;        var hour = c.getHours;        var minute = c.getMinutes;        var second = c.getSeconds;        // Formatting the time's data        month = month.toString.padStart(2, 0);        day = day.toString.padStart(2, 0);        hour = hour.toString.padStart(2, 0);        minute = minute.toString.padStart(2, 0);        second = second.toString.padStart(2, 0);        // Returning the part with timestamp of a message        return "[" + year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second + "] ";    }    // Log everything by event    (function { // Describing actions that need to be logged var actions = ["chat:add", "join", "logout", "kick", "ban"]; // Defining their appearance var actionsLook = ["has joined the chat", "has left the chat", "has been kicked by", "has been banned by"]; actions.forEach(function(t) {           mainRoom.socket.bind(t, function(a) { var loggedTime = getTime(new Date); // Getting the content data var data = JSON.parse(a.data); var nickname = data.attrs.name; var text = data.attrs.text; // For kicks and bans var modName = data.attrs.moderatorName; var kickedName = data.attrs.kickedUserName; // Future messages var logged; var msgLogged = loggedTime + "<" + nickname + "> " + text; var joinLogged = loggedTime + nickname + " " + actionsLook[0]; var partLogged = loggedTime + nickname + " " + actionsLook[1]; var kickLogged = loggedTime + kickedName + " " + actionsLook[2] + " " + modName; var banLogged = loggedTime + kickedName + " " + actionsLook[3] + " " + modName; // Choosing the format depending on the event type if (t == "join") logged = joinLogged; else if (t == "chat:add") logged = msgLogged; else if (t == "logout") logged = partLogged; else if (t == "kick") logged = kickLogged; else if (t == "ban") logged = banLogged; // Putting the message into the array loggedMsg.push(logged); });       }, this); return loggedMsg; });   // Creating a future feature    var chatLogger = {        // This array contains the logged messages        logged: [],        // Formatting the private logs        formatPM: function(messages, messagesObj) {            messages.forEach(function(a) { var nickname = messagesObj[a]._previousAttributes.name; var content = messagesObj[a]._previousAttributes.text; // Declaring the format of logs var loggedTime = getTime(new Date(messagesObj[a]._previousAttributes.timeStamp)); var privateMsg = loggedTime + "<" + nickname + "> " + content; this.logged.push(privateMsg); }, this);       },        // Logging the currently opened room        clickLog: function {            // This variable contains the number of iteration when the main chat gets logged            var amount = 0;            if (mainRoom.activeRoom == "main" || mainRoom.activeRoom === null) {                loggedMsg.forEach(function(a) { this.logged.push(loggedMsg[amount]); amount++; }, this);           } else {                var messagesObj = window.mainRoom.chats.privates[mainRoom.activeRoom].model.chats._byCid;                var messages = Object.keys(messagesObj);                this.formatPM(messages, messagesObj);            }        },        // Creating paragraphs for a future modal        createText: function {            var preContent = "";            for (var c = 0; c < this.logged.length; c++) {                console.log(this.logged[c]);                preContent += this.logged[c] + '\n';            }            console.log(preContent);            var textDiv = $(" ", { id: "ChatLoggerContent", text: preContent, });           return textDiv;        },        // A modal window that contains the logged stuff        modal: function {            var modalContent = [this.createText,                $(" ", { id: "ChatLoggerCopy", text: "Copy" }),               $(" ", {                    id: "ChatLoggerDl" })           ];            $.showCustomModal("The result of logging the chat", $(" ").append(modalContent).html, {               id: "ChatLoggerResult", }).css({ width: "750px", overflow: "auto", height: "550px", "margin-left": "-388px" });           // Copy button feature: copy the logs with one single click! (Or two of them :P)            var chatLoggerCopy = document.querySelector("#ChatLoggerCopy");            chatLoggerCopy.addEventListener("click", function(event) { // Double-click: the copy feature works at the second click, for some reason. $("#ChatLoggerCopy").click; var content = document.querySelector("#ChatLoggerContent"); var range = document.createRange; range.selectNode(content); window.getSelection.addRange(range); try { var successful = document.execCommand("copy"); } catch(err) { console.log("Oops, unable to copy"); }               window.getSelection.removeAllRanges; // Shows that logs are copied if (!document.getElementById("ChatLoggerCopied")) { $(".modalContent").append($(" ", { id: "ChatLoggerCopied", text: "Text is copied!" }));               }            });            // Download feature: choose the filename and keep the logs on your side easily!            var blob = new Blob([$("#ChatLoggerContent").text], {type: 'octet/stream'});            var buttonDl = $("#ChatLoggerDl");            buttonDl.append($("", {                download: "logs.txt",                href: URL.createObjectURL(blob),                text: "Download logs"            }).css({color: "#FFF"}));        }    };    // Creating a button    mw.hook("dev.chat").add(function(chat) { chatLogger.button = new chat.button({           name: "ChatLog",            attr: {                text: "Log",                click: function {                    chatLogger.logged = [];                    chatLogger.clickLog;                    chatLogger.modal;                }            }        }); });

// thanks to Speedy-kun for this incredible library <3 importArticle({       type: "script",        article: "u:dev:Chat-js.js"    }); });

importArticles({   type: 'script',    articles: [        'u:dev:MediaWiki:ChatNotifications/code.js',        'u:dev:MediaWiki:MobileChat.js',        'u:dev:MediaWiki:ChatHacks.js'    ] });