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 () {
  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 título desde la URL real, no desde wgPageName
      const pathParts = decodeURIComponent(window.location.pathname).split('/index.php/')[1].split('/');
    const pathParts = decodeURIComponent(window.location.pathname).split('/index.php/')[1].split('/');
      const possibleLang = pathParts[pathParts.length - 1];
    const possibleLang = pathParts[pathParts.length - 1];
      const currentLang = langs.includes(possibleLang) ? possibleLang : 'pt';
    const currentLang = langs.includes(possibleLang) ? possibleLang : 'pt';
      const baseTitle = langs.includes(possibleLang) ? pathParts.slice(0, -1).join('/') : pathParts.join('/');
    const baseTitle = langs.includes(possibleLang) ? pathParts.slice(0, -1).join('/') : pathParts.join('/');


    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');


    // Redireccionamiento automático
      const shouldForce = preferedLang && preferedLang !== 'pt' && (!langs.includes(possibleLang) || currentLang !== preferedLang);
    if (preferedLang && !alreadyInLang && !redirected) {
      if (shouldForce && !redirected) {
      const targetTitle = baseTitle + (preferedLang === 'pt' ? '' : '/' + preferedLang);
        const targetTitle = baseTitle + '/' + preferedLang;
      const baseUrl = '/index.php/' + targetTitle.split('/').map(encodeURIComponent).join('/');
        const targetUrl = '/index.php/' + targetTitle.split('/').map(encodeURIComponent).join('/');
      fetch(baseUrl, { method: 'HEAD' }).then(response => {
 
        sessionStorage.setItem('langRedirectDone', '1');
        fetch(targetUrl, { method: 'HEAD' }).then(response => {
        window.location.href = response.ok ? baseUrl : '/index.php/' + encodeURIComponent(baseTitle);
          sessionStorage.setItem('langRedirectDone', '1');
      }).catch(() => {
          if (response.ok) {
        sessionStorage.setItem('langRedirectDone', '1');
            window.location.href = targetUrl;
        window.location.href = '/index.php/' + encodeURIComponent(baseTitle);
          } else {
      });
            const fallbackUrl = '/index.php/' + baseTitle.split('/').map(encodeURIComponent).join('/');
      return;
            if (window.location.pathname !== fallbackUrl) {
    }
              window.location.href = fallbackUrl;
            }
          }
        }).catch(() => {
          sessionStorage.setItem('langRedirectDone', '1');
          const fallbackUrl = '/index.php/' + baseTitle.split('/').map(encodeURIComponent).join('/');
          if (window.location.pathname !== fallbackUrl) {
            window.location.href = fallbackUrl;
          }
        });
 
        return;
 
      }


    // Contenedor de banderas
      // Contenedor de banderas
    var container = document.createElement('div');
      var container = document.createElement('div');
    container.className = 'language-buttons';
      container.className = 'language-buttons';
    container.style.position = 'absolute';
      container.style.position = 'absolute';
    container.style.top = '1px';
      container.style.top = '1px';
    container.style.right = '438px';
      container.style.right = '438px';
    container.style.display = 'flex';
      container.style.display = 'flex';
    container.style.padding = '6px 10px';
      container.style.padding = '6px 10px';
    container.style.gap = '8px';
      container.style.gap = '8px';
    container.style.zIndex = '999';
      container.style.zIndex = '999';


    langs.forEach(function (lang) {
      langs.forEach(function (lang) {
      var pageTitle = baseTitle + (lang === 'pt' ? '' : '/' + lang);
        var pageTitle = baseTitle + (lang === 'pt' ? '' : '/' + lang);
      var fullUrl = '/index.php/' + pageTitle.split('/').map(encodeURIComponent).join('/')
        var fullUrl = '/index.php/' + pageTitle.split('/').map(encodeURIComponent).join('/')


      var link = document.createElement('a');
        var link = document.createElement('a');
      link.href = '#';
        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');


            const langNames = { es: 'Español', en: 'English', pl: 'Polski' };
              const langNames = { es: 'Español', en: 'English', pl: 'Polski' };
            const msgTranslations = {
              const msgTranslations = {
              es: 'La versión en {lang} no está disponible.',
                es: 'La versión en {lang} no está disponible.',
              en: 'The {lang} version is not available.',
                en: 'The {lang} version is not available.',
              pl: 'Wersja w języku {lang} jest niedostępna.',
                pl: 'Wersja w języku {lang} jest niedostępna.',
              pt: 'A versão em {lang} não está disponível.'
                pt: 'A versão em {lang} não está disponível.'
            };
              };


            if (lang !== 'pt') {
              if (lang !== 'pt') {
              const userLang = (navigator.language || 'en').substring(0, 2);
                const userLang = (navigator.language || 'en').substring(0, 2);
              const messageTemplate = msgTranslations[userLang] || msgTranslations['en'];
                const messageTemplate = msgTranslations[userLang] || msgTranslations['en'];
              const langLabel = langNames[lang] || lang;
                const langLabel = langNames[lang] || lang;
              const msg = document.createElement('div');
                const msg = document.createElement('div');
              msg.textContent = messageTemplate.replace('{lang}', langLabel);
                msg.textContent = messageTemplate.replace('{lang}', langLabel);
              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.transition = 'opacity 0.3s ease';
              document.body.appendChild(msg);
              setTimeout(() => { msg.style.opacity = '1'; }, 10);
              setTimeout(() => {
                 msg.style.opacity = '0';
                 msg.style.opacity = '0';
                msg.style.transition = 'opacity 0.3s ease';
                document.body.appendChild(msg);
                setTimeout(() => { msg.style.opacity = '1'; }, 10);
                 setTimeout(() => {
                 setTimeout(() => {
                   msg.remove();
                   msg.style.opacity = '0';
                  window.location.href = '/index.php/' + encodeURIComponent(baseTitle);
                  setTimeout(() => {
                }, 500);
                    msg.remove();
              }, 3000);
                    window.location.href = '/index.php/' + encodeURIComponent(baseTitle);
            } else {
                  }, 500);
              window.location.href = '/index.php/' + encodeURIComponent(baseTitle);
                }, 3000);
              } else {
                window.location.href = '/index.php/' + encodeURIComponent(baseTitle);
              }
             }
             }
           }
           }).catch(() => {
        }).catch(() => {
            sessionStorage.setItem('langRedirectDone', '1');
          sessionStorage.setItem('langRedirectDone', '1');
            window.location.href = '/index.php/' + encodeURIComponent(baseTitle);
          window.location.href = '/index.php/' + encodeURIComponent(baseTitle);
          });
         });
         });
      });


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


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

Edição das 03h47min de 27 de agosto 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'
     };
     const pathParts = decodeURIComponent(window.location.pathname).split('/index.php/')[1].split('/');
     const possibleLang = pathParts[pathParts.length - 1];
     const currentLang = langs.includes(possibleLang) ? possibleLang : 'pt';
     const baseTitle = langs.includes(possibleLang) ? pathParts.slice(0, -1).join('/') : pathParts.join('/');
     const preferedLang = localStorage.getItem('preferedLang');
     const alreadyInLang = currentLang === preferedLang;
     const redirected = sessionStorage.getItem('langRedirectDone');
     const shouldForce = preferedLang && preferedLang !== 'pt' && (!langs.includes(possibleLang) || currentLang !== preferedLang);
     if (shouldForce && !redirected) {
       const targetTitle = baseTitle + '/' + preferedLang;
       const targetUrl = '/index.php/' + targetTitle.split('/').map(encodeURIComponent).join('/');
       fetch(targetUrl, { method: 'HEAD' }).then(response => {
         sessionStorage.setItem('langRedirectDone', '1');
         if (response.ok) {
           window.location.href = targetUrl;
         } else {
           const fallbackUrl = '/index.php/' + baseTitle.split('/').map(encodeURIComponent).join('/');
           if (window.location.pathname !== fallbackUrl) {
             window.location.href = fallbackUrl;
           }
         }
       }).catch(() => {
         sessionStorage.setItem('langRedirectDone', '1');
         const fallbackUrl = '/index.php/' + baseTitle.split('/').map(encodeURIComponent).join('/');
         if (window.location.pathname !== fallbackUrl) {
           window.location.href = fallbackUrl;
         }
       });
       return;
     }
     // Contenedor de banderas
     var container = document.createElement('div');
     container.className = 'language-buttons';
     container.style.position = 'absolute';
     container.style.top = '1px';
     container.style.right = '438px';
     container.style.display = 'flex';
     container.style.padding = '6px 10px';
     container.style.gap = '8px';
     container.style.zIndex = '999';
     langs.forEach(function (lang) {
       var pageTitle = baseTitle + (lang === 'pt' ?  : '/' + lang);
       var fullUrl = '/index.php/' + pageTitle.split('/').map(encodeURIComponent).join('/')
       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');
             const langNames = { es: 'Español', en: 'English', pl: 'Polski' };
             const msgTranslations = {
               es: 'La versión en {lang} no está disponible.',
               en: 'The {lang} version is not available.',
               pl: 'Wersja w języku {lang} jest niedostępna.',
               pt: 'A versão em {lang} não está disponível.'
             };
             if (lang !== 'pt') {
               const userLang = (navigator.language || 'en').substring(0, 2);
               const messageTemplate = msgTranslations[userLang] || msgTranslations['en'];
               const langLabel = langNames[lang] || lang;
               const msg = document.createElement('div');
               msg.textContent = messageTemplate.replace('{lang}', langLabel);
               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);
               setTimeout(() => {
                 msg.style.opacity = '0';
                 setTimeout(() => {
                   msg.remove();
                   window.location.href = '/index.php/' + encodeURIComponent(baseTitle);
                 }, 500);
               }, 3000);
             } else {
               window.location.href = '/index.php/' + encodeURIComponent(baseTitle);
             }
           }
         }).catch(() => {
           sessionStorage.setItem('langRedirectDone', '1');
           window.location.href = '/index.php/' + encodeURIComponent(baseTitle);
         });
       });
       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>