User:Ultimate Dark Carnage/chat.js

var ToolbarItems = { 'Rules': 'http://community.wikia.com/wiki/Chat_Guidelines' };

importArticles({   type: 'script',    articles: [        //'u:dev:Colors/code.js',        'u:dev:IsTyping/code.js'    ] });

(function(mw, $, mainRoom, factory){   var global = typeof window == 'undefined' ? window : this,        config = typeof window.FandomizedChatConfig == 'object' ? window.FandomizedChatConfig : {};    if (mw.config.get('wgCanonicalSpecialPageName') === 'Chat'){        if (typeof module == 'object' && typeof module.exports == 'object'){            module.exports = global.document ? factory(mw, $, mainRoom) : function(w){                if (!w.docoument){                    throw new Error('This script requires a window with a document.');                }                return factory(mw, $, mainRoom, config);            };        } else {            factory(mw, $, mainRoom, config);        }    } }(this.mediaWiki, this.jQuery, this.mainRoom, function(mw, $, mainRoom, config){ var main = {}; /**    * @method importResources * @param resources - The resources that will be imported **/   main.importResources = function(resources){ var scripts = [], styles = []; $.each(resources, function(index, resource){           if (resource.endsWith('.css')) styles[styles.length] = resource;            else if (resource.endsWith('js')) scripts[scripts.length] = resource;        }); importArticles(           { type: 'style', articles: styles },            { type: 'script', articles: scripts }        ); };   main.importResources([        // Styles        'u:dev:FandomizedChat/code.css',        // Scripts        'u:dev:Colors/code.js',        'u:dev:FandomizedChat/library.2.js'    ]); main.scrollSpeed = typeof config.scrollSpeed == 'number' ? config.scrollSpeed : 80; main.i18n = $.extend(true, {       en: {            hideChatList: 'Hide Chat List',            showChatList: 'Show Chat List',            away: 'Away',            here: 'Here'        }    }, config.i18n || {}); main.msg = main.i18n[mw.config.get('wgUserLanguage')] || main.i18n[mw.config.get('wgUserLanguage').split('-')[0]] || main.i18n.en; main.createArrows = function{ var arrow = { up: $('', {               'class': 'list-arrow up-arrow',                'id': 'list-arrow-up',                'href': '#WikiChatList',                html: '',                on: {                    'click': main.moveUp                }            }), down: $('', {               'class': 'list-arrow down-arrow',                'id': 'list-arrow-down',                'href': '#WikiChatList',                html: '',                on: {                    'click': main.moveDown                }            }) };       return arrow; };   main.createToggleButton = function{ var toggleButton = $('', {           'class': 'toggleChatList show',            'id': 'toggleChatList',            'href': '#WikiChatList',            html: [main.msg.hideChatList, '']        }); toggleButton.on('click', main.toggleChatList); return toggleButton; };   main.moveUp = function(event){ event.preventDefault; var $link = $(event.target).is('#list-arrow-up') ? $(event.target) : $(event.target).parents('#list-arrow-up'), $target = $($link.prop('hash')); $target.animate({ scrollTop: '-=' + main.scrollSpeed + 'px' }, {           step: function(value){                var rounded = Math.round(value);                if ($(this).scrollTop === 0){                    $(this).stop;                }            }        }); };   main.moveDown = function(event){ event.preventDefault; var $link = $(event.target).is('#list-arrow-down') ? $(event.target) : $(event.target).parents('#list-arrow-down'), $target = $($link.prop('hash')); $target.animate({ scrollTop: '+=' + main.scrollSpeed + 'px' }, {           step: function(value){                if ($(this).scrollTop + $(this).innerHeight > $(this).prop('scrollHeight'))                    $(this).stop;            }        }); };   main.toggleChatList = function(event){ event.preventDefault; var $link = $(event.target).is('#toggleChatList') ? $(event.target) : $(event.target).parents('#toggleChatList'), $target = $($link.prop('hash')); if ($target.is(':hidden')){ $link.html([main.msg.hideChatList, '']); $target.slideDown('fast'); } else { $link.html([main.msg.showChatList, '']); $target.slideUp('fast'); }   };    main.init = function{ var $arrows = $.map(main.createArrows, function(value){               return value;            }), $toggle = main.createToggleButton; $('#Rail').prepend($('#ChatHeader .User').clone.addClass('self')); $('#WikiChatList').before($toggle, $arrows[0]); $('#WikiChatList').after($arrows[1]); mw.hook('FandomizedChat.loaded').fire(main); };   $(main.init); window.FandomizedChatMain = window.fcMain = main; }));

(function(mw, $, chat_logger){	var ChatLogger = Object.create(null);	function createCookieString(cookie_obj, parse_function){		var cookie_keys = Object.keys(cookie_obj),			res = null;		function mapItems(key, index){			var item = cookie_obj[key], string = '';			if (typeof parse_function == 'function'){				string = parse_function.apply(this, [typeof item !== 'object' ? item : '', key, index, cookie_keys, typeof item == 'object' ? item : {}]);			} else {				if (index < (cookie_keys.length - 1)){					string = key + ':' + item + ';';				} else {					string = key + ':' + item;				}			}			return string || ;		}		var mapKeys = mapItems.bind(ChatLogger);		res = cookie_keys.map(mapKeys).filter(function(item){ return item !==  });		return res.join(' ');	}	function setCookie(name, value, expiry){		var expiry_date = new Date,			target_date = parseInt(expiry_date.getDate + expiry, 10);		expiry_date.setDate(target_date);		var cookie_obj = Object.defineProperty({}, name, { configurable: true, writable: true, enumarable: true, value: value });		cookie_obj['expires'] = {			condition: ['string', 'number'].indexOf(typeof expiry) > -1 && expiry !== null,			value: expiry		};		document.cookie = createCookieString(cookie_obj, function(item, key, index, object, config){ var keys = Object.keys(object), isLast = (index === (keys.length - 1)), res = ''; if (typeof config == 'object'){ if (typeof config.condition == 'true' && config.condition === true){ if (!isLast){ res = key + '=' + encodeURIComponent(config.value) + ';'; } else { res = key + '=' + encodeURIComponent(config.value); }					return res; }				return ''; } else { if (!isLast){ res = key + '=' + encodeURIComponent(item) + ';'; } else { res = key + '=' + encodeURIComponent(item); }				return res || ''; }		});	}	function getCookie(name){		if (document.cookie.length > 0){			var start = document.cookie.indexOf(name + '=');			if (start !== -1){				start = start + name.length + 1;				var end = document.cookie.length(';', start);				if (end === -1){					end = document.cookie.length;				}				return decodeURIComponent(document.cookie.substring(start, end));			}		}		return '';	}	ChatLogger.pad = function(n, base){		base = typeof base !== 'undefined' ? base : 10;		if (!isNaN(n)){			if (parseInt(n, base) < base){				return '0' + n;			}			return n;		}		return NaN;	};	ChatLogger.convert = function(time, name){		var time_object = {			'h': function(n){				return n * 60 * 60 * 1000;			},			'm': function(n){				return n * 60 * 1000;			},			's': function(n){				return n * 1000;			}		};		if (time_object.hasOwnProperty(name)){			return time_object[name].apply(this, [parseInt(time, 10)]);		}	};	ChatLogger.date = new Date; ChatLogger.curTime = [null, null, null]; ChatLogger.curTime[0] = ChatLogger.date.getUTCHours; ChatLogger.curTime[1] = ChatLogger.date.getUTCMinutes; ChatLogger.curTime[2] = ChatLogger.date.getUTCSeconds; ChatLogger.timeTillMidnight = (ChatLogger.convert((23 - ChatLogger.curTime[0]), 'h')) + (ChatLogger.convert((59 - ChatLogger.curTime[1]), 'm')) + (ChatLogger.convert((60 - ChatLogger.curTime[2]), 's')); setTimeout('setInterval("ChatLogger.submitLog", ' + ChatLogger.convert(24, 'h') + ')', ChatLogger.timeTillMidnight); setTimeout('ChatLogger.submitLog', ChatLogger.timeTillMidnight); ChatLogger.logInterval = ChatLogger.logInterval ? ChatLogger.logInterval : ChatLogger.convert(60, 'm'); ChatLogger.toUTCTime = function(string){ var time_zone = d.getTimezoneOffset/60, hour = parseInt(string.split(':')[0]), UTChour = hour-zime_zone; if (UTChour < 1){ UTChour = 12 + UTChour; } else if (UTChour > 12){ UTChour = UTChour - 12; }		var UTCTime = this.pad(UTChour) + ':' + string.split(':')[1] + (string.split(':').length > 2 ? ':' + string.split(':')[2] : ''); return UTCTime; };	ChatLogger.getLog = function(exists, content){ var messages = $('.Chat').find('.message'), message = messages.eq(0).html, msguser = messages.eq(0).closest('li').data('user') || messages.eq(0).closest('li').attr('data-user'), msgtime = this.toUTCTime(messages.eq(0).closest('li').find('.time').html), i; for (i = 1; typeof messages[i] !== 'undefined'; i++){ message += '<|>' + messages.eq(i).html; msguser += '<|>' + messages.eq(i).closest('li').data('user') || messages.eq(i).closest('li').attr('data-user'); msgtime += this.toUTCTime(messages.eq(i).closest('li').find('.time').html); }		message = message.split('<|>'); msguser = msguser.split('<|>'); msgtime = msgtime.split('<|>'); var ChatLog = ''; for (i = 0; typeof message[i] !== 'undefined'; i++){ ChatLog += '' + ' ' + msgtime[i] + ' ' + ' ' + msguser[i] + ' ' + ' ' + message[i] + ' ' + ''; }		var wikilinks = new RegExp('<a href="' + mw.config.get('wgServer') + '/wiki/([^"]*)">([^<]*)</a>', 'g'),			patterns = [				[/(\s|\t){2,}/g, ' '],				[/<img src="[^"]+".*? alt="([^"]+)"[^>]*>/g, '$1'],				[wikilinks, function(match, page, title){ return  + title + ; }],				[/\[\[([^\]]*?) {2,}/g, '$1'],				[/\[\[([^|]*)\|\1\]\]/g, '$1'],				[/<a href="([^"]+)">[^<]*<\/a>/g, '$1'], ['' + mw.config.get('wgServer') + '/wiki/', mw.config.get('wgServer') + '/wiki/'] ];		for (var j = 0; j < patterns.length; j++){ var pattern = patterns[j][0], result = patterns[j][1]; ChatLog = ChatLog.replace(pattern, result); }		var res = exists ? content.replace(' ', ChatLog + ' ') : ' \n' + ChatLog + ' \n'; return res; };	ChatLogger.submitLog = function(restart){ restart = restart ? restart : false; var date = new Date, monthNames = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'], d = date.getUTCDate + '_' + monthNames[date.getUTCMonth] + '_' + date.getUTCFullYear, that = this; $.ajax({			method: 'GET',			dataType: 'json',			url: mw.config.get('wgScriptPath') + '/api.php',			data: {				'action': 'query',				'prop': 'info|revisions',				'intoken': 'edit',				'titles': 'User:Ultimate_Dark_Carnage/Chat/Logs/' + d,				'rvprop': 'content',				'rvlimit': '1',				'indexpageids': 'true',				'format': 'json'			},			complete: function(response){				console.log(response);				var page = response.query.pages[response.query.pageids[0]],					pageExists = response.query.pages['-1'] ? false : true,					content = typeof page['revisions'] !== 'undefined' ? page.revisions[0]['*'] : '';				$.ajax({ method: 'POST', dataType: 'json', url: mw.config.get('wgScriptPath') + '/api.php', data: { 'minor': 'yes', 'bot': 'yes', 'summary': '', 'action': 'edit', 'title': 'User:Ultimate_Dark_Carnage/Chat/Logs/' + d,						'starttimestamp': page.starttimestamp, 'token': page.edittoken, 'text': that.getLog(pageExists, content), 'format': 'json' },					complete: function{ if (restart === true){ window.location.reload(true); }					}				});			}		});	};	window.ChatLogger = ChatLogger; })(this.mediaWiki, this.jQuery, typeof this.WikiChatLogger == 'object' ? this.WikiChatLogger : {});