MediaWiki:Common.js: Porovnání verzí
Z Wiki JU
Bez shrnutí editace značka: revertováno |
Bez shrnutí editace značka: revertováno |
||
| Řádek 25: | Řádek 25: | ||
// Unwrap external links when page is viewed via Google Translate proxy | // Unwrap external links when page is viewed via Google Translate proxy | ||
(function () { | (function () { | ||
function isGoogleTranslateHost( | function isGoogleTranslateHost(host) { | ||
return /(^|\.)translate\.(goog | return /(^|\.)translate\.(goog|googleusercontent\.com)$/.test(host); | ||
} | } | ||
function | |||
function isTranslateView() { | |||
return isGoogleTranslateHost(location.hostname) || | return isGoogleTranslateHost(location.hostname) || | ||
/[?&] | /[?&]_x_tr_/.test(location.search) || | ||
/\/translate(_c)?\?/.test(location.href); | /\/translate(_c)?\?/.test(location.href); | ||
} | } | ||
// | if (!isTranslateView()) return; | ||
var | // 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 | // Vrátí původní URL z různých forem Google Translate wrapperu | ||
function | function unwrapGoogleTranslateUrl(href) { | ||
try { | try { | ||
var url = new URL(href); | var url = new URL(href); | ||
// 1) translate.googleusercontent.com/translate_c? | // 1) translate.googleusercontent.com/translate_c?u=<ORIGINAL>&... | ||
if (isGoogleTranslateHost(url.hostname) && url. | 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 s parametry _x_tr_* | // 2) *.translate.goog — hostname s pomlčkami a parametry _x_tr_* | ||
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, '.'); | |||
// 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_* | |||
// ponecháme jen | |||
var kept = []; | var kept = []; | ||
url.searchParams.forEach(function (v, k) { | url.searchParams.forEach(function (v, k) { | ||
| Řádek 60: | Řádek 73: | ||
}); | }); | ||
var query = kept.length ? ('?' + kept.join('&')) : ''; | var query = kept.length ? ('?' + kept.join('&')) : ''; | ||
return scheme + '://' + | |||
return scheme + '://' + hostPart + url.pathname + query + url.hash; | |||
} | } | ||
| Řádek 69: | Řádek 83: | ||
} | } | ||
function isHttpLike(href) { | |||
return /^https?:\/\//i.test(href); | |||
} | |||
// Projdeme všechny odkazy a externí „odbalíme“ | |||
document.querySelectorAll('a[href]').forEach(function (a) { | document.querySelectorAll('a[href]').forEach(function (a) { | ||
var | var href = a.getAttribute('href'); | ||
if (! | 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 { | try { | ||
var | var destHost = new URL(unwrapped).host; | ||
// pokud odkaz | |||
if ( | // 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; | |||
} | } | ||
} catch (e) {} | |||
// 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 | |||
} | |||
}); | }); | ||
})(); | })(); | ||
Verze z 13. 8. 2025, 08:18
/* 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
}
});
})();