MediaWiki:Common.js: differenze tra le versioni

Admin (discussione | contributi)
Common.js v5: URL decide UI, niente cross-redirect, mobile nuke mantenuto
Rimosso redirect JS useformat=mobile (duplicato del 302 server): faceva perdere i tap sul gate
 
(2 versioni intermedie di un altro utente non mostrate)
Riga 1: Riga 1:
/* ============================================================
/* ============================================================
  * Common.js v5 — Wiki Methode Paret
  * Common.js v7 — Wiki Methode Paret
  * ============================================================
  * ============================================================
  * REGOLA UNICA:
  * REGOLA UNICA:
  *  URL /Titolo      → UI italiana
  *  URL /Titolo      → UI italiana
  *  URL /Titolo/ru    → UI russa
  *  URL /Titolo/ru    → UI russa
  *   URL /Titolo/en    → UI inglese (futuro)
  *
  *   etc.
  * Mobile detection v7:
  *  
  *   - Touch capability (funziona anche con "Versione Desktop" del browser)
  * Niente cookie che ricordano scelte. Niente cross-redirect.
  *   - Viewport width <= 1200 (più tollerante: cattura anche desktop mode 1080)
* L'URL determina sia il contenuto SIA la lingua dell'interfaccia.
  *   - Una delle due basta
*
* + mobile cookie nuke (per MobileFrontend)
  * + cleanup cookie velenosi delle versioni precedenti
  * ============================================================ */
  * ============================================================ */
( function ( mw ) {
( function ( mw ) {
Riga 19: Riga 16:
     var SUPPORTED_LANGS = [ 'ru', 'en', 'fr', 'es', 'pt' ];
     var SUPPORTED_LANGS = [ 'ru', 'en', 'fr', 'es', 'pt' ];


    // ─── Cookie helpers ─────────────────────────────────────
     function getCookie( name ) {
     function getCookie( name ) {
         var m = document.cookie.match( new RegExp( '(?:^|;\\s*)' + name + '=([^;]*)' ) );
         var m = document.cookie.match( new RegExp( '(?:^|;\\s*)' + name + '=([^;]*)' ) );
Riga 37: Riga 33:
     }
     }


     // ─── 1. Mobile cookie nuke (per MobileFrontend / Minerva) ──
     // ─── DETECTION MOBILE ROBUSTA ──────────────────────────
     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 isMobileUA = /iPhone|iPad|iPod|Android|Mobile|webOS|BlackBerry|Opera Mini|IEMobile|Silk/i.test( ua );
     var mobileCookiesDeleted = false;
     var viewportWidth = window.innerWidth || document.documentElement.clientWidth || 1024;
    var isSmallViewport = viewportWidth <= 1200;
    var hasTouch = ( 'ontouchstart' in window ) ||
                  ( navigator.maxTouchPoints > 0 ) ||
                  ( navigator.msMaxTouchPoints > 0 );
 
    // Mobile se: UA mobile OPPURE (touch + viewport piccolino)
    // Il caso "Versione Desktop attivata" ha touch=true e viewport ~1080 → cattura
    var isMobile = isMobileUA || ( hasTouch && isSmallViewport );
 
     if ( isMobile ) {
     if ( isMobile ) {
         [ 'stopMobileRedirect', 'mf_useformat' ].forEach( function ( n ) {
         [ 'stopMobileRedirect', 'mf_useformat' ].forEach( function ( n ) {
             if ( getCookie( n ) !== null ) {
             if ( getCookie( n ) !== null ) deleteCookie( n );
                deleteCookie( n );
                mobileCookiesDeleted = true;
            }
         } );
         } );
     }
     }


    // ─── 2. Cleanup cookie velenosi delle versioni precedenti ──
     [ 'autolang_set', 'no_auto_lang_redirect' ].forEach( function ( n ) {
     [ 'autolang_set', 'no_auto_lang_redirect' ].forEach( function ( n ) {
         if ( getCookie( n ) !== null ) deleteCookie( n );
         if ( getCookie( n ) !== null ) deleteCookie( n );
     } );
     } );
    // [ISICNV 14/07/2026] Redirect useformat=mobile RIMOSSO: lo fa gia il server (302).
    // Il location.replace ricaricava la pagina dopo il render, facendo perdere i tap sul gate.


     // ─── 3. Se mobile + appena pulito + su Vector → reload ─────
     // ─── URL decide UI lingua ─────────────────────────────
    if ( mobileCookiesDeleted &&
        document.body &&
        /skin-vector/.test( document.body.className ) ) {
        window.location.reload();
        return;
    }
 
    // ─── 4. LOGICA UNICA: URL decide UI ───────────────────────
     function syncUILangFromURL() {
     function syncUILangFromURL() {
         if ( typeof mw === 'undefined' || !mw.config ) return;
         if ( typeof mw === 'undefined' || !mw.config ) return;
Riga 71: Riga 66:
         var contentLang = mw.config.get( 'wgContentLanguage' ) || 'it';
         var contentLang = mw.config.get( 'wgContentLanguage' ) || 'it';


        // Determino la lingua DESIDERATA dall'URL
         var match = pageName.match( /\/([a-z]{2})$/ );
         var match = pageName.match( /\/([a-z]{2})$/ );
         var desiredLang;
         var desiredLang;
Riga 77: Riga 71:
             desiredLang = match[ 1 ];
             desiredLang = match[ 1 ];
         } else {
         } else {
             desiredLang = contentLang; // pagina senza suffisso → italiano
             desiredLang = contentLang;
         }
         }


        // Già allineata? OK
         if ( currentUI === desiredLang ) return;
         if ( currentUI === desiredLang ) return;


        // Solo per visualizzazione pagina normale
         var action = mw.config.get( 'wgAction' );
         var action = mw.config.get( 'wgAction' );
         if ( action && action !== 'view' ) return;
         if ( action && action !== 'view' ) return;


         var ns = mw.config.get( 'wgNamespaceNumber' );
         var ns = mw.config.get( 'wgNamespaceNumber' );
         if ( ns < 0 ) return; // Special pages
         if ( ns < 0 ) return;


        // Se URL ha già ?uselang= → è già stato forzato, non rifare loop
         var url = new URL( window.location.href );
         var url = new URL( window.location.href );
         if ( url.searchParams.get( 'uselang' ) ) return;
         if ( url.searchParams.get( 'uselang' ) ) return;


        // Aggiungi ?uselang=XX e ricarica una volta
         url.searchParams.set( 'uselang', desiredLang );
         url.searchParams.set( 'uselang', desiredLang );
         window.location.replace( url.toString() );
         window.location.replace( url.toString() );
Riga 104: Riga 94:


}( window.mediaWiki || window.mw ) );
}( window.mediaWiki || window.mw ) );
/* ============================================================
* Niente più auto-redirect cross-language.
* Per cambiare lingua: clicca sull'icona globo (ULS) o cambia URL.
* ============================================================ */