MediaWiki:Common.js: Difference between revisions
No edit summary |
No edit summary |
||
| (2 intermediate revisions by the same user not shown) | |||
| Line 1: | Line 1: | ||
mw.hook('wikipage.content').add(function () { | mw.hook('wikipage.content').add(function () { | ||
console.log("[Language Menu] Script started"); | console.log("[Language Menu] Script started"); | ||
| Line 23: | Line 22: | ||
if (!currentLang) currentLang = 'en'; | if (!currentLang) currentLang = 'en'; | ||
// Labels e links | |||
// Labels | |||
var labels = { | var labels = { | ||
'pt': 'Português', | 'pt': 'Português', | ||
| Line 35: | Line 29: | ||
}; | }; | ||
// | var links = {}; | ||
if (base === 'Main_Page' || base === 'Página_principal') { | |||
links = { | |||
'pt': 'Página_principal/pt', | |||
'en': 'Main_Page', // inglês é base | |||
} | 'es': 'Main_Page/es' | ||
}; | |||
} else { | |||
links = { | |||
'pt': base + '/pt', | |||
'en': base, // inglês é sem sufixo | |||
'es': base + '/es' | |||
}; | |||
} | |||
// Monta menu de idiomas | // Monta menu de idiomas | ||
| Line 47: | Line 49: | ||
if (!el) return; | if (!el) return; | ||
if (lang === currentLang) { | if (lang === currentLang) { | ||
el.textContent = labels[lang]; | el.textContent = labels[lang]; | ||
} else { | } else { | ||
el.innerHTML = '<a class="lang-menu-link" href="' + | el.innerHTML = '<a class="lang-menu-link" href="' + mw.util.getUrl(links[lang]) + '">' + labels[lang] + '</a>'; | ||
} | } | ||
} | } | ||
| Line 59: | Line 60: | ||
insertContent("link-es", "es"); | insertContent("link-es", "es"); | ||
// 🌀 Reescreve links do conteúdo | // 🌀 Reescreve links do conteúdo | ||
console.log("[Language Menu] Rewriting content links for:", currentLang); | console.log("[Language Menu] Rewriting content links for:", currentLang); | ||
document.querySelectorAll('#mw-content-text a[href^="/"]').forEach(function (link) { | //document.querySelectorAll('#mw-content-text a[href^="/"]').forEach(function (link) { | ||
document.querySelectorAll('a[href^="/doc/"]').forEach(function (link) { | |||
if (link.classList.contains('lang-menu-link')) return; // ignora menu de idiomas | |||
var href = link.getAttribute('href'); | var href = link.getAttribute('href'); | ||