User:Dorumin/monobook.js

// Is after $.fn.isAfter = function(sel) { return this.prevAll(sel).length !== 0; };

// Regex Escape RegExp.escape= function(s) { return s.replace(/[-\/\\^$*+?.|[\]{}]/g, '\\$&'); };

// For custom inline-alerts function getInlineRegex(variable, Default, title) { $.get('/api.php?action=query&format=json&prop=revisions&rvprop=content&titles=' + title, function(data) {      if ( data.query.pages['-1'] != null) {            window[variable] = Default;        } else {            window[variable] = RegExp.escape(data.query.pages[Object.keys(data.query.pages)[0]].revisions[0]['*']).replace(/\\\$(1|2|3)/g, '.*');        }    }); }

// Don't leave window.onbeforeunload = function { if (leave || restart) return; return 'Man you must be out of your mind!'; };

function API(method, data, callback) { data.format = 'json'; $.ajax({       type: method,        data: data,        url: wgScriptPath + '/api.php',        dataType: 'json',        success: function(response) {            if (response.error) {                console.log('API error: ' + response.error.info);            } else {                callback(response);            }        },        error: function {            console.log('An AJAX error occurred.');        }    }); }

getInlineRegex('kickmsg', '.+ has been kicked by .+', 'MediaWiki:Chat-user-was-kicked'); getInlineRegex('cbanmsg', '.+ has been banned by .+', 'MediaWiki:Chat-user-was-banned'); getInlineRegex('quitmsg', '.+ has left the chat', 'MediaWiki:Chat-user-parted'); getInlineRegex('joinmsg', '.+ has joined the chat', 'MediaWiki:Chat-user-joined');

var kickcount = 0, cbancount = 0, quitcount = 0, joincount = 0, chatcount = 0;

var months = [ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ],   weekdays = [ "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" ];

var logText = ' '; var curDay = new Date.getUTCDate; // Send function var send = function(msg) { mainRoom.socket.send(new models.ChatEntry({ roomId: this.roomId, name: mw.config.get('wgUserName'), text: msg }).xport); };

// Function itself var BotCheck = function(chat) { /* Logging */ // return if it was found before the bot entered, or if it its placed before the last mesage on !restart if (!$('#entry-' + chat.cid).isAfter('.inline-alert')) return; // get variables var t = chat.attributes.text, te = mw.html.escape(t); var n = chat.attributes.name, ne = mw.html.escape(n); var isInlineAlert = chat.attributes.isInlineAlert == null ? false : true; var ns = n + 'Swr'; cid = chat.cid; var msgType; // submit at the end of the day if ( new Date.getUTCDate != curDay ) { curDay = new Date.getUTCDate; submitLogs(date.getUTCDate + '_' + months[date.getUTCMonth] + '_' + date.getUTCFullYear); }   date = new Date; // get message type if ( isInlineAlert ) { if ( new RegExp(joinmsg, 'mi').test(t)) { msgType = ' [JOIN] '; joincount++; } else if ( new RegExp(quitmsg, 'mi').test(t)) { msgType = ' [QUIT] '; quitcount++; } else if ( new RegExp(kickmsg, 'mi').test(t)) { msgType = ' [KICK] '; localStorage.setItem('lastKick', t.trim.replace(/\(gem destabilizer left\) \[\[User:.*?\||\]\] has been destabilized by \[\[User:\$2\|.*\]\] \(gem destabilizer right\)/g, '')); kickcount++; } else if ( new RegExp(cbanmsg, 'mi').test(t)) { msgType = ' [CBAN] '; te = te.replace(/\(&lt;a href=&quot;#&quot; data-type=&quot;ban-undo&quot; data-user=&quot;.+&quot;\s*&gt;undo&lt;\/a&gt;\)/, ''); cbancount++; } else { return; }   } else if (n === mw.config.get('wgUserName') ) { msgType = ' [CBOT] '; } else { msgType = ' [CHAT] '; chatcount++; }   // get message time var msgDate = new Date; var msgTimestamp = (msgDate.getUTCHours < 10 ? '0' : ) + msgDate.getUTCHours + ':' + (msgDate.getUTCMinutes < 10 ? '0' : ) + msgDate.getUTCMinutes + ':' + (msgDate.getUTCSeconds < 10 ? '0' : '') + msgDate.getUTCSeconds; // add the values to the actual log if ( msgType == ' [CHAT] ' || msgType == ' [CBOT] ' ) { te = te.replace(/\n/g, '\n         ' + msgType); logText = logText.replace(' ', '[' + msgTimestamp + ']' + msgType + ne + ': ' + te + '\n '); } else { logText = logText.replace(' ', '[' + msgTimestamp + ']' + msgType + te + '\n '); } };

// Submits the logs (uploadText) and then clears them submitLogs = function(logDate) { if ( wgPageName != 'Special:Chat' || isLogging ) return; isLogging = true; var d = new Date, title = logDate || d.getUTCDate + '_' + months[d.getUTCMonth] + '_' + d.getUTCFullYear, uploadText = logText; logText = ' '; API('GET', {       'action': 'query',        'prop': 'info|revisions',        'intoken': 'edit',        'titles': 'Project:Chat/Logs/' + title,        'rvprop': 'content|timestamp',        'rvlimit': '1',        'indexpageids': 'true',    }, function(data) {        var page = data.query.pages[Object.keys(data.query.pages)[0]];        var pageExists = data.query.pages["-1"] ? false : true;        var content = typeof(page.revisions) != "undefined" ? page.revisions[0]['*'] : '';        if (!pageExists) {            API('POST', { 'minor': 'yes', 'bot': 'yes', 'summary': 'Creating chatlog: ' + kickcount + ' kicks and ' + cbancount + ' bans reported. ' + joincount + ' joins, ' + quitcount + ' leaves, and ' + chatcount + ' messages logged.', 'action': 'edit', 'title': 'Project:Chat/Logs/' + title, 'token': page.edittoken, 'text': '\n' + uploadText + '\n' }, function { isLogging = false; console.log('Creating chatlog: ' + kickcount + ' kicks and ' + cbancount + ' bans reported. ' + joincount + ' joins, ' + quitcount + ' leaves, and ' + chatcount + ' messages logged.'); kickcount = 0, cbancount = 0, quitcount = 0, joincount = 0, chatcount = 0; $('#entry-' + cid).prevAll('.Chat li').remove; $('#entry-' + cid).replaceWith('Window cleared.'); });       } else {            API('POST', { 'minor': 'yes', 'bot': 'yes', 'summary': 'Adding to chatlog: ' + kickcount + ' kicks and ' + cbancount + ' bans reported. ' + joincount + ' joins, ' + quitcount + ' leaves, and ' + chatcount + ' messages logged.', 'action': 'edit', 'title': 'Project:Chat/Logs/' + title, 'basetimestamp': page.revisions[0].timestamp, 'token': page.edittoken, 'text': content.replace(' ', uploadText) }, function { isLogging = false; console.log('Adding to chatlog: ' + kickcount + ' kicks and ' + cbancount + ' bans reported. ' + joincount + ' joins, ' + quitcount + ' leaves, and ' + chatcount + ' messages logged.'); kickcount = 0, cbancount = 0, quitcount = 0, joincount = 0, chatcount = 0; $('#entry-' + cid).prevAll('.Chat li').remove; $('#entry-' + cid).replaceWith('Window cleared.'); });       }    }); }; // Sets the interval for submitting the logs setInterval(function {   submitLogs; }, window.logInterval || 600000);

// Append the function to chat updates if (mw.config.get('wgCanonicalSpecialPageName') == 'Chat') { mainRoom.model.chats.bind('afteradd', BotCheck); } // Confirm the bot is up and running $(window).load(function {   send('ChatBot v1.4 is online!'); });