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    var loggedMsgArray = (function { // First, log all the old messages, once the page is loaded window.addEventListener("load", function {           var messagesObj = window.mainRoom.model.chats._byCid;            var messages = Object.keys(messagesObj);            messages.forEach(function(a) { // Getting the messages' data var loggedTime = getTime(new Date(messagesObj[a]._previousAttributes.timeStamp)); var nickname = messagesObj[a]._previousAttributes.name; var content = messagesObj[a]._previousAttributes.text; var oldMsg = loggedTime + "<" + nickname + "> " + content; // This way it won't put chat greetings and other useless alerts if (messagesObj[a].attributes.roomId !== 0) loggedMsg.push(oldMsg); });           // Debugging            console.log(window.mainRoom.model.chats._byCid);        }); // 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        formatLogs: 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(loggedMsgArray[amount]); amount++; }, this);           } else {                var messagesObj = window.mainRoom.chats.privates[mainRoom.activeRoom].model.chats._byCid;                var messages = Object.keys(messagesObj);                this.formatLogs(messages, messagesObj);            }            // Debugging            console.log(this.logged.join("\n"));        },        // Creating paragraphs for a future modal        createText: function {            var textDiv = $(" ", { class: "ChatLoggerDiv" });           for(var c = 0; c < this.logged.length; c++) {                textDiv.append($(" ", {                    text: this.logged[c]                }));            }            return textDiv;        },        // A modal window that contains the logged stuff        modal: function {            var modalContent = [this.createText,                $(" ", { id: "ChatLoggerCopy", text: "Copy" })           ];            $.showCustomModal("The result of logging the chat", $(" ").append(modalContent).html, {               id: "ChatLoggerResult", }).css({ width: "750px", overflow: "auto", height: "550px", "margin-left": "-388px" });           $("#ChatLoggerCopy").click(function { console.log("Yes it works"); // NO IT FUCKING DOES NOT // It's also a DOM function that you can call on nodes but only to execute their click listeners, not to set them var temp = document.createElement("input"); temp.setAttribute("value", document.getElementById("ChatLoggerDiv").innerHTML); document.body.appendChild(temp); temp.select; document.execCommand("copy"); document.body.removeChild(temp); $("#ChatLoggerResult").append($(" ", { id: "ChatLoggerCopied", text: "Text is copied!" }));           });        }    };    // 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'    ] });