MediaWiki:Common.js

Z Wiki JU

Poznámka: Po zveřejnění musíte vyprázdnit cache vašeho prohlížeče, jinak změny neuvidíte.

  • Firefox / Safari: Při kliknutí na Aktualizovat držte Shift nebo stiskněte Ctrl-F5 nebo Ctrl-R (na Macu ⌘-R)
  • Google Chrome: Stiskněte Ctrl-Shift-R (na Macu ⌘-Shift-R)
  • Edge: Při kliknutí na Aktualizovat držte Ctrl nebo stiskněte Ctrl-F5.
/* 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);
    });
  });
});

// Unwrap external links when page is viewed via Google Translate proxy
(function () {
  function isGoogleTranslateHost(host) {
    return /(^|\.)translate\.(goog|googleusercontent\.com)$/.test(host);
  }

  function isTranslateView() {
    return isGoogleTranslateHost(location.hostname) ||
           /[?&]_x_tr_/.test(location.search) ||
           /\/translate(_c)?\?/.test(location.href);
  }

  if (!isTranslateView()) return;

  // 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();

  // Vrátí původní URL z různých forem Google Translate wrapperu
  function unwrapGoogleTranslateUrl(href) {
    try {
      var url = new URL(href);

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

      // 2) *.translate.goog — hostname s pomlčkami a parametry _x_tr_*
      if (url.hostname.endsWith('.translate.goog')) {
        // Převod hostu zpět
        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';

        // Z parametrů ponecháme jen ty, které nejsou _x_tr_*
        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 href;
    } catch (e) {
      return href;
    }
  }

  function isHttpLike(href) {
    return /^https?:\/\//i.test(href);
  }

  // Projdeme všechny odkazy a externí „odbalíme“
  document.querySelectorAll('a[href]').forEach(function (a) {
    var href = a.getAttribute('href');
    if (!href || href.startsWith('#') || href.startsWith('mailto:') || href.startsWith('tel:')) return;

    // Absolutizace kvůli dalšímu zpracování
    var absHref;
    try {
      absHref = new URL(href, location.href).href;
    } catch (e) {
      return;
    }

    // Odstraníme google translate wrapper, pokud tam je
    var unwrapped = unwrapGoogleTranslateUrl(absHref);

    // Pokud to není http/https, neřešíme
    if (!isHttpLike(unwrapped)) return;

    // Rozhodnutí: interní vs externí
    try {
      var destHost = new URL(unwrapped).host;

      // Interní odkazy (na naši wiki) ponecháme tak, aby zůstaly v překladu
      // – tzn. pokud odkaz míří na naši původní doménu (originalWikiHost), tak NEMĚNÍME nic,
      //   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
    }
  });
})();