MediaWiki:Common.js: differenze tra le versioni
Aspetto
Common.js v5: URL decide UI, niente cross-redirect, mobile nuke mantenuto |
Common.js v6: viewport <768 forza ?useformat=mobile |
||
| Riga 1: | Riga 1: | ||
/* ============================================================ | /* ============================================================ | ||
* Common.js | * 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) | ||
* | * | ||
* + Detection mobile via VIEWPORT WIDTH (più robusto di UA) | |||
* + mobile cookie nuke | |||
* | |||
* + mobile cookie nuke | |||
* + 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 | var isMobileUA = /iPhone|iPad|iPod|Android|Mobile|webOS|BlackBerry|Opera Mini|IEMobile|Silk/i.test( ua ); | ||
var | 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 ); | ||
} ); | } ); | ||
} | } | ||
| Riga 55: | Riga 52: | ||
} ); | } ); | ||
// ─── 3. | // ─── 3. FORCE MOBILE VIEW se viewport piccolo + su Vector ── | ||
if ( | // 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. | // Aggiungiamo ?useformat=mobile e ricarichiamo (forza Minerva) | ||
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'; | ||
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; | desiredLang = contentLang; | ||
} | } | ||
if ( currentUI === desiredLang ) return; | if ( currentUI === desiredLang ) return; | ||
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; | if ( ns < 0 ) return; | ||
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; | ||
url.searchParams.set( 'uselang', desiredLang ); | url.searchParams.set( 'uselang', desiredLang ); | ||
window.location.replace( url.toString() ); | window.location.replace( url.toString() ); | ||
| Riga 106: | Riga 104: | ||
/* ============================================================ | /* ============================================================ | ||
* | * Mobile via viewport width <768px. | ||
* | * Niente cross-redirect tra lingue. | ||
* ============================================================ */ | * ============================================================ */ | ||
Versione delle 19:48, 9 giu 2026
/* ============================================================
* Common.js v6 — Wiki Methode Paret
* ============================================================
* REGOLA UNICA:
* URL /Titolo → UI italiana
* URL /Titolo/ru → UI russa
* URL /Titolo/en → UI inglese (futuro)
*
* + Detection mobile via VIEWPORT WIDTH (più robusto di UA)
* + mobile cookie nuke
* + 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 isMobileUA = /iPhone|iPad|iPod|Android|Mobile|webOS|BlackBerry|Opera Mini|IEMobile|Silk/i.test( ua );
var viewportWidth = window.innerWidth || document.documentElement.clientWidth || 1024;
var isMobileViewport = viewportWidth < 768;
var isMobile = isMobileUA || isMobileViewport;
if ( isMobile ) {
[ 'stopMobileRedirect', 'mf_useformat' ].forEach( function ( n ) {
if ( getCookie( n ) !== null ) deleteCookie( n );
} );
}
// ─── 2. Cleanup cookie velenosi delle versioni precedenti ──
[ 'autolang_set', 'no_auto_lang_redirect' ].forEach( function ( n ) {
if ( getCookie( n ) !== null ) deleteCookie( n );
} );
// ─── 3. FORCE MOBILE VIEW se viewport piccolo + su Vector ──
// Robusto: anche se UA non viene riconosciuto, viewport width <768 è mobile
if ( isMobile &&
document.body &&
/skin-vector/.test( document.body.className ) ) {
// Aggiungiamo ?useformat=mobile e ricarichiamo (forza Minerva)
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
}
}
// ─── 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';
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 ) );
/* ============================================================
* Mobile via viewport width <768px.
* Niente cross-redirect tra lingue.
* ============================================================ */