Mudanças entre as edições de "Widget:Droflax"
Ir para navegação
Ir para pesquisar
| Linha 1: | Linha 1: | ||
<script> | <script> | ||
(function () { | (function () { | ||
// Esperar a que el DOM esté listo | |||
function onReady(fn) { | function onReady(fn) { | ||
if (document.readyState !== 'loading') { | if (document.readyState !== 'loading') { | ||
| Linha 18: | Linha 19: | ||
}; | }; | ||
// Obtener título de página actual | |||
var currentTitle = (window.mw && mw.config && mw.config.get('wgPageName')) || ''; | var currentTitle = (window.mw && mw.config && mw.config.get('wgPageName')) || ''; | ||
if (!currentTitle) { | if (!currentTitle) { | ||
| Linha 28: | Linha 30: | ||
const baseTitle = currentTitle.replace(langRegex, ''); | const baseTitle = currentTitle.replace(langRegex, ''); | ||
// Redirección automática | // Función para consultar si una página existe usando API MediaWiki | ||
function pageExists(title, callback) { | |||
var apiUrl = (window.mw && mw.config && mw.config.get('wgScriptPath') ? mw.config.get('wgScriptPath') : '') + '/api.php'; | |||
var url = apiUrl + '?action=query&titles=' + encodeURIComponent(title) + '&format=json'; | |||
fetch(url) | |||
.then(response => response.json()) | |||
.then(data => { | |||
var pages = data.query.pages; | |||
var pageId = Object.keys(pages)[0]; | |||
// Si pageId es -1 la página no existe | |||
callback(pageId !== '-1'); | |||
}) | |||
.catch(() => { | |||
// En caso de error asumimos que no existe | |||
callback(false); | |||
}); | |||
} | |||
// Si la URL tiene un sufijo de idioma y no es portugués, verificamos si existe la página | |||
if (currentLang !== 'pt') { | |||
pageExists(currentTitle, function (exists) { | |||
if (!exists) { | |||
// Si no existe, redirigir a la versión sin sufijo (la original) | |||
var baseUrl = (window.mw && mw.util && mw.util.getUrl) ? mw.util.getUrl(baseTitle) : '/index.php/' + encodeURIComponent(baseTitle); | |||
// Solo redirigir si no estamos ya ahí para evitar bucles | |||
if (window.location.pathname !== baseUrl) { | |||
window.location.replace(baseUrl); | |||
} | |||
} | |||
}); | |||
} | |||
// CONTINUAR CON LA CREACIÓN DE LOS BOTONES DE IDIOMA | |||
// Redirección automática si usuario eligió idioma y no está en esa versión | |||
const preferedLang = localStorage.getItem('preferedLang'); | const preferedLang = localStorage.getItem('preferedLang'); | ||
const alreadyInLang = currentLang === preferedLang; | const alreadyInLang = currentLang === preferedLang; | ||
const redirected = sessionStorage.getItem('langRedirectDone'); | const redirected = sessionStorage.getItem('langRedirectDone'); | ||
if (preferedLang && !alreadyInLang && !redirected) { | // Importante: Solo hacer esta redirección si ya NO estamos redirigiendo por página inexistente | ||
if (preferedLang && !alreadyInLang && !redirected && (currentLang === 'pt' || currentLang === 'pt')) { | |||
const targetTitle = baseTitle + (preferedLang === 'pt' ? '' : '/' + preferedLang); | const targetTitle = baseTitle + (preferedLang === 'pt' ? '' : '/' + preferedLang); | ||
sessionStorage.setItem('langRedirectDone', '1'); | sessionStorage.setItem('langRedirectDone', '1'); | ||
var | var targetUrl = (window.mw && mw.util && mw.util.getUrl) ? mw.util.getUrl(targetTitle) : '/index.php/' + encodeURIComponent(targetTitle); | ||
window.location.href = | window.location.href = targetUrl; | ||
return; | return; | ||
} | } | ||
// Crear contenedor | // Crear contenedor para los botones de idioma | ||
var container = document.createElement('div'); | var container = document.createElement('div'); | ||
container.className = 'language-buttons'; | container.className = 'language-buttons'; | ||
| Linha 55: | Linha 93: | ||
container.style.zIndex = '999'; | container.style.zIndex = '999'; | ||
// Para cada idioma creamos el botón | |||
langs.forEach(function (lang) { | |||
// Para cada idioma creamos el | |||
langs.forEach(function(lang) { | |||
var pageTitle = baseTitle + (lang === 'pt' ? '' : '/' + lang); | var pageTitle = baseTitle + (lang === 'pt' ? '' : '/' + lang); | ||
var link = document.createElement('a'); | var link = document.createElement('a'); | ||
link.title = lang.toUpperCase(); | link.title = lang.toUpperCase(); | ||
// | // Guardar preferencia al hacer click | ||
link.addEventListener('click', function() { | link.addEventListener('click', function () { | ||
localStorage.setItem('preferedLang', lang); | localStorage.setItem('preferedLang', lang); | ||
sessionStorage.removeItem('langRedirectDone'); | sessionStorage.removeItem('langRedirectDone'); | ||
| Linha 95: | Linha 114: | ||
img.style.objectFit = 'cover'; | img.style.objectFit = 'cover'; | ||
img.style.cursor = 'pointer'; | img.style.cursor = 'pointer'; | ||
img.addEventListener('mouseenter', function() { | img.addEventListener('mouseenter', function () { | ||
img.style.transform = 'scale(1.1)'; | img.style.transform = 'scale(1.1)'; | ||
img.style.borderColor = '#888'; | img.style.borderColor = '#888'; | ||
}); | }); | ||
img.addEventListener('mouseleave', function() { | img.addEventListener('mouseleave', function () { | ||
img.style.transform = 'scale(1)'; | img.style.transform = 'scale(1)'; | ||
img.style.borderColor = (lang === currentLang) ? '#333' : '#ccc'; | img.style.borderColor = (lang === currentLang) ? '#333' : '#ccc'; | ||
}); | }); | ||
// Enlace apuntando directamente, sin verificar existencia, para que el usuario pueda probar. | |||
link.href = (window.mw && mw.util && mw.util.getUrl) ? mw.util.getUrl(pageTitle) : '/index.php/' + encodeURIComponent(pageTitle); | |||
link.appendChild(img); | link.appendChild(img); | ||
container.appendChild(link); | container.appendChild(link); | ||
}); | }); | ||
Edição das 00h16min de 1 de julho de 2025
<script> (function () {
// Esperar a que el DOM esté listo
function onReady(fn) {
if (document.readyState !== 'loading') {
fn();
} else {
document.addEventListener('DOMContentLoaded', fn);
}
}
onReady(function () {
const langs = ['pt', 'en', 'es', 'pl'];
const langFlags = {
pt: 'https://upload.wikimedia.org/wikipedia/en/0/05/Flag_of_Brazil.svg',
en: 'https://upload.wikimedia.org/wikipedia/en/a/a4/Flag_of_the_United_States.svg',
es: 'https://upload.wikimedia.org/wikipedia/en/9/9a/Flag_of_Spain.svg',
pl: 'https://upload.wikimedia.org/wikipedia/en/1/12/Flag_of_Poland.svg'
};
// Obtener título de página actual
var currentTitle = (window.mw && mw.config && mw.config.get('wgPageName')) || ;
if (!currentTitle) {
currentTitle = decodeURIComponent(window.location.pathname.split('/').pop());
}
const langRegex = /\/(pt|en|es|pl)$/; const currentLangMatch = currentTitle.match(langRegex); const currentLang = currentLangMatch ? currentLangMatch[1] : 'pt'; const baseTitle = currentTitle.replace(langRegex, );
// Función para consultar si una página existe usando API MediaWiki
function pageExists(title, callback) {
var apiUrl = (window.mw && mw.config && mw.config.get('wgScriptPath') ? mw.config.get('wgScriptPath') : ) + '/api.php';
var url = apiUrl + '?action=query&titles=' + encodeURIComponent(title) + '&format=json';
fetch(url)
.then(response => response.json())
.then(data => {
var pages = data.query.pages;
var pageId = Object.keys(pages)[0];
// Si pageId es -1 la página no existe
callback(pageId !== '-1');
})
.catch(() => {
// En caso de error asumimos que no existe
callback(false);
});
}
// Si la URL tiene un sufijo de idioma y no es portugués, verificamos si existe la página
if (currentLang !== 'pt') {
pageExists(currentTitle, function (exists) {
if (!exists) {
// Si no existe, redirigir a la versión sin sufijo (la original)
var baseUrl = (window.mw && mw.util && mw.util.getUrl) ? mw.util.getUrl(baseTitle) : '/index.php/' + encodeURIComponent(baseTitle);
// Solo redirigir si no estamos ya ahí para evitar bucles
if (window.location.pathname !== baseUrl) {
window.location.replace(baseUrl);
}
}
});
}
// CONTINUAR CON LA CREACIÓN DE LOS BOTONES DE IDIOMA
// Redirección automática si usuario eligió idioma y no está en esa versión
const preferedLang = localStorage.getItem('preferedLang');
const alreadyInLang = currentLang === preferedLang;
const redirected = sessionStorage.getItem('langRedirectDone');
// Importante: Solo hacer esta redirección si ya NO estamos redirigiendo por página inexistente
if (preferedLang && !alreadyInLang && !redirected && (currentLang === 'pt' || currentLang === 'pt')) {
const targetTitle = baseTitle + (preferedLang === 'pt' ? : '/' + preferedLang);
sessionStorage.setItem('langRedirectDone', '1');
var targetUrl = (window.mw && mw.util && mw.util.getUrl) ? mw.util.getUrl(targetTitle) : '/index.php/' + encodeURIComponent(targetTitle);
window.location.href = targetUrl;
return;
}
// Crear contenedor para los botones de idioma
var container = document.createElement('div');
container.className = 'language-buttons';
container.style.position = 'fixed';
container.style.top = '20px';
container.style.right = '20px';
container.style.display = 'flex';
container.style.gap = '8px';
container.style.background = '#ffffffcc';
container.style.padding = '6px 10px';
container.style.borderRadius = '8px';
container.style.boxShadow = '0 0 6px rgba(0,0,0,0.2)';
container.style.zIndex = '999';
// Para cada idioma creamos el botón
langs.forEach(function (lang) {
var pageTitle = baseTitle + (lang === 'pt' ? : '/' + lang);
var link = document.createElement('a');
link.title = lang.toUpperCase();
// Guardar preferencia al hacer click
link.addEventListener('click', function () {
localStorage.setItem('preferedLang', lang);
sessionStorage.removeItem('langRedirectDone');
});
var img = document.createElement('img');
img.src = langFlags[lang];
img.style.width = '32px';
img.style.height = '24px';
img.style.borderRadius = '6px';
img.style.border = (lang === currentLang) ? '2px solid #333' : '1px solid #ccc';
img.style.transition = 'transform 0.2s ease';
img.style.objectFit = 'cover';
img.style.cursor = 'pointer';
img.addEventListener('mouseenter', function () {
img.style.transform = 'scale(1.1)';
img.style.borderColor = '#888';
});
img.addEventListener('mouseleave', function () {
img.style.transform = 'scale(1)';
img.style.borderColor = (lang === currentLang) ? '#333' : '#ccc';
});
// Enlace apuntando directamente, sin verificar existencia, para que el usuario pueda probar.
link.href = (window.mw && mw.util && mw.util.getUrl) ? mw.util.getUrl(pageTitle) : '/index.php/' + encodeURIComponent(pageTitle);
link.appendChild(img);
container.appendChild(link);
});
document.body.appendChild(container); });
})(); </script>