User:2Actimv/wikia.js

/* chat connect - vanilla version */ 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; this.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) { for (var j in list) { 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]; }               }            }            this.dupes = dupes; this.users = users; this.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 = this.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) { inchat.push(data.users[i].username); }                       var html = ' '; for (var i in dupes) { html += '' + i + ' (whois)'; for (var j in dupes[i]) { 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.pathname, 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); chatconnect.process(chatconnect.get(doc)); }           };            xhr.send; },       init: function { this.css; this.process(this.get); }   };    if (mw.config.get('wgTitle') == 'Log/chatconnect') { chatconnect.init; } });
 * (function {