MediaWiki:Common.js: differenze tra le versioni

Admin (discussione | contributi)
Common.js v5: URL decide UI, niente cross-redirect, mobile nuke mantenuto
Admin (discussione | contributi)
Common.js v6: viewport <768 forza ?useformat=mobile
Riga 1: Riga 1:
/* ============================================================
/* ============================================================
  * Common.js v5 — Wiki Methode Paret
  * Common.js v6 — Wiki Methode Paret
  * ============================================================
  * ============================================================
  * REGOLA UNICA:
  * REGOLA UNICA:
Riga 6: Riga 6:
  *  URL /Titolo/ru    → UI russa
  *  URL /Titolo/ru    → UI russa
  *  URL /Titolo/en    → UI inglese (futuro)
  *  URL /Titolo/en    → UI inglese (futuro)
  *   etc.
  *
*
  * + Detection mobile via VIEWPORT WIDTH (più robusto di UA)
* Niente cookie che ricordano scelte. Niente cross-redirect.
  * + mobile cookie nuke
* L'URL determina sia il contenuto SIA la lingua dell'interfaccia.
  *  
  * + mobile cookie nuke (per MobileFrontend)
  * + cleanup cookie velenosi delle versioni precedenti
  * + cleanup cookie velenosi delle versioni precedenti
  * ============================================================ */
  * ============================================================ */
Riga 39: Riga 36:
     // ─── 1. Mobile cookie nuke (per MobileFrontend / Minerva) ──
     // ─── 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 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 isMobileViewport = viewportWidth < 768;
    var isMobile = isMobileUA || isMobileViewport;
 
     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;
            }
         } );
         } );
     }
     }
Riga 55: Riga 52:
     } );
     } );


     // ─── 3. Se mobile + appena pulito + su Vector → reload ─────
     // ─── 3. FORCE MOBILE VIEW se viewport piccolo + su Vector ──
     if ( mobileCookiesDeleted &&
    // Robusto: anche se UA non viene riconosciuto, viewport width <768 è mobile
     if ( isMobile &&
         document.body &&
         document.body &&
         /skin-vector/.test( document.body.className ) ) {
         /skin-vector/.test( document.body.className ) ) {
         window.location.reload();
         // Aggiungiamo ?useformat=mobile e ricarichiamo (forza Minerva)
        return;
        var url = new URL( window.location.href );
        if ( url.searchParams.get( 'useformat' ) !== 'mobile' ) {
            url.searchParams.set( 'useformat', 'mobile' );
            window.location.replace( url.toString() );
            return; // stop everything else
        }
     }
     }


Riga 71: Riga 74:
         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 79:
             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 106: Riga 104:


/* ============================================================
/* ============================================================
  * Niente più auto-redirect cross-language.
  * Mobile via viewport width <768px.
  * Per cambiare lingua: clicca sull'icona globo (ULS) o cambia URL.
  * Niente cross-redirect tra lingue.
  * ============================================================ */
  * ============================================================ */