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

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