MediaWiki:GlobalChatJS.js

/* GlobalChatJS - use Community Central chat.js page globally, like global.js page */

/*jslint browser, this, single */ /*global window, jQuery, mediaWiki, ChatWidget */

(function ($, mw) {   'use strict';

// double-run protection if (window.loadedGlobalChatJS) { return; }   window.loadedGlobalChatJS = true;

var chatWindow;

// hook to import user's chat.js page from Community Central function chatHook { chatWindow.importArticle({           type: 'script',            article: 'u:community:User:' + chatWindow.mediaWiki.config.get('wgUserName') + '/chat.js'        }); }

// original handler: https://github.com/Wikia/app/blob/dev/extensions/wikia/Chat2/js/ChatWidget.js#L207-L219 function chatLinkHandler(linkToSpecialChat) { var chatUrl = linkToSpecialChat && new mw.Uri(linkToSpecialChat);

if (!chatUrl || chatUrl.host !== location.hostname) { // that shouldn't happen… return; }

// always use oasis skin (monobook support) chatUrl.extend({           useskin: 'oasis'        });

// open chat window chatWindow = window.open(chatUrl.toString, 'wikiachat', mw.config.get('wgWikiaChatWindowFeatures'));

// don't hook chat on Community Central wiki, else JS may be loaded twice if (mw.config.get('wgCityId') === '177') { return; }

// add hook to chat window if (chatWindow.addEventListener) { chatWindow.addEventListener('DOMContentLoaded', chatHook, false); } else { // addEventListener isn't immediately available in some browsers (IE) setTimeout(chatHook, 3600); }   }

function main { if (window.ChatWidget && ChatWidget.onClickChatButton) { // oasis - replace existing chat link handler ChatWidget.onClickChatButton = chatLinkHandler; } else { // other skins - add chat link handler // copy of: https://github.com/Wikia/app/blob/dev/extensions/wikia/Chat2/js/ChatWidget.js#L14-L18 $('body').on('click', '.WikiaChatLink', function (event) {               event.preventDefault;                event.stopPropagation;                chatLinkHandler(this.href);            }); }   }

$(main);

}(jQuery, mediaWiki));