Jump to content

User:Kephir/global.js

From Meta, a Wikimedia project coordination wiki

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
mw.loader.using(['user', 'mediawiki.util', 'mediawiki.api'], function () {

window.LocalComments = {
	dateDifference: true,
	dateFormat: 'dmy',
	timeFirst: false,
	twentyFourHours: true,
	dayOfWeek: true,
	dropDays: 90,
	dropMonths: 12
};

if (mw.util.getParamValue('$VANILLA'))
	return;

var maxage = mw.util.getParamValue('debug') ? '' : '&maxage=86400&smaxage=7200';

/* [[w:User:Kephir/gadgets/unclutter]] (some information might not be applicable) */
mw.loader.load('//en.wikipedia.org/w/index.php?title=User:Kephir/gadgets/unclutter.js&action=raw&ctype=text/javascript' + maxage);
/* [[w:User:Kephir/gadgets/hollis]] */
mw.loader.load('//en.wikipedia.org/w/index.php?title=User:Kephir/gadgets/hollis.js&action=raw&ctype=text/javascript' + maxage);
/* [[w:Wikipedia:User script sandbox]] */
mw.loader.load('//en.wikipedia.org/w/index.php?title=User:Kephir/gadgets/jssand.js&action=raw&ctype=text/javascript' + maxage);
/* [[w:User:Kephir/gadgets/preview]] */
mw.loader.load('//en.wikipedia.org/w/index.php?title=User:Kephir/gadgets/preview.js&action=raw&ctype=text/javascript' + maxage);
/* Comments in Local Time */
mw.loader.load('//en.wikipedia.org/w/index.php?title=User:Gary/comments_in_local_time.js&action=raw&ctype=text/javascript' + maxage);
/* ⚓ */
if (mw.config.get('wgServer') !== '//en.wiktionary.org')
mw.loader.load('//en.wiktionary.org/w/index.php?title=MediaWiki:Gadget-U2693.js&action=raw&ctype=text/javascript' + maxage);

// Alt+; to substitute immediately
function installImmedSubst(textbox, getTitle) {
	var link = document.createElement('a');
	var api = new mw.Api();
	link.accessKey = ';';
	link.addEventListener('click', function (ev) {
		var selected = textbox.value.slice(textbox.selectionStart, textbox.selectionEnd);
		var before = textbox.value.substr(0, textbox.selectionStart);
		var after = textbox.value.substr(textbox.selectionEnd);
		var start = textbox.selectionStart;
		var scroll = textbox.scrollTop;
		
		textbox.readOnly = true;
		api.post({
			action: 'parse',
			onlypst: 1,
			text: selected,
			title: getTitle ? getTitle() : '',
		}).then(function (resp) {
			textbox.readOnly = false;
			var text = resp.parse.text['*'];

			textbox.value = before + text + after;
			setTimeout(function() {
				// WebKit doesn't like selecting text immediately after setting .value,
				// seems the actual field value is updated after the script exits.
				textbox.setSelectionRange(start, start + text.length);
				textbox.scrollTop = scroll;
			}, 0);
		}, function () {
			textbox.readOnly = false;
		});
	}, false);
	document.body.appendChild(link);
}

if ((mw.config.get('wgAction') === 'edit') || (mw.config.get('wgAction') === 'submit')) {
	installImmedSubst(document.getElementById('wpTextbox1'), function () {
		return mw.config.get('wgPageName');
	});
} else if (mw.config.get('wgCanonicalSpecialPageName') === 'ExpandTemplates') {
	installImmedSubst(document.getElementById('wpInput'), function () {
		return document.getElementById('contexttitle').value || 'Special:ExpandTemplates';
	});
} else if (window.liquidThreads) {
	mw.hook('ext.lqt.textareaCreated').add(function ($textbox) {
		installImmedSubst(document.getElementById('wpTexbox1'), void(0)); /* even LQT itself cannot get it right, so I will not bother either */
	});
}

});