Mudanças entre as edições de "Widget:Droflax"

De Wiki Gla
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') {
        fn();
      fn();
      } else {
    } else {
        document.addEventListener('DOMContentLoaded', fn);
      document.addEventListener('DOMContentLoaded', fn);
      }
     }
     }
  }


  onReady(function () {
    onReady(function () {
    const langs = ['pt', 'en', 'es', 'pl'];
      const langs = ['pt', 'en', 'es', 'pl'];
    const langFlags = {
      const langFlags = {
      pt: 'https://upload.wikimedia.org/wikipedia/en/0/05/Flag_of_Brazil.svg',
        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',
        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',
        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'
        pl: 'https://upload.wikimedia.org/wikipedia/en/1/12/Flag_of_Poland.svg'
    };
      };


    // Obtener el nombre de la página actual (wgPageName)
      var currentTitle = (window.mw && mw.config && mw.config.get('wgPageName')) || '';
    // MediaWiki inserta una variable global mw.config, sino se puede adaptar
      if (!currentTitle) {
    var currentTitle = (window.mw && mw.config && mw.config.get('wgPageName')) || '';
        currentTitle = decodeURIComponent(window.location.pathname.split('/').pop());
    if (!currentTitle) {
      }
      // fallback, intentar sacar el título de la URL
      currentTitle = decodeURIComponent(window.location.pathname.split('/').pop());
    }


    const langRegex = /\/(pt|en|es|pl)$/;
      const langRegex = /\/(pt|en|es|pl)$/;
    const currentLangMatch = currentTitle.match(langRegex);
      const currentLangMatch = currentTitle.match(langRegex);
    const currentLang = currentLangMatch ? currentLangMatch[1] : 'pt';
      const currentLang = currentLangMatch ? currentLangMatch[1] : 'pt';
    const baseTitle = currentTitle.replace(langRegex, '');
      const baseTitle = currentTitle.replace(langRegex, '');


    // Redirección automática basada en localStorage y sessionStorage
      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) {
      if (preferedLang && !alreadyInLang && !redirected) {
  const targetTitle = baseTitle + (preferedLang === 'pt' ? '' : '/' + preferedLang);
        const targetTitle = baseTitle + (preferedLang === 'pt' ? '' : '/' + preferedLang);
  const baseUrl = (window.mw && mw.util && mw.util.getUrl) ? mw.util.getUrl(targetTitle) : '/index.php/' + encodeURIComponent(targetTitle);
        const baseUrl = (window.mw && mw.util && mw.util.getUrl) ? mw.util.getUrl(targetTitle) : '/index.php/' + encodeURIComponent(targetTitle);


  fetch(baseUrl, { method: 'HEAD' }).then(response => {
        fetch(baseUrl, { method: 'HEAD' }).then(response => {
    sessionStorage.setItem('langRedirectDone', '1');
          sessionStorage.setItem('langRedirectDone', '1');
    if (response.ok) {
          if (response.ok) {
      window.location.href = baseUrl;
            window.location.href = baseUrl;
    } else {
          } else {
      // Fallback a página base si no existe traducción
            // Fallback a página base si no existe traducción
      const fallbackUrl = (window.mw && mw.util && mw.util.getUrl)
            const fallbackUrl = (window.mw && mw.util && mw.util.getUrl)
        ? mw.util.getUrl(baseTitle)
              ? mw.util.getUrl(baseTitle)
        : '/index.php/' + encodeURIComponent(baseTitle);
              : '/index.php/' + encodeURIComponent(baseTitle);
      window.location.href = fallbackUrl;
            window.location.href = fallbackUrl;
    }
          }
  }).catch(() => {
        }).catch(() => {
    // Fallback por error de red
          // Fallback por error de red
    const fallbackUrl = (window.mw && mw.util && mw.util.getUrl)
          const fallbackUrl = (window.mw && mw.util && mw.util.getUrl)
      ? mw.util.getUrl(baseTitle)
            ? mw.util.getUrl(baseTitle)
      : '/index.php/' + encodeURIComponent(baseTitle);
            : '/index.php/' + encodeURIComponent(baseTitle);
    window.location.href = fallbackUrl;
          window.location.href = fallbackUrl;
  });
        });
 
  return; // evitar que el resto del código se ejecute aún
}


        return;
      }


    // Crear contenedor
      var container = document.createElement('div');
    var container = document.createElement('div');
      container.className = 'language-buttons';
    container.className = 'language-buttons';
      container.style.position = 'fixed';
    container.style.position = 'fixed';
      container.style.top = '20px';
    container.style.top = '20px';
      container.style.right = '20px';
    container.style.right = '20px';
      container.style.display = 'flex';
    container.style.display = 'flex';
      container.style.gap = '8px';
    container.style.gap = '8px';
      container.style.background = '#ffffffcc';
    container.style.background = '#ffffffcc';
      container.style.padding = '6px 10px';
    container.style.padding = '6px 10px';
      container.style.borderRadius = '8px';
    container.style.borderRadius = '8px';
      container.style.boxShadow = '0 0 6px rgba(0,0,0,0.2)';
    container.style.boxShadow = '0 0 6px rgba(0,0,0,0.2)';
      container.style.zIndex = '999';
    container.style.zIndex = '999';


    // Crear botones para cada idioma
      // botoes pra cada idioma
    langs.forEach(function(lang) {
      langs.forEach(function (lang) {
  var pageTitle = baseTitle + (lang === 'pt' ? '' : '/' + lang);
        var pageTitle = baseTitle + (lang === 'pt' ? '' : '/' + lang);
  var fullUrl = (window.mw && mw.util && mw.util.getUrl) ? mw.util.getUrl(pageTitle) : '/index.php/' + encodeURIComponent(pageTitle);
        var fullUrl = (window.mw && mw.util && mw.util.getUrl) ? mw.util.getUrl(pageTitle) : '/index.php/' + encodeURIComponent(pageTitle);


  var link = document.createElement('a');
        var link = document.createElement('a');
  link.href = '#'; // evitamos navegación inmediata
        link.href = '#';
  link.title = lang.toUpperCase();
        link.title = lang.toUpperCase();


  link.addEventListener('click', function (e) {
        link.addEventListener('click', function (e) {
  e.preventDefault();
          e.preventDefault();


  fetch(fullUrl, { method: 'HEAD' }).then(response => {
          fetch(fullUrl, { method: 'HEAD' }).then(response => {
  if (response.ok) {
            if (response.ok) {
    localStorage.setItem('preferedLang', lang);
              localStorage.setItem('preferedLang', lang);
    sessionStorage.setItem('langRedirectDone', '1');
              sessionStorage.setItem('langRedirectDone', '1');
    window.location.href = fullUrl;
              window.location.href = fullUrl;
  } else {
            } else {
    sessionStorage.setItem('langRedirectDone', '1');
              sessionStorage.setItem('langRedirectDone', '1');


    if (lang !== 'pt') {
              if (lang !== 'pt') {
      const langNames = { es: 'Español', en: 'Inglés', pl: 'Polaco' };
                const langNames = { es: 'Español', en: 'Inglés', pl: 'Polaco' };
      const msg = document.createElement('div');
                const msg = document.createElement('div');
      msg.textContent = `La versión en ${langNames[lang]} no está disponible.`;
                msg.textContent = `La versión en ${langNames[lang]} no está disponible.`;
      msg.style.position = 'fixed';
                msg.style.position = 'fixed';
      msg.style.top = '20px';
                msg.style.top = '20px';
      msg.style.left = '50%';
                msg.style.left = '50%';
      msg.style.transform = 'translateX(-50%)';
                msg.style.transform = 'translateX(-50%)';
      msg.style.background = '#ffdddd';
                msg.style.background = '#ffdddd';
      msg.style.color = '#a00';
                msg.style.color = '#a00';
      msg.style.padding = '12px 24px';
                msg.style.padding = '12px 24px';
      msg.style.border = '1px solid #a00';
                msg.style.border = '1px solid #a00';
      msg.style.borderRadius = '8px';
                msg.style.borderRadius = '8px';
      msg.style.fontSize = '16px';
                msg.style.fontSize = '16px';
      msg.style.zIndex = '1000';
                msg.style.zIndex = '1000';
      msg.style.boxShadow = '0 0 6px rgba(0,0,0,0.2)';
                msg.style.boxShadow = '0 0 6px rgba(0,0,0,0.2)';
      msg.style.opacity = '0';
                msg.style.opacity = '0';
      msg.style.transition = 'opacity 0.3s ease';
                msg.style.transition = 'opacity 0.3s ease';


      document.body.appendChild(msg);
                document.body.appendChild(msg);
      setTimeout(() => { msg.style.opacity = '1'; }, 10); // Fade in
                setTimeout(() => { msg.style.opacity = '1'; }, 10); // Fade in


      setTimeout(() => {
                setTimeout(() => {
        msg.style.opacity = '0'; // Fade out
                  msg.style.opacity = '0'; // Fade out
        setTimeout(() => {
                  setTimeout(() => {
          msg.remove();
                    msg.remove();
          const fallbackUrl = (window.mw && mw.util && mw.util.getUrl)
                    const fallbackUrl = (window.mw && mw.util && mw.util.getUrl)
            ? mw.util.getUrl(baseTitle)
                      ? mw.util.getUrl(baseTitle)
            : '/index.php/' + encodeURIComponent(baseTitle);
                      : '/index.php/' + encodeURIComponent(baseTitle);
          window.location.href = fallbackUrl;
                    window.location.href = fallbackUrl;
        }, 500); // Espera a que se desvanezca
                  }, 500);
      }, 5000);
                }, 3000);
    } else {
              } else {
      // Redirigir directo sin mensaje
                const fallbackUrl = (window.mw && mw.util && mw.util.getUrl)
      const fallbackUrl = (window.mw && mw.util && mw.util.getUrl)
                  ? mw.util.getUrl(baseTitle)
        ? mw.util.getUrl(baseTitle)
                  : '/index.php/' + encodeURIComponent(baseTitle);
        : '/index.php/' + encodeURIComponent(baseTitle);
                window.location.href = fallbackUrl;
      window.location.href = fallbackUrl;
              }
    }
            }
  }
          }).catch(() => {
}).catch(() => {
            const fallbackUrl = (window.mw && mw.util && mw.util.getUrl)
  const fallbackUrl = (window.mw && mw.util && mw.util.getUrl)
              ? mw.util.getUrl(baseTitle)
    ? mw.util.getUrl(baseTitle)
              : '/index.php/' + encodeURIComponent(baseTitle);
    : '/index.php/' + encodeURIComponent(baseTitle);
            sessionStorage.setItem('langRedirectDone', '1');
  sessionStorage.setItem('langRedirectDone', '1');
            window.location.href = fallbackUrl;
  window.location.href = fallbackUrl;
          });
});


});
        });




  var img = document.createElement('img');
        var img = document.createElement('img');
  img.src = langFlags[lang];
        img.src = langFlags[lang];
  img.style.width = '32px';
        img.style.width = '32px';
  img.style.height = '24px';
        img.style.height = '24px';
  img.style.borderRadius = '6px';
        img.style.borderRadius = '6px';
  img.style.border = (lang === currentLang) ? '2px solid #333' : '1px solid #ccc';
        img.style.border = (lang === currentLang) ? '2px solid #333' : '1px solid #ccc';
  img.style.transition = 'transform 0.2s ease';
        img.style.transition = 'transform 0.2s ease';
  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';
  });
        });


  link.appendChild(img);
        link.appendChild(img);
  container.appendChild(link);
        container.appendChild(link);
});
      });




    document.body.appendChild(container);
      document.body.appendChild(container);
  });
    });
})();
  })();
</script>
</script>

Edição das 00h27min de 1 de julho de 2025

<script>

 (function () {
   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'
     };
     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, );
     const preferedLang = localStorage.getItem('preferedLang');
     const alreadyInLang = currentLang === preferedLang;
     const redirected = sessionStorage.getItem('langRedirectDone');
     if (preferedLang && !alreadyInLang && !redirected) {
       const targetTitle = baseTitle + (preferedLang === 'pt' ?  : '/' + preferedLang);
       const baseUrl = (window.mw && mw.util && mw.util.getUrl) ? mw.util.getUrl(targetTitle) : '/index.php/' + encodeURIComponent(targetTitle);
       fetch(baseUrl, { method: 'HEAD' }).then(response => {
         sessionStorage.setItem('langRedirectDone', '1');
         if (response.ok) {
           window.location.href = baseUrl;
         } else {
           // Fallback a página base si no existe traducción
           const fallbackUrl = (window.mw && mw.util && mw.util.getUrl)
             ? mw.util.getUrl(baseTitle)
             : '/index.php/' + encodeURIComponent(baseTitle);
           window.location.href = fallbackUrl;
         }
       }).catch(() => {
         // Fallback por error de red
         const fallbackUrl = (window.mw && mw.util && mw.util.getUrl)
           ? mw.util.getUrl(baseTitle)
           : '/index.php/' + encodeURIComponent(baseTitle);
         window.location.href = fallbackUrl;
       });
       return;
     }
     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';
     // botoes pra cada idioma
     langs.forEach(function (lang) {
       var pageTitle = baseTitle + (lang === 'pt' ?  : '/' + lang);
       var fullUrl = (window.mw && mw.util && mw.util.getUrl) ? mw.util.getUrl(pageTitle) : '/index.php/' + encodeURIComponent(pageTitle);
       var link = document.createElement('a');
       link.href = '#';
       link.title = lang.toUpperCase();
       link.addEventListener('click', function (e) {
         e.preventDefault();
         fetch(fullUrl, { method: 'HEAD' }).then(response => {
           if (response.ok) {
             localStorage.setItem('preferedLang', lang);
             sessionStorage.setItem('langRedirectDone', '1');
             window.location.href = fullUrl;
           } else {
             sessionStorage.setItem('langRedirectDone', '1');
             if (lang !== 'pt') {
               const langNames = { es: 'Español', en: 'Inglés', pl: 'Polaco' };
               const msg = document.createElement('div');
               msg.textContent = `La versión en ${langNames[lang]} no está disponible.`;
               msg.style.position = 'fixed';
               msg.style.top = '20px';
               msg.style.left = '50%';
               msg.style.transform = 'translateX(-50%)';
               msg.style.background = '#ffdddd';
               msg.style.color = '#a00';
               msg.style.padding = '12px 24px';
               msg.style.border = '1px solid #a00';
               msg.style.borderRadius = '8px';
               msg.style.fontSize = '16px';
               msg.style.zIndex = '1000';
               msg.style.boxShadow = '0 0 6px rgba(0,0,0,0.2)';
               msg.style.opacity = '0';
               msg.style.transition = 'opacity 0.3s ease';
               document.body.appendChild(msg);
               setTimeout(() => { msg.style.opacity = '1'; }, 10); // Fade in
               setTimeout(() => {
                 msg.style.opacity = '0'; // Fade out
                 setTimeout(() => {
                   msg.remove();
                   const fallbackUrl = (window.mw && mw.util && mw.util.getUrl)
                     ? mw.util.getUrl(baseTitle)
                     : '/index.php/' + encodeURIComponent(baseTitle);
                   window.location.href = fallbackUrl;
                 }, 500);
               }, 3000);
             } else {
               const fallbackUrl = (window.mw && mw.util && mw.util.getUrl)
                 ? mw.util.getUrl(baseTitle)
                 : '/index.php/' + encodeURIComponent(baseTitle);
               window.location.href = fallbackUrl;
             }
           }
         }).catch(() => {
           const fallbackUrl = (window.mw && mw.util && mw.util.getUrl)
             ? mw.util.getUrl(baseTitle)
             : '/index.php/' + encodeURIComponent(baseTitle);
           sessionStorage.setItem('langRedirectDone', '1');
           window.location.href = fallbackUrl;
         });
       });


       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';
       });
       link.appendChild(img);
       container.appendChild(link);
     });


     document.body.appendChild(container);
   });
 })();

</script>