User:SunAfterRain/js/redirect.js
外观
< User:SunAfterRain | js
注意:保存之后,你必须清除浏览器缓存才能看到做出的更改。Google Chrome、Firefox、Microsoft Edge及Safari:按住⇧ Shift键并单击工具栏的“刷新”按钮。参阅Help:绕过浏览器缓存以获取更多帮助。
// <nowiki>
$.when(
$.ready,
mw.loader.using(['mediawiki.util']),
(() => {
for (const rlName of ['ext.gadget.HanAssist', 'userscript.SunAfterRain.HanAssist']) {
if (mw.loader.getState(rlName)) {
return mw.loader.using(rlName).then(require => require(rlName));
}
}
return new Promise((resolve) => {
mw.hook('userscript.SunAfterRain.HanAssist.ready').add(function handler(HanAssist) {
mw.hook('userscript.SunAfterRain.HanAssist.ready').remove(handler);
resolve(HanAssist);
});
});
})()
).then((_$, _require, HanAssist) => {
const api = new mw.Api();
const summarySuffix = window.location.host === 'test.strore.xyz' ?
' via. [[User:SunAfterRain/js/redirect.js|redirect.js]]' :
' via. [[:mw:zh:User:SunAfterRain/js/redirect.js|redirect.js]]';
const messages = HanAssist.batchConv({
buttonRedirectTo: { hans: '重定向至…', hant: '重新導向至…' },
buttonRedirectFrom: { hans: '重定向自…', hant: '重新導向自…' },
titleRedirectTo: { hans: '将页面重定向至此', hant: '將頁面重新導向至此' },
titleRedirectFrom: { hans: '将此页重定向至其他页面', hant: '將此頁重新導向至其他頁面' },
labelRedirectTo: { hans: '请输入要重定向至此的目标页面名:', hant: '請輸入要重新導向至此的目標頁面名:' },
labelRedirectFrom: { hans: '请输入重定向的目标页面名:', hant: '請輸入重新導向的目標頁面名:' },
placeholderTargetTitle: { hans: '目标页面名', hant: '目標頁面名' },
labelRedirectTemplate: { hans: '要插入的重新导向模板,留空以自动检测:', hant: '要插入的重新導向模板,留空以自動檢測:' },
labelDetectRedirectTemplate: { hans: '加载现有模板', hant: '載入現有模板' },
labelSummary: { hans: '编辑摘要:', hant: '編輯摘要:' },
defaultSummary: { hans: '重定向页面至 [[$1]]', hant: '重新導向至 [[$1]]' },
confirmMismatchContentModel: { hans: '页面“$1”的内容模型与当前页面不同,是否强行覆盖?', hant: '页面「$1」的内容模型与当前页面不同,是否强行覆盖?' },
warningTitleNotARedirect: { hans: '页面“$1”不是一个重定向,无法检索重定向模板。', hant: '頁面「$1」不是一個重新導向,無法檢索重新導向模板。' },
warningUnrecognizeRedirect: { hans: '无法识别页面“$1”的重定向语法,这真的是一个 wikitext 重定向?', hant: '無法識別頁面「$1」的重定向語法,這真的是一個 wikitext 重定向?' },
warningMismatchContentModel: { hans: '页面“$1”的内容模型与当前页面不同。', hant: '页面「$1」的内容模型与当前页面不同。' },
errorMismatchContentModel: { hans: '页面“$1”的内容模型与当前页面不同,然而你似乎没有权限可以覆盖。', hant: '頁面「$1」的內容模型與當前頁面不同,然而你似乎沒有權限可以覆蓋。' },
});
const makeRedirectContent = {
/**
* @param {mw.Title} destination
* @param {string} text
*/
css(destination, text) {
// MediaWiki\Content\CssContentHandler::makeRedirectContent
let url = mw.util.getUrl(destination.getPrefixedText(), {
action: 'raw',
ctype: 'text/css'
});
// CSSMin::buildUrlValue
if (/^[\w\d:@/~.%+;,?&=-]+$/.test(url)) {
url = `url(${url})`;
} else {
url = url.replaceAll('\\', '\\\\').replaceAll('"', '\\"');
url = `url("${url}")`;
}
return `/* #REDIRECT */@import ${url};`;
},
/**
* @param {mw.Title} destination
* @param {string} text
*/
'sanitized-css'(destination, text) { return css(destination, text) },
/**
* @param {mw.Title} destination
* @param {string} text
*/
javascript(destination, text) {
// MediaWiki\Content\JavaScriptContentHandler::makeRedirectContent
let url = mw.util.getUrl(destination.getPrefixedText(), {
action: 'raw',
ctype: 'text/javascript'
});
return `/* #REDIRECT */mw.loader.load(${JSON.stringify(url)});`;
},
/**
* @param {mw.Title} destination
* @param {string} text
*/
scribunto(destination, text) {
// MediaWiki\Extension\Scribunto\ScribuntoContentHandler::makeRedirectContent
// MediaWiki\Extension\Scribunto\Engines\LuaCommon\LuaEngine::makeRedirectContent
return `return require [[${destination.getPrefixedText()}]]`;
},
/**
* @param {mw.Title} destination
* @param {string} text
*/
wikitext(destination, text) {
// MediaWiki\Content\WikitextContentHandler::makeRedirectContent
let optionalColon = '';
if (destination.getNamespaceId() === 14 /*NS_CATEGORY*/) {
optionalColon = ':';
}
let redirectText = `#REDIRECT [[${optionalColon}${destination.getPrefixedText()}]]`;
if (text) {
redirectText += `\n${Text}`;
}
return redirectText;
}
};
/**
* 利用解析器函數 {{#contentmodel:}} 繞過 api 無法取得不存在頁面的預設內容模型的窘境
*
* @param {mw.Title} title
* @return {Promise<string>}
*/
async function getDefaultContentModelForPage(title) {
const result = await api.parse(`{{safesubst:#contentmodel:canonical|${title.getPrefixedText()}}}`, {
contentmodel: 'wikitext',
onlypst: 1,
});
if (result.startsWith('<')) {
throw new Error('Failed to parse api response: ' + result);
} else if (result.startsWith('{{')) {
throw new Error('Failed to invoke #contentmodel; is the magic word disabled on this wiki?');
}
return result;
}
});
// </nowiki>