MediaWiki:Common.js: Porovnání verzí

Z Wiki JU
Ofara (diskuse | příspěvky)
Bez shrnutí editace
značka: revertováno
Ofara (diskuse | příspěvky)
Bez shrnutí editace
značka: revertováno
Řádek 23: Řádek 23:
});
});


// Unwrap external links when page is viewed via Google Translate proxy
// --- Fix externích odkazů při zobrazení přes Google Translate proxy ---
// Doména vaší wiki:
var WIKI_HOST = 'wiki.jcu.cz';
 
(function () {
(function () {
   function isGoogleTranslateHost(host) {
   function isTranslateHost(host) {
     return /(^|\.)translate\.(goog|googleusercontent\.com)$/.test(host);
     return host.endsWith('.translate.goog') ||
  }
          /(^|\.)translate\.googleusercontent\.com$/.test(host);
 
  function isTranslateView() {
    return isGoogleTranslateHost(location.hostname) ||
          /[?&]_x_tr_/.test(location.search) ||
          /\/translate(_c)?\?/.test(location.href);
   }
   }
 
   function inTranslateView() {
  if (!isTranslateView()) return;
     return isTranslateHost(location.hostname) || /[?&]_x_tr_/.test(location.search);
 
  // Z původního hostitele wiki: pokud jsme na *.translate.goog, vezmeme část před sufixem a nahradíme pomlčky tečkami
   function getOriginalWikiHost() {
     var h = location.hostname;
    if (h.endsWith('.translate.goog')) {
      var base = h.slice(0, -'.translate.goog'.length); // např. "wiki-jcu-cz"
      return base.replace(/-/g, '.');                  // → "wiki.jcu.cz"
    }
    return h;
   }
   }
   var originalWikiHost = getOriginalWikiHost();
   if (!inTranslateView()) return;


   // Vrátí původní URL z různých forem Google Translate wrapperu
   // Rozbalí URL z Google Translate (*.translate.goog → původní host, zahodí _x_tr_*)
   function unwrapGoogleTranslateUrl(href) {
   function unwrapUrl(href) {
     try {
     try {
       var url = new URL(href);
       var url = new URL(href, location.href);


       // 1) translate.googleusercontent.com/translate_c?u=<ORIGINAL>&...
       // translate.googleusercontent.com/translate_c?u=<ORIGINAL>
       if (isGoogleTranslateHost(url.hostname) && url.pathname.indexOf('/translate_c') !== -1) {
       if (isTranslateHost(url.hostname) && url.pathname.indexOf('/translate_c') !== -1) {
         var u = url.searchParams.get('u');
         var u = url.searchParams.get('u');
         if (u) return new URL(u).href;
         if (u) return new URL(u).href;
       }
       }


       // 2) *.translate.goog — hostname s pomlčkami a parametry _x_tr_*
       // *.translate.goog → host s pomlčkami zpět na tečky
       if (url.hostname.endsWith('.translate.goog')) {
       if (url.hostname.endsWith('.translate.goog')) {
        // Převod hostu zpět
         var hostPart = url.hostname.slice(0, -'.translate.goog'.length).replace(/-/g, '.');
         var hostPart = url.hostname.slice(0, -'.translate.goog'.length).replace(/-/g, '.');
        // Protokol může být v parametru _x_tr_sch (http/https), defaultně https
         var scheme = url.searchParams.get('_x_tr_sch') || 'https';
         var scheme = url.searchParams.get('_x_tr_sch') || 'https';
        // Z parametrů ponecháme jen ty, které nejsou _x_tr_*
         var kept = [];
         var kept = [];
         url.searchParams.forEach(function (v, k) {
         url.searchParams.forEach(function (v, k) {
           if (!/^_x_tr_/.test(k)) kept.push(encodeURIComponent(k) + '=' + encodeURIComponent(v));
           if (!/^_x_tr_/.test(k)) kept.push(encodeURIComponent(k) + '=' + encodeURIComponent(v));
         });
         });
         var query = kept.length ? ('?' + kept.join('&')) : '';
         var query = kept.length ? '?' + kept.join('&') : '';
 
         return scheme + '://' + hostPart + url.pathname + query + url.hash;
         return scheme + '://' + hostPart + url.pathname + query + url.hash;
       }
       }


       return href;
       return url.href;
     } catch (e) {
     } catch (e) {
       return href;
       return href;
Řádek 83: Řádek 66:
   }
   }


   function isHttpLike(href) {
   function isExternal(href) {
     return /^https?:\/\//i.test(href);
     try { return new URL(href).host !== WIKI_HOST; } catch { return false; }
  }
 
  // 1) Hromadně upravíme existující <a> a <form>
  function patchLinksAndForms(root) {
    root.querySelectorAll('a[href]').forEach(function (a) {
      var clean = unwrapUrl(a.href);
      if (isExternal(clean)) {
        a.href = clean;
        a.target = '_blank';
        a.rel = (a.rel ? a.rel + ' ' : '') + 'noopener noreferrer';
      }
    });
 
    root.querySelectorAll('form[action]').forEach(function (f) {
      try {
        var clean = unwrapUrl(f.action);
        if (isExternal(clean)) {
          f.action = clean;
          // u SSO je lepší nové okno, ať to Google neobalí
          f.target = '_blank';
          // případně: f.rel = 'noopener'; (ne každý prohlížeč to na <form> uplatní)
        }
      } catch (e) {}
    });
   }
   }


   // Projdeme všechny odkazy a externí „odbalíme“
   // 2) Zachytíme kliky dřív než Google – a otevřeme čistou URL sami
   document.querySelectorAll('a[href]').forEach(function (a) {
   document.addEventListener('click', function (e) {
    // pouze levé tlačítko bez modifikátorů
    if (e.defaultPrevented || e.button !== 0 || e.metaKey || e.ctrlKey || e.shiftKey || e.altKey) return;
 
    var a = e.target;
    while (a && a.tagName !== 'A') a = a.parentElement;
    if (!a) return;
     var href = a.getAttribute('href');
     var href = a.getAttribute('href');
     if (!href || href.startsWith('#') || href.startsWith('mailto:') || href.startsWith('tel:')) return;
     if (!href || href.startsWith('#') || href.startsWith('mailto:') || href.startsWith('tel:')) return;


    // Absolutizace kvůli dalšímu zpracování
     var clean = unwrapUrl(a.href);
     var absHref;
     if (isExternal(clean)) {
     try {
       e.preventDefault();
       absHref = new URL(href, location.href).href;
      e.stopImmediatePropagation(); // předejdeme přepisům od Googlu
    } catch (e) {
       window.open(clean, '_blank', 'noopener');
       return;
     }
     }
  }, true); // capture = true => jsme před Googlem


    // Odstraníme google translate wrapper, pokud tam je
  // 3) Ošetříme i odeslání formulářů (SAML/SSO, vyhledávače apod.)
     var unwrapped = unwrapGoogleTranslateUrl(absHref);
  document.addEventListener('submit', function (e) {
    var f = e.target;
    if (!(f && f.action)) return;
     var clean = unwrapUrl(f.action);
    if (isExternal(clean)) {
      // jen upravíme action a necháme proběhnout (cílit do nového okna)
      f.action = clean;
      f.target = '_blank';
    }
  }, true);


    // Pokud to není http/https, neřešíme
  // 4) Prvotní průchod a ošetření dynamicky přidaného obsahu
    if (!isHttpLike(unwrapped)) return;
  patchLinksAndForms(document);


    // Rozhodnutí: interní vs externí
  // Volitelně: observer pro později načtené bloky
    try {
  var mo = new MutationObserver(function (muts) {
      var destHost = new URL(unwrapped).host;
    muts.forEach(function (m) {
 
       if (m.addedNodes) {
       // Interní odkazy (na naši wiki) ponecháme tak, aby zůstaly v překladu
        m.addedNodes.forEach(function (n) {
      // – tzn. pokud odkaz míří na naši původní doménu (originalWikiHost), tak NEMĚNÍME nic,
          if (n.nodeType === 1) patchLinksAndForms(n);
      //  ponecháme původní (přeložený) <a>, aby uživatel zůstal v translated verzi.
         });
      if (destHost === originalWikiHost) {
         return;
       }
       }
 
     });
      // Externí odkazy přepíšeme na původní čistou URL, otevřít do nového okna
      a.setAttribute('href', unwrapped);
      a.setAttribute('target', '_blank');
      a.setAttribute('rel', (a.getAttribute('rel') ? a.getAttribute('rel') + ' ' : '') + 'noopener noreferrer');
     } catch (e) {
      // Pokud URL nejde parsovat, raději neděláme nic
    }
   });
   });
  mo.observe(document.documentElement, { childList: true, subtree: true });
})();
})();

Verze z 13. 8. 2025, 08:23

/* Zde uvedený JavaScript bude použit pro všechny uživatele při načtení každé stránky */

$(function () {
  // Zruš jQuery UI vzhled tlačítka
  $('.articleFeedbackv5-submit').button('destroy');
});

document.addEventListener('DOMContentLoaded', function () {
  document.querySelectorAll('.extern-download').forEach(el => {
    el.style.cursor = 'pointer';
    el.style.color = '#0645AD';
    el.style.textDecoration = 'underline';
    el.addEventListener('click', () => {
      const a = document.createElement('a');
      a.href = el.getAttribute('data-url');
      a.setAttribute('download', '');
      a.style.display = 'none';
      document.body.appendChild(a);
      a.click();
      document.body.removeChild(a);
    });
  });
});

// --- Fix externích odkazů při zobrazení přes Google Translate proxy ---
// Doména vaší wiki:
var WIKI_HOST = 'wiki.jcu.cz';

(function () {
  function isTranslateHost(host) {
    return host.endsWith('.translate.goog') ||
           /(^|\.)translate\.googleusercontent\.com$/.test(host);
  }
  function inTranslateView() {
    return isTranslateHost(location.hostname) || /[?&]_x_tr_/.test(location.search);
  }
  if (!inTranslateView()) return;

  // Rozbalí URL z Google Translate (*.translate.goog → původní host, zahodí _x_tr_*)
  function unwrapUrl(href) {
    try {
      var url = new URL(href, location.href);

      // translate.googleusercontent.com/translate_c?u=<ORIGINAL>
      if (isTranslateHost(url.hostname) && url.pathname.indexOf('/translate_c') !== -1) {
        var u = url.searchParams.get('u');
        if (u) return new URL(u).href;
      }

      // *.translate.goog → host s pomlčkami zpět na tečky
      if (url.hostname.endsWith('.translate.goog')) {
        var hostPart = url.hostname.slice(0, -'.translate.goog'.length).replace(/-/g, '.');
        var scheme = url.searchParams.get('_x_tr_sch') || 'https';
        var kept = [];
        url.searchParams.forEach(function (v, k) {
          if (!/^_x_tr_/.test(k)) kept.push(encodeURIComponent(k) + '=' + encodeURIComponent(v));
        });
        var query = kept.length ? '?' + kept.join('&') : '';
        return scheme + '://' + hostPart + url.pathname + query + url.hash;
      }

      return url.href;
    } catch (e) {
      return href;
    }
  }

  function isExternal(href) {
    try { return new URL(href).host !== WIKI_HOST; } catch { return false; }
  }

  // 1) Hromadně upravíme existující <a> a <form>
  function patchLinksAndForms(root) {
    root.querySelectorAll('a[href]').forEach(function (a) {
      var clean = unwrapUrl(a.href);
      if (isExternal(clean)) {
        a.href = clean;
        a.target = '_blank';
        a.rel = (a.rel ? a.rel + ' ' : '') + 'noopener noreferrer';
      }
    });

    root.querySelectorAll('form[action]').forEach(function (f) {
      try {
        var clean = unwrapUrl(f.action);
        if (isExternal(clean)) {
          f.action = clean;
          // u SSO je lepší nové okno, ať to Google neobalí
          f.target = '_blank';
          // případně: f.rel = 'noopener'; (ne každý prohlížeč to na <form> uplatní)
        }
      } catch (e) {}
    });
  }

  // 2) Zachytíme kliky dřív než Google – a otevřeme čistou URL sami
  document.addEventListener('click', function (e) {
    // pouze levé tlačítko bez modifikátorů
    if (e.defaultPrevented || e.button !== 0 || e.metaKey || e.ctrlKey || e.shiftKey || e.altKey) return;

    var a = e.target;
    while (a && a.tagName !== 'A') a = a.parentElement;
    if (!a) return;
    var href = a.getAttribute('href');
    if (!href || href.startsWith('#') || href.startsWith('mailto:') || href.startsWith('tel:')) return;

    var clean = unwrapUrl(a.href);
    if (isExternal(clean)) {
      e.preventDefault();
      e.stopImmediatePropagation(); // předejdeme přepisům od Googlu
      window.open(clean, '_blank', 'noopener');
    }
  }, true); // capture = true => jsme před Googlem

  // 3) Ošetříme i odeslání formulářů (SAML/SSO, vyhledávače apod.)
  document.addEventListener('submit', function (e) {
    var f = e.target;
    if (!(f && f.action)) return;
    var clean = unwrapUrl(f.action);
    if (isExternal(clean)) {
      // jen upravíme action a necháme proběhnout (cílit do nového okna)
      f.action = clean;
      f.target = '_blank';
    }
  }, true);

  // 4) Prvotní průchod a ošetření dynamicky přidaného obsahu
  patchLinksAndForms(document);

  // Volitelně: observer pro později načtené bloky
  var mo = new MutationObserver(function (muts) {
    muts.forEach(function (m) {
      if (m.addedNodes) {
        m.addedNodes.forEach(function (n) {
          if (n.nodeType === 1) patchLinksAndForms(n);
        });
      }
    });
  });
  mo.observe(document.documentElement, { childList: true, subtree: true });
})();