User:MACH-59330/chat.js

// chat imports importArticles({	type: 'script',	articles: [		'u:kocka:MediaWiki:Emoticons/code.js',		// 'u:dev:Colors/code.js'	] }); // give chat a wiki-specific class document.body.classList.add('wiki-' + wgDBname); // mobile chatskin meta tag $('head').append(mw.html.element('meta', { name: 'viewport', content: 'width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0' })); // HTML5 desktop notifications +ping class & PM notifications, forked from TK-999 and Noreplyz require(['wikia.window', 'mw'], function (context, mw) {	if (!context.Notification) {		return;	}	// main room ping variables	var pings = new RegExp('(' + ['Speed', 'help', 'CSS', 'template', 'infobox', 'Vanguard'].join('|') + ')', 'gi'),		notification = document.createElement('audio');		notification.src = 'http://images.wikia.nocookie.net/speedit/images/0/01/Notification.ogg';		notification.id = 'ChatNotification';		notification.style.display = 'none';	document.body.appendChild(notification);	// main room notification ping function	function main(message) {		var data = JSON.parse(message.data).attrs;		if (data.isInlineAlert || !pings.test(data.text) || data.name == wgUserName ) {			return;		}		var mainconfig = {			body: data.text,			icon: data.avatarSrc.replace(/28(?!.*28)/, '150')		};		document.querySelector('#Chat_' + window.mainRoom.roomId + ' li:last-of-type').classList.add('ping');		notification.play; if ( !document.hasFocus ) { var ping = new context.Notification(data.name, mainconfig); setTimeout(function { ping.close; }, 5000); }	}	// pm notification function function priv(cwpm) { var privateRoomId = cwpm.attributes.roomId; var privateRoom = window.mainRoom.chats.privates[privateRoomId]; privateRoom.model.chats.bind('afteradd', function(cwmsg) {			user = cwmsg.attributes.name;			if ( user === wgUserName || document.getElementById('Chat_' + privateRoomId).style.display !== 'none' && document.hasFocus ) return;			msg = cwmsg.attributes.text;			var obj = mainRoom.model.users.findByName(name);			avatar = cwmsg.attributes.avatarSrc.replace(/28(?!.*28)/, '150');			pmtitle = "PM from " + user;			var pmconfig = {				body: msg,				icon: avatar			};			var pmping = new context.Notification(pmtitle, pmconfig),				pmnotif = document.getElementById('ChatNotification');			pmnotif.play;			setTimeout(function { pmping.close; }, 5000);		}); }	// unbind listener if the user didn't allow notifications Notification.requestPermission(function (response) {		if (response !== "granted") {			context.mainRoom.socket.unbind('chat:add', main);			context.mainRoom.model.privateUsers.unbind('add', priv);		}	}); context.mainRoom.socket.bind('chat:add', main); context.mainRoom.model.privateUsers.bind('add', priv); });