User:XenoTejada/common.js

/* chat connect - vanilla version */ ;(function {   var chatconnect = {        regex: {            timestamp: /[0-2]{1}\d{1}:[0-5]{1}\d{1}, \w* \d{1,2}, \d{4}/gi,            ip: /(?:\d{1,3}\.){3}\d{1,3}/gi        },        title: document.title,        css: function {            var css = document.createElement('style');            css.type = 'text/css';            css.innerHTML =                '.banned {' +                    'background-color: #ffbfbf;' +                '}' +                '.banned:after {' +                    'content: \' [banned]\';' +                    'font-weight: bold;' +                    'color: darkred;' +                '}' +                '.banned a {' +                    'color: #c34d4d;' +                '}' +                '.inchat {' +                    'background-color: #c6e4ef;' +                '}' +                '.inchat:after {' +                    'content: \' [in chat]\';' + 'font-weight: bold;' + 'color: #000;' + '}';           document.head.appendChild(css); },       add: function(doc) { var doc = doc || document, list = doc.getElementsByClassName('mw-logline-chatconnect'); Array.prototype.forEach.call(list, function(item, i) {               list[i].innerHTML = list[i].innerHTML.replace(chatconnect.regex.timestamp, function(match) { return ' ' + match + ' '; }).replace(chatconnect.regex.ip, function(match) { return ' ' + match + ' '; });           });         },        get: function(doc) { var doc = doc || document; chatconnect.add(doc); var list = {}, logs = doc.getElementsByClassName('mw-logline-chatconnect'), rev = [].slice.call(logs, 0).reverse; Array.prototype.forEach.call(rev, function(item) {               list[item.children[1].innerText] = {                    timestamp: item.children[0].innerText,

ip: item.children[4].innerText

},       add: function(doc) { var doc = doc || document, list = doc.getElementsByClassName('mw-logline-chatconnect'); Array.prototype.forEach.call(list, function(item, i) {               list[i].innerHTML = list[i].innerHTML.replace(chatconnect.regex.timestamp, function(match) { return ' ' + match + ' '; }).replace(chatconnect.regex.ip, function(match) { return ' ' + match + ' '; });           });         },        get: function(doc) { var doc = doc || document; chatconnect.add(doc); var list = {}, logs = doc.getElementsByClassName('mw-logline-chatconnect'), rev = [].slice.call(logs, 0).reverse; Array.prototype.forEach.call(rev, function(item) {               list[item.children[1].innerText] = {                    timestamp: item.children[0].innerText,                    ip: item.children[4].innerText                };            }); return list; },       process: function(list) { var dupes = {}, users = Object.keys(list); for (var i in list) { if (list.hasOwnProperty(i)) { for (var j in list) { if (list.hasOwnProperty(j)) { if (i !== j && list[i].ip == list[j].ip) { var ip = list[i].ip; if (typeof dupes[ip] == 'undefined') { dupes[ip] = {}; dupes[ip][i] = list[i].timestamp; }                               dupes[ip][j] = list[j].timestamp; delete list[j]; }                       }                    }                }            }            chatconnect.dupes = dupes; chatconnect.users = users; chatconnect.banned; },       banned: function { var xhr = new XMLHttpRequest; xhr.open('GET', '/api.php?action=query&list=logevents&letype=chatban&lelimit=100&format=json', true); xhr.onload = function { var users = JSON.parse(xhr.responseText).query.logevents.filter(function(obj) {                       return chatconnect.users.indexOf(obj.title.substr(5)) > -1;                }), banned = {}; for (var i = 0; i < users.length; i++) { banned[users[i].title.substr(5)] = new Date(new Date(users[i].timestamp).getTime + (users[i][4] * 1000)).getTime > new Date.getTime; }               chatconnect.generate(banned); };           xhr.send; },       generate: function(banned) { var dupes = chatconnect.dupes; require(['wikia.nirvana'], function(nirvana) {               nirvana.sendRequest({ controller: 'ChatRail', type: 'GET', method: 'executeGetUsers', callback: function(data) { var inchat = []; for (var i in data.users) { if (data.users.hasOwnProperty(i)) { inchat.push(data.users[i].username); }                       }                        var html = ' '; for (i in dupes) { if (dupes.hasOwnProperty(i)) { html += ' ' + i + ' (whois)'; for (var j in dupes[i]) { if (dupes[i].hasOwnProperty(j)) { var classes = []; for (var k = 0; k < inchat.length; k++) { if (j == inchat[k]) { classes.push('inchat'); break; }                                       }                                        for (var l in banned) { if (j == l && banned[l]) { classes.push('banned'); }                                       }                                        var enc = mw.util.wikiUrlencode(j); html += ' ' + dupes[i][j] + ' ' + j + ' (wall | contribs | check | bans | blocks</a>)</li>'; }                               }                                html += ' </ul></li>'; }                         }                        html += ' </ul>'; if (!document.getElementById('chat-connect')) { document.querySelectorAll('#mw-content-text > ul')[0].insertAdjacentHTML('beforebegin', ' Breakdown A total of 0 duplicate IP entries were found.  </ul> Chat connection log '); }                       document.getElementById('dupe-count').textContent = Object.keys(dupes).length; document.getElementById('dupes').outerHTML = html; document.title = '[' + Object.keys(dupes).length + '] | ' + chatconnect.title; setTimeout(chatconnect.update, 30000); }               });            });         },        update: function { document.title = '[ ... ] | ' + chatconnect.title; var xhr = new XMLHttpRequest; xhr.open('GET', window.location.href, true); xhr.onload = function { if (xhr.status == 200) { var doc = (function(str) {                       var temp = document.implementation.createHTMLDocument;                        temp.body.innerHTML = str;                        return temp;                    })(xhr.responseText); var list = document.querySelector('#mw-content-text > ul'); list.insertAdjacentHTML('afterEnd', doc.querySelector('#mw-content-text > ul').outerHTML); list.parentNode.removeChild(list); chatconnect.process(chatconnect.get(doc)); }           };            xhr.send; },       init: function { chatconnect.css; chatconnect.process(chatconnect.get); }    };    if (mw.config.get('wgTitle') == 'Log/chatconnect') { chatconnect.init; }