MediaWiki:Common.js: differenze tra le versioni
Aspetto
Common.js v5: rimuovo auto-redirect IT-RU bidirezionale (causava loop e ritorni in russo). Lascio solo mobile cookie nuke + cleanup cookie autolang residui. |
Common.js v5: URL decide UI, niente cross-redirect, mobile nuke mantenuto |
||
| Riga 2: | Riga 2: | ||
* Common.js v5 — Wiki Methode Paret | * Common.js v5 — Wiki Methode Paret | ||
* ============================================================ | * ============================================================ | ||
* REGOLA UNICA: | |||
* URL /Titolo → UI italiana | |||
* URL /Titolo/ru → UI russa | |||
* URL /Titolo/en → UI inglese (futuro) | |||
* etc. | |||
* | * | ||
* | * Niente cookie che ricordano scelte. Niente cross-redirect. | ||
* URL | * L'URL determina sia il contenuto SIA la lingua dell'interfaccia. | ||
* | * | ||
* | * + mobile cookie nuke (per MobileFrontend) | ||
* + cleanup cookie velenosi delle versioni precedenti | |||
* | |||
* ============================================================ */ | * ============================================================ */ | ||
( function () { | ( function ( mw ) { | ||
'use strict'; | 'use strict'; | ||
var SUPPORTED_LANGS = [ 'ru', 'en', 'fr', 'es', 'pt' ]; | |||
// ─── Cookie helpers ───────────────────────────────────── | // ─── Cookie helpers ───────────────────────────────────── | ||
| Riga 34: | Riga 37: | ||
} | } | ||
// ─── 1. Mobile cookie nuke | // ─── 1. Mobile cookie nuke (per MobileFrontend / Minerva) ── | ||
var ua = navigator.userAgent || ''; | var ua = navigator.userAgent || ''; | ||
var isMobile = /iPhone|iPad|iPod|Android.*Mobile|Mobile.*Android|webOS|BlackBerry|Opera Mini|IEMobile/i.test( ua ); | var isMobile = /iPhone|iPad|iPod|Android.*Mobile|Mobile.*Android|webOS|BlackBerry|Opera Mini|IEMobile/i.test( ua ); | ||
var mobileCookiesDeleted = false; | var mobileCookiesDeleted = false; | ||
if ( isMobile ) { | if ( isMobile ) { | ||
[ 'stopMobileRedirect', 'mf_useformat' ].forEach( function ( | [ 'stopMobileRedirect', 'mf_useformat' ].forEach( function ( n ) { | ||
if ( getCookie( | if ( getCookie( n ) !== null ) { | ||
deleteCookie( | deleteCookie( n ); | ||
mobileCookiesDeleted = true; | mobileCookiesDeleted = true; | ||
} | } | ||
| Riga 47: | Riga 50: | ||
} | } | ||
// ─── 2. | // ─── 2. Cleanup cookie velenosi delle versioni precedenti ── | ||
[ 'autolang_set', 'no_auto_lang_redirect' ].forEach( function ( n ) { | |||
if ( getCookie( n ) !== null ) deleteCookie( n ); | |||
} ); | |||
} | |||
// ─── 3. Se mobile + appena pulito | // ─── 3. Se mobile + appena pulito + su Vector → reload ───── | ||
if ( mobileCookiesDeleted && | if ( mobileCookiesDeleted && | ||
document.body && | document.body && | ||
/skin-vector/.test( document.body.className ) ) { | /skin-vector/.test( document.body.className ) ) { | ||
window.location.reload(); | window.location.reload(); | ||
return; | |||
} | |||
// ─── 4. LOGICA UNICA: URL decide UI ─────────────────────── | |||
function syncUILangFromURL() { | |||
if ( typeof mw === 'undefined' || !mw.config ) return; | |||
var pageName = mw.config.get( 'wgPageName' ) || ''; | |||
var currentUI = mw.config.get( 'wgUserLanguage' ); | |||
var contentLang = mw.config.get( 'wgContentLanguage' ) || 'it'; | |||
// Determino la lingua DESIDERATA dall'URL | |||
var match = pageName.match( /\/([a-z]{2})$/ ); | |||
var desiredLang; | |||
if ( match && SUPPORTED_LANGS.indexOf( match[ 1 ] ) !== -1 ) { | |||
desiredLang = match[ 1 ]; | |||
} else { | |||
desiredLang = contentLang; // pagina senza suffisso → italiano | |||
} | |||
// Già allineata? OK | |||
if ( currentUI === desiredLang ) return; | |||
// Solo per visualizzazione pagina normale | |||
var action = mw.config.get( 'wgAction' ); | |||
if ( action && action !== 'view' ) return; | |||
var ns = mw.config.get( 'wgNamespaceNumber' ); | |||
if ( ns < 0 ) return; // Special pages | |||
// Se URL ha già ?uselang= → è già stato forzato, non rifare loop | |||
var url = new URL( window.location.href ); | |||
if ( url.searchParams.get( 'uselang' ) ) return; | |||
// Aggiungi ?uselang=XX e ricarica una volta | |||
url.searchParams.set( 'uselang', desiredLang ); | |||
window.location.replace( url.toString() ); | |||
} | |||
if ( typeof mw !== 'undefined' && mw.config ) { | |||
mw.loader.using( [ 'mediawiki.util' ] ).then( syncUILangFromURL ); | |||
} | } | ||
}() ); | }( window.mediaWiki || window.mw ) ); | ||
/* ============================================================ | /* ============================================================ | ||
* | * Niente più auto-redirect cross-language. | ||
* | * Per cambiare lingua: clicca sull'icona globo (ULS) o cambia URL. | ||
* ============================================================ */ | * ============================================================ */ | ||
Versione delle 10:07, 1 giu 2026
/* ============================================================
* Common.js v5 — Wiki Methode Paret
* ============================================================
* REGOLA UNICA:
* URL /Titolo → UI italiana
* URL /Titolo/ru → UI russa
* URL /Titolo/en → UI inglese (futuro)
* etc.
*
* Niente cookie che ricordano scelte. Niente cross-redirect.
* L'URL determina sia il contenuto SIA la lingua dell'interfaccia.
*
* + mobile cookie nuke (per MobileFrontend)
* + cleanup cookie velenosi delle versioni precedenti
* ============================================================ */
( function ( mw ) {
'use strict';
var SUPPORTED_LANGS = [ 'ru', 'en', 'fr', 'es', 'pt' ];
// ─── Cookie helpers ─────────────────────────────────────
function getCookie( name ) {
var m = document.cookie.match( new RegExp( '(?:^|;\\s*)' + name + '=([^;]*)' ) );
return m ? decodeURIComponent( m[ 1 ] ) : null;
}
function deleteCookie( name ) {
var hostname = window.location.hostname;
var parts = hostname.split( '.' );
var domains = [ hostname, '' ];
if ( parts.length > 2 ) {
domains.push( '.' + parts.slice( -2 ).join( '.' ) );
}
domains.forEach( function ( d ) {
var suffix = d ? '; domain=' + d : '';
document.cookie = name + '=; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/' + suffix;
} );
}
// ─── 1. Mobile cookie nuke (per MobileFrontend / Minerva) ──
var ua = navigator.userAgent || '';
var isMobile = /iPhone|iPad|iPod|Android.*Mobile|Mobile.*Android|webOS|BlackBerry|Opera Mini|IEMobile/i.test( ua );
var mobileCookiesDeleted = false;
if ( isMobile ) {
[ 'stopMobileRedirect', 'mf_useformat' ].forEach( function ( n ) {
if ( getCookie( n ) !== null ) {
deleteCookie( n );
mobileCookiesDeleted = true;
}
} );
}
// ─── 2. Cleanup cookie velenosi delle versioni precedenti ──
[ 'autolang_set', 'no_auto_lang_redirect' ].forEach( function ( n ) {
if ( getCookie( n ) !== null ) deleteCookie( n );
} );
// ─── 3. Se mobile + appena pulito + su Vector → reload ─────
if ( mobileCookiesDeleted &&
document.body &&
/skin-vector/.test( document.body.className ) ) {
window.location.reload();
return;
}
// ─── 4. LOGICA UNICA: URL decide UI ───────────────────────
function syncUILangFromURL() {
if ( typeof mw === 'undefined' || !mw.config ) return;
var pageName = mw.config.get( 'wgPageName' ) || '';
var currentUI = mw.config.get( 'wgUserLanguage' );
var contentLang = mw.config.get( 'wgContentLanguage' ) || 'it';
// Determino la lingua DESIDERATA dall'URL
var match = pageName.match( /\/([a-z]{2})$/ );
var desiredLang;
if ( match && SUPPORTED_LANGS.indexOf( match[ 1 ] ) !== -1 ) {
desiredLang = match[ 1 ];
} else {
desiredLang = contentLang; // pagina senza suffisso → italiano
}
// Già allineata? OK
if ( currentUI === desiredLang ) return;
// Solo per visualizzazione pagina normale
var action = mw.config.get( 'wgAction' );
if ( action && action !== 'view' ) return;
var ns = mw.config.get( 'wgNamespaceNumber' );
if ( ns < 0 ) return; // Special pages
// Se URL ha già ?uselang= → è già stato forzato, non rifare loop
var url = new URL( window.location.href );
if ( url.searchParams.get( 'uselang' ) ) return;
// Aggiungi ?uselang=XX e ricarica una volta
url.searchParams.set( 'uselang', desiredLang );
window.location.replace( url.toString() );
}
if ( typeof mw !== 'undefined' && mw.config ) {
mw.loader.using( [ 'mediawiki.util' ] ).then( syncUILangFromURL );
}
}( window.mediaWiki || window.mw ) );
/* ============================================================
* Niente più auto-redirect cross-language.
* Per cambiare lingua: clicca sull'icona globo (ULS) o cambia URL.
* ============================================================ */