MediaWiki:Common.js: differenze tra le versioni

Admin (discussione | contributi)
Common.js v5: rimuovo auto-redirect IT-RU bidirezionale (causava loop e ritorni in russo). Lascio solo mobile cookie nuke + cleanup cookie autolang residui.
Admin (discussione | contributi)
Common.js v7: touch+viewport <=1200 = mobile (cattura Desktop Mode)
 
(2 versioni intermedie di uno stesso utente non sono mostrate)
Riga 1: Riga 1:
/* ============================================================
/* ============================================================
  * Common.js v5 — Wiki Methode Paret
  * Common.js v7 — Wiki Methode Paret
  * ============================================================
  * ============================================================
  *  
  * REGOLA UNICA:
* PRINCIPIO: Non interferire con le scelte dell'utente.
  *   URL /Titolo      → UI italiana
  * URL decide il contenuto. L'icona globo (ULS) decide la UI.
  *   URL /Titolo/ru    → UI russa
  * Niente redirect automatici incrociati.
  *
  *  
  * Mobile detection v7:
  * Le uniche logiche attive:
  *  - Touch capability (funziona anche con "Versione Desktop" del browser)
  *  1. Pulizia cookie velenosi mobile (stopMobileRedirect, mf_useformat)
  *   - Viewport width <= 1200 (più tollerante: cattura anche desktop mode 1080)
  *     → permette a MobileFrontend di servire sempre Minerva su UA mobile
  *  - Una delle due basta
  *  2. Pulizia cookie autolang_set residuo da versioni precedenti
*      → toglie l'auto-redirect bidirezionale che disturbava
  * ============================================================ */
  * ============================================================ */
( function () {
( function ( mw ) {
     'use strict';
     'use strict';


     // ─── Cookie helpers ─────────────────────────────────────
     var SUPPORTED_LANGS = [ 'ru', 'en', 'fr', 'es', 'pt' ];
 
     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 ──────────────────────────────
     // ─── 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 ( name ) {
         [ 'stopMobileRedirect', 'mf_useformat' ].forEach( function ( n ) {
             if ( getCookie( name ) !== null ) {
             if ( getCookie( n ) !== null ) deleteCookie( n );
                deleteCookie( name );
                mobileCookiesDeleted = true;
            }
         } );
         } );
     }
     }


     // ─── 2. Auto-lang residuo (cleanup definitivo) ──────────
     [ 'autolang_set', 'no_auto_lang_redirect' ].forEach( function ( n ) {
    // Le versioni precedenti del Common.js settavano autolang_set per redirect
         if ( getCookie( n ) !== null ) deleteCookie( n );
    // bidirezionali tra IT/RU. Lo cancelliamo SEMPRE: niente più auto-redirect.
     } );
    if ( getCookie( 'autolang_set' ) !== null ) {
         deleteCookie( 'autolang_set' );
    }
    if ( getCookie( 'no_auto_lang_redirect' ) !== null ) {
        deleteCookie( 'no_auto_lang_redirect' );
     }


     // ─── 3. Se mobile + appena pulito cookie + su Vector ───
     // ─── Force ?useformat=mobile se mobile + su Vector ─────
    //  → reload per ottenere Minerva
     if ( isMobile &&
     if ( mobileCookiesDeleted &&
         document.body &&
         document.body &&
         /skin-vector/.test( document.body.className ) ) {
         /skin-vector/.test( document.body.className ) ) {
         window.location.reload();
         var url = new URL( window.location.href );
        if ( url.searchParams.get( 'useformat' ) !== 'mobile' ) {
            url.searchParams.set( 'useformat', 'mobile' );
            window.location.replace( url.toString() );
            return;
        }
    }
 
    // ─── URL decide UI lingua ─────────────────────────────
    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';
 
        var match = pageName.match( /\/([a-z]{2})$/ );
        var desiredLang;
        if ( match && SUPPORTED_LANGS.indexOf( match[ 1 ] ) !== -1 ) {
            desiredLang = match[ 1 ];
        } else {
            desiredLang = contentLang;
        }
 
        if ( currentUI === desiredLang ) return;
 
        var action = mw.config.get( 'wgAction' );
        if ( action && action !== 'view' ) return;
 
        var ns = mw.config.get( 'wgNamespaceNumber' );
        if ( ns < 0 ) return;
 
        var url = new URL( window.location.href );
        if ( url.searchParams.get( 'uselang' ) ) return;
 
        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 auto-redirect IT↔RU.
* L'utente sceglie la lingua via icona globo (ULS) o URL.
* ============================================================ */