MediaWiki:Common.js: differenze tra le versioni

Admin (discussione | contributi)
Common.js v6: viewport <768 forza ?useformat=mobile
Admin (discussione | contributi)
Common.js v7: touch+viewport <=1200 = mobile (cattura Desktop Mode)
 
Riga 1: Riga 1:
/* ============================================================
/* ============================================================
  * Common.js v6 — 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)
  *
  *
  * + Detection mobile via VIEWPORT WIDTH (più robusto di UA)
  * Mobile detection v7:
  * + mobile cookie nuke
*  - Touch capability (funziona anche con "Versione Desktop" del browser)
  * + cleanup cookie velenosi delle versioni precedenti
  *   - Viewport width <= 1200 (più tollerante: cattura anche desktop mode 1080)
  *   - Una delle due basta
  * ============================================================ */
  * ============================================================ */
( function ( mw ) {
( function ( mw ) {
Riga 16: 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 34: Riga 33:
     }
     }


     // ─── 1. Mobile cookie nuke (per MobileFrontend / Minerva) ──
     // ─── DETECTION MOBILE ROBUSTA ──────────────────────────
     var ua = navigator.userAgent || '';
     var ua = navigator.userAgent || '';
     var isMobileUA = /iPhone|iPad|iPod|Android|Mobile|webOS|BlackBerry|Opera Mini|IEMobile|Silk/i.test( ua );
     var isMobileUA = /iPhone|iPad|iPod|Android|Mobile|webOS|BlackBerry|Opera Mini|IEMobile|Silk/i.test( ua );
     var viewportWidth = window.innerWidth || document.documentElement.clientWidth || 1024;
     var viewportWidth = window.innerWidth || document.documentElement.clientWidth || 1024;
     var isMobileViewport = viewportWidth < 768;
     var isSmallViewport = viewportWidth <= 1200;
     var isMobile = isMobileUA || isMobileViewport;
    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 ) {
Riga 47: Riga 52:
     }
     }


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


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


     // ─── 4. LOGICA UNICA: URL decide UI ───────────────────────
     // ─── URL decide UI lingua ─────────────────────────────
     function syncUILangFromURL() {
     function syncUILangFromURL() {
         if ( typeof mw === 'undefined' || !mw.config ) return;
         if ( typeof mw === 'undefined' || !mw.config ) return;
Riga 102: Riga 104:


}( window.mediaWiki || window.mw ) );
}( window.mediaWiki || window.mw ) );
/* ============================================================
* Mobile via viewport width <768px.
* Niente cross-redirect tra lingue.
* ============================================================ */