Участник:Jack who built the house/canonicalPageName.js
Перейти к навигации
Перейти к поиску
if (mw.config.get('wgNamespaceNumber')) {
mw.loader.using([
'mediawiki.util',
'mediawiki.Title',
'mediawiki.Uri',
]).then(function () {
function getCanonicalPageName() {
const page = mw.Title.newFromText(mw.config.get('wgPageName'));
const title = mw.config.get('wgCanonicalSpecialPageName') ?
mw.config.get('wgCanonicalSpecialPageName') + page.title.replace(/^[^/]+/, '') :
page.title;
return mw.config.get('wgCanonicalNamespace') + ':' + title;
}
function setUrl(path) {
history.replaceState(history.state, '', path);
}
if (location.pathname.startsWith(mw.util.getUrl())) {
const pathName = mw.config.get('wgArticlePath')
.replace('$1', mw.util.wikiUrlencode(getCanonicalPageName()));
setUrl(pathName + location.search + location.hash);
} else if (location.pathname.startsWith(mw.config.get('wgScriptPath'))) {
const uri = new mw.Uri();
if (uri.query.title) {
uri.query.title = getCanonicalPageName();
setUrl(uri.getRelativePath());
}
}
});
}