User:WhitePhosphorus/js/MagicRedirect.js

维基百科,自由的百科全书

注意:保存之后,你必须清除浏览器缓存才能看到做出的更改。Google ChromeFirefoxMicrosoft EdgeSafari:按住⇧ Shift键并单击工具栏的“刷新”按钮。参阅Help:绕过浏览器缓存以获取更多帮助。

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

$.getScript(`https://zh.wikipedia.org/w/index.php?title=User:${mw.config.get("wgUserName")}/MagicRedirectConfig.js&action=raw&ctype=text/javascript`).done(function() {

let pageName = mw.config.get('wgPageName');
const prefix = 'Special:';
if (!pageName.startsWith(prefix)) return;  // deal with special pages only
let newPageName = pageName.substr(prefix.length);
if (!window.P4js || !window.P4js.magicRedirect) return;
let rules = window.P4js.magicRedirect.rules;
if (!rules) return;
for (let replacement of rules) {
	let [oldpattern, newpattern] = replacement;
	let willReplace = false;
	if ((typeof oldpattern === 'string' && newPageName.startsWith(oldpattern)) ||
			(typeof oldpattern === 'object')) {
		newPageName = newPageName.replace(oldpattern, newpattern);	
	}
}
if (prefix + newPageName != pageName) {
	let url = mw.config.get('wgScript') + '?title=' + encodeURIComponent(prefix + newPageName);
	mw.notify('Redirecting to ' + mw.html.escape(url));
	window.location.href = url;
}

});

})(jQuery, mediaWiki);