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

De Wiki Gla
Ir para navegação Ir para pesquisar
 
(21 revisões intermediárias pelo mesmo usuário não estão sendo mostradas)
Linha 1: Linha 1:
<script>
<div class="language-switch">
   (function () {
   <a data-lang="pt"><img src="https://upload.wikimedia.org/wikipedia/en/0/05/Flag_of_Brazil.svg" width="32" height="24"></a>
    function onReady(fn) {
  <a data-lang="es"><img src="https://upload.wikimedia.org/wikipedia/commons/9/9a/Flag_of_Spain.svg" width="32" height="24"></a>
      if (document.readyState !== 'loading') {
  <a data-lang="en"><img src="https://upload.wikimedia.org/wikipedia/en/a/a4/Flag_of_the_United_States.svg" width="32" height="24"></a>
        fn();
  <a data-lang="pl"><img src="https://upload.wikimedia.org/wikipedia/en/1/12/Flag_of_Poland.svg" width="32" height="24"></a>
      } else {
</div>
        document.addEventListener('DOMContentLoaded', fn);
      }
    }


    onReady(function () {
<style>
      const langs = ['pt', 'en', 'es', 'pl'];
.language-switch {
      const langFlags = {
  display: flex;
        pt: 'https://upload.wikimedia.org/wikipedia/en/0/05/Flag_of_Brazil.svg',
  gap: 6px;
        en: 'https://upload.wikimedia.org/wikipedia/en/a/a4/Flag_of_the_United_States.svg',
  margin: 8px 0;
        es: 'https://upload.wikimedia.org/wikipedia/en/9/9a/Flag_of_Spain.svg',
  justify-content: center;
        pl: 'https://upload.wikimedia.org/wikipedia/en/1/12/Flag_of_Poland.svg'
}
      };
.language-switch img {
  border: 1px solid #ccc;
  border-radius: 6px;
  cursor: pointer;
  transition: transform 0.2s ease, border-color 0.2s ease;
}
.language-switch img:hover {
  transform: scale(1.1);
  border-color: #333;
}
.language-switch img.active {
  border-color: #000;
}
</style>


      const pathParts = decodeURIComponent(window.location.pathname).split('/index.php/')[1].split('/');
<script>
      const possibleLang = pathParts[pathParts.length - 1];
(function () {
      const currentLang = langs.includes(possibleLang) ? possibleLang : 'pt';
  let currentLang = 'pt'; // idioma por defecto
      const baseTitle = langs.includes(possibleLang) ? pathParts.slice(0, -1).join('/') : pathParts.join('/');


      const preferedLang = localStorage.getItem('preferedLang');
  const langSwitch = document.querySelector('.language-switch');
       const alreadyInLang = currentLang === preferedLang;
  if (langSwitch) {
      const redirected = sessionStorage.getItem('langRedirectDone');
    langSwitch.querySelectorAll('a[data-lang]').forEach(btn => {
       btn.addEventListener('click', () => {
        currentLang = btn.dataset.lang;
        langSwitch.querySelectorAll('img').forEach(img => img.classList.remove('active'));
        btn.querySelector('img').classList.add('active');


      const shouldForce = preferedLang && preferedLang !== 'pt' && (!langs.includes(possibleLang) || currentLang !== preferedLang);
        const active = document.querySelector('.skill-icon.active');
       if (shouldForce && !redirected) {
        if (active) active.click();
        const targetTitle = baseTitle + '/' + preferedLang;
       });
        const targetUrl = '/index.php/' + targetTitle.split('/').map(encodeURIComponent).join('/');
    });


        fetch(targetUrl, { method: 'HEAD' }).then(response => {
    // Activar la bandera por defecto
          sessionStorage.setItem('langRedirectDone', '1');
    const defaultBtn = langSwitch.querySelector('a[data-lang="pt"] img');
          if (response.ok) {
    if (defaultBtn) defaultBtn.classList.add('active');
            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;
  const oldClicks = document.querySelectorAll('.skill-icon');
  oldClicks.forEach(el => {
    el.addEventListener('click', () => {
      const name = el.dataset.nome || '';
      const rawDesc = el.dataset.desc || '';
      let descText = '';


      try {
        const obj = JSON.parse(rawDesc);
        descText = obj[currentLang] || obj['pt'] || '';
      } catch (e) {
        descText = rawDesc; // fallback si no es un JSON
       }
       }


       // Contenedor de banderas
       const attrs = el.dataset.atr || '';
      var container = document.createElement('div');
       const descBox = document.querySelector('.desc-box');
      container.className = 'language-buttons';
      if (descBox) {
       container.style.position = 'absolute';
        descBox.innerHTML = `<div class="skill-title"><h3>${name}</h3></div>${renderAttributes(attrs)}<div class="desc">${descText}</div>`;
      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>
</script>

Edição atual tal como às 06h05min de 31 de agosto de 2025

 <a data-lang="pt"><img src="https://upload.wikimedia.org/wikipedia/en/0/05/Flag_of_Brazil.svg" width="32" height="24"></a>
 <a data-lang="es"><img src="https://upload.wikimedia.org/wikipedia/commons/9/9a/Flag_of_Spain.svg" width="32" height="24"></a>
 <a data-lang="en"><img src="https://upload.wikimedia.org/wikipedia/en/a/a4/Flag_of_the_United_States.svg" width="32" height="24"></a>
 <a data-lang="pl"><img src="https://upload.wikimedia.org/wikipedia/en/1/12/Flag_of_Poland.svg" width="32" height="24"></a>

<style> .language-switch {

 display: flex;
 gap: 6px;
 margin: 8px 0;
 justify-content: center;

} .language-switch img {

 border: 1px solid #ccc;
 border-radius: 6px;
 cursor: pointer;
 transition: transform 0.2s ease, border-color 0.2s ease;

} .language-switch img:hover {

 transform: scale(1.1);
 border-color: #333;

} .language-switch img.active {

 border-color: #000;

} </style>

<script> (function () {

 let currentLang = 'pt'; // idioma por defecto
 const langSwitch = document.querySelector('.language-switch');
 if (langSwitch) {
   langSwitch.querySelectorAll('a[data-lang]').forEach(btn => {
     btn.addEventListener('click', () => {
       currentLang = btn.dataset.lang;
       langSwitch.querySelectorAll('img').forEach(img => img.classList.remove('active'));
       btn.querySelector('img').classList.add('active');
       const active = document.querySelector('.skill-icon.active');
       if (active) active.click();
     });
   });
   // Activar la bandera por defecto
   const defaultBtn = langSwitch.querySelector('a[data-lang="pt"] img');
   if (defaultBtn) defaultBtn.classList.add('active');
 }
 const oldClicks = document.querySelectorAll('.skill-icon');
 oldClicks.forEach(el => {
   el.addEventListener('click', () => {
     const name = el.dataset.nome || ;
     const rawDesc = el.dataset.desc || ;
     let descText = ;
     try {
       const obj = JSON.parse(rawDesc);
       descText = obj[currentLang] || obj['pt'] || ;
     } catch (e) {
       descText = rawDesc; // fallback si no es un JSON
     }
     const attrs = el.dataset.atr || ;
     const descBox = document.querySelector('.desc-box');
     if (descBox) {

descBox.innerHTML = `

${name}

${renderAttributes(attrs)}

${descText}

`;

     }
   });
 });

})(); </script>