User:魔琴/gadgets/flip-lang/index.js
外观
注意:保存之后,你必须清除浏览器缓存才能看到做出的更改。Google Chrome、Firefox、Microsoft Edge及Safari:按住⇧ Shift键并单击工具栏的“刷新”按钮。参阅Help:绕过浏览器缓存以获取更多帮助。
$(document).keydown(function (e) {
mw.loader.using(['mediawiki.util'], function () {
if (mw.config.get('wgAction') === 'edit') {
return;
}
if (e.ctrlKey && e.altKey && e.shiftKey && e.code === 'KeyI') {
var lang = prompt('Language code').trim().toLowerCase();
const ns = mw.config.get('wgNamespaceNumber');
var pagename = mw.config.get('wgPageName');
if (ns === -1) {
if (lang === "mos") {
lang = "fr:mos"; // same as above
}
const specialPageName = mw.config.get('wgCanonicalSpecialPageName');
const url = new URL(window.location);
var newTitle = lang + ":Special:" + specialPageName;
if (url.pathname.endsWith('index.php')) {
url.searchParams.set('title', newTitle);
} else {
const parts = url.pathname.split('/');
parts[2] = encodeURIComponent(newTitle);
url.pathname = parts.join('/');
}
window.open(url.toString());
} else if ((ns === 2 || ns === 3 )&& !/\//.test(pagename)) {
canonicalUserPrefix = ns===2 ? ':User:' : ':User talk:';
if (lang === "mos") {
lang = "fr:mos"; // I hope frwiki won't come up with a mos: pseudo-namespace
}
var newPath = lang + canonicalUserPrefix + mw.config.get('wgRelevantUserName');
window.open('/wiki/' + newPath);
} else {
normal = normalizeLang(lang);
useSkin = mw.config.get('skin');
var langSection = getLangSection(useSkin);
iwSelect = langSection + ' .interwiki-' + normal + ' a';
target = $(iwSelect).attr('href');
if (target === undefined) {
if (normal !== lang) {
lang = '"' + normal + '" (' + lang + ')';
} else {
lang = '"' + lang + '"';
}
mw.notify('Fail to find the page in ' + lang + '.',{type:"error"});
if ($(langSection).length === 0 && useSkin === "vector-2022") {
mw.notify('Vector 2022 users should install 魔琴\'s legacy language bar gadget.',{type:"error"});
}
return;
}
window.open($(iwSelect).attr('href'));
}
}
function normalizeLang (code) {
const langMap = {
'gsw': 'als',
'sgs': 'bat-smg',
'vro': 'fiu-vro',
'nrf': 'nrm',
'rup': 'roa-rup',
'en-simple': 'simple',
'lzh': 'zh-classical',
'nan': 'zh-min-nan',
'yue': 'zh-yue',
'mo': 'ro',
'be-x-old': 'be-tarask',
'nb': 'no'
};
const key = (code || '').trim().toLowerCase();
return langMap[key] || key;
}
function getLangSection (skin) {
const elementMap = {
'vector': '#p-lang',
'vector-2022': '#legacy-lang-menu-container', // [[User:魔琴/gadgets/legacy-2022#经典语言栏]]
'timeless': '#p-lang',
'monoboook': '#p-lang',
'modern': '#p-lang',
'cologneblue': '#langlinks',
'minerva': '#p-lang',
'': '#p-lang'
};
const key = (skin || '');
return elementMap[key] || key;
}
})
});