MediaWiki:Gadget-preserve-variant.js

注意:保存之后,你必须清除浏览器缓存才能看到做出的更改。Google ChromeFirefoxMicrosoft EdgeSafari:按住⇧ Shift键并单击工具栏的“刷新”按钮。参阅Help:绕过浏览器缓存以获取更多帮助。
/**
 * Work around https://phabricator.wikimedia.org/T223053 by rewriting link URLs
 *
 * @Author: Wctaiwan, Lt2818
 * Thanks to User:Legoktm for the idea and for technical assistance, and to
 * User:Jack Phoenix for reviewing the implementation.
 */

(function () {
	var userVariant = mw.config.get('wgUserVariant');
	if (!userVariant || userVariant.indexOf('zh-') !== 0 ||
		window.location.pathname.indexOf('/' + userVariant + '/') !== 0 &&
		new mw.Uri().query.variant !== userVariant
	)
		return;
	$(document).on('keydown mousedown touchstart', 'a', function () {
		var originalHref = $(this).attr('href'),
			uri;
		if (!originalHref)
			return;
		try {
			uri = new mw.Uri(originalHref);
		} catch (e) {
			return;
		}
		if (!('variant' in uri.query))
			if (originalHref.indexOf('/wiki/') === 0)
				this.href = '/' + userVariant + originalHref.slice(5);
			else if (originalHref.indexOf('/w/index.php?') === 0)
				this.href = uri.extend({variant: userVariant}).getRelativePath();
	});
})();