User:Saftzie/global.js

// define user.util module (window.user = window.user || {}).util = window.user.util || (function (mw, $) {	'use strict';

var wgSkin = mw.config.get('skin'), self = { // try to prevent double loading of scripts preview: function (page) { user.page = user.page || {}; if (user.page[page] !== undefined) { throw new Error('user.util.preview: double loading ' + page); } else { user.page[page] = true; }			},

// execute loop until cond returns true // then call then // time (optional) = # of msec per loop (default: 100) wait: function wait(cond, then, time) { if ((typeof cond !== 'function') || (typeof then !== 'function')) { throw new TypeError('user.util.wait: required wait parameters must be functions'); }				if (cond) { then; } else { time = (typeof time === 'number') ? time : 100; setTimeout(wait, time, cond, then, time); }			},

// add a text notification to the top of the page notify: function (text) { var wrapper = $('.banner-notifications-wrapper'), msg = $(String.prototype.concat( ' ',							'', ' ',							' ',							' ',								text, ' ',						' '					));

if (wrapper.length === 0) { wrapper = $(' '); switch (wgSkin) { case 'monobook': $('#content').prepend(wrapper); break; case 'oasis': $('.WikiaPageContentWrapper').prepend(wrapper); break; default: }				}				msg.find('button').click(function {					msg.remove;				}); wrapper.append(msg); }		};

return self; }(mediaWiki, jQuery));

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

var wgCanonicalSpecialPageName = mw.config.get('wgCanonicalSpecialPageName'), wgNamespaceNumber = mw.config.get('wgNamespaceNumber'), wgSkin = mw.config.get('skin'), blankSpecial, idDiv, js;

// stop execution of this page on diff and preview user.util.preview('Community:User:Saftzie/global.js');

if (mw.util !== undefined) { blankSpecial = mw.util.getParamValue('blankspecial'), idDiv = mw.util.getParamValue('divid'), js = mw.util.getParamValue('js'), }

// redirect Specal:Chat to Oasis (function {		var			q = location.search;

if ((wgCanonicalSpecialPageName === 'Chat') &&			(wgSkin !== 'oasis') && !/[&\?]useskin=/.test(q)) { location.search = q.length ? q + '&useskin=oasis' : '?useskin=oasis'; }	});

// add classes to closed and highlighted thread items if (wgNamespaceNumber === 2000) { $(function {			$('.ThreadList > .thread').each(function  { var li = $(this);

$.post('/wikia.php', { // mw.util.wikiScript('wikia'), {					controller: 'Forum',					method: 'brickHeader',					id: li.data('id'),					format: 'json'				}, function (data) {					if (data.isClosed) {						li.addClass('g-closed');					}					if (data.isNotifyeveryone) {						li.addClass('g-highlight');					}				}); });		});	}

/*  What doesn't work? 1. Monobook edits to threads, the handler is detached after the first edit 2. Oasis new article comments and edits to article comments, nothing happens 3. Oasis edits to global.js and skin.js do not run global.js (Wikia problem) // insert tab for tab key event in edit areas // however, because of Event.timeStamp, //  it's broken in IE 8 and earlier //  and might be broken in Chrome 49 and later $(function {		var			wait = user.util.wait,			t = 0;

// insert a tab function tab(textarea) { var s = textarea.selectionStart, t = textarea.scrollTop, v = textarea.value;

textarea.value = v.substr(0, s) + '\t' + v.substr(textarea.selectionEnd); textarea.scrollTop = t;			textarea.selectionStart = textarea.selectionEnd = s + 1; }

// attach keydown and keypress handlers to DOM elements function handle(select) { $(select).keydown(function (e) {				var					epoch = e.timeStamp;

if (e.keyCode === 9) { e.preventDefault; if (typeof e.stopPropagation === 'function') { // required for replies to Wikia threads e.stopPropagation; }					if (epoch !== t) { tab(this); t = epoch; }				}			}).keypress(function (e) { var epoch = e.timeStamp;

if (e.keyCode === 9) { e.preventDefault; if (epoch !== t) { tab(this); t = epoch; }				}			});		}

if (!document.createEvent || !document.createEvent('HTMLEvents').timeStamp) { // exit if events don't have timestamps return; }		// some things can take key handlers at document.ready handle(			'#wpTextbox1,'  + // articles, includes Wikia blogs			'#Wall .body,'   + // new Wikia threads			'#Wall .replyBody' // new replies to Wikia threads		); // edits to Wikia threads and replies $('a.edit-message').click(function {			var				s = '#' + $(this).closest('li.message')[0].id + ' .body';			wait(function  { return $(s).length > 0; },	function { handle(s); });		});

// article comments need to wait longer if ($('#mw-data-after-content').length > 0) { wait(function {				return $('#article-comm').length > 0;			}, function  {				// new article comments				handle('#article-comm');				// new replies to article comments				$('button.article-comm-reply').click(function  { var s = $(this).closest('li')[0].id							.replace('comm', '#article-comm-reply-textfield');

wait(function {						return $(s).length > 0;					},	function  {						handle(s);					}); });				// edits to Wikia article comments and replies				$('a.article-comm-edit').click(function { var s = this.id							.replace('comment', '#article-comm-textfield-'); wait(function {						return $(s).length > 0;					},	function  {						handle(s);					}); });			});		}	});

// add a div to Special:BlankPage to make other stuff easier if ((wgCanonicalSpecialPageName === 'Blankpage') &&		(blankSpecial === 'userjs')) { if (idDiv !== null) { $('#mw-content-text').empty.append(''); }		if (js !== null) { switch (wgSkin) { case 'monobook': $('#firstHeading').text(js); break; case 'oasis': $('.header-title > h1').text(js); break; default: // anything else }		}	}

// alert when Gadgets are set $.post('/api.php', {		format: 'json',		action: 'query',		meta: 'userinfo',		uiprop: 'options'	}, function (o) {		var			s = '',			i;

if ((o = o.query) && (o = o.userinfo) && (o = o.options)) { for (i in o) { if (o.hasOwnProperty(i) &&					(i.substr(0, 7) === 'gadget-') &&					((o[i] === '1') || (o[i] === 1))) { // 1 if set, false if unset if (s.length) { s += '|'; }					s += i + '=false'; }			}			if (s.length) { /* action=options doesn't like the gadget names $.post('/api.php', {					format: 'json',					action: 'options',					change: s,					token: mw.user.tokens.get('editToken')				}); $(function {					user.util.notify('Gadgets are set');				}); }		}	});

// replace https with http for nocookie.net $('img').each(function (i, e) {		var			r = /^https:\/\/[^\/]+\.nocookie\.net\//i,			src;

if ((src = e.getAttribute('data-src')) && (src.search(r) === 0)) { e.setAttribute('data-src', src.replace(/^https/, 'http')); }		if ((src = e.src) && (src.search(r) === 0)) { e.src = src.replace(/^https/, 'http'); }	}); }(mediaWiki, jQuery));