MediaWiki:Common.js: differenze tra le versioni
Aspetto
Common.js v3: aggiunto auto-redirect-original (UI italiana + URL /ru = torna a Italian page) |
Common.js v7: touch+viewport <=1200 = mobile (cattura Desktop Mode) |
||
| (4 versioni intermedie di uno stesso utente non sono mostrate) | |||
| Riga 1: | Riga 1: | ||
/* ============================================================ | /* ============================================================ | ||
* | * Common.js v7 — Wiki Methode Paret | ||
* ============================================================ | * ============================================================ | ||
* | * REGOLA UNICA: | ||
* | * URL /Titolo → UI italiana | ||
* URL /Titolo/ru → UI russa | |||
* | |||
* | * Mobile detection v7: | ||
* - Touch capability (funziona anche con "Versione Desktop" del browser) | |||
* | * - Viewport width <= 1200 (più tollerante: cattura anche desktop mode 1080) | ||
* | * - Una delle due basta | ||
* ( | |||
* | |||
* | |||
* ============================================================ */ | * ============================================================ */ | ||
( function ( mw ) { | ( function ( mw ) { | ||
| Riga 18: | Riga 15: | ||
var SUPPORTED_LANGS = [ 'ru', 'en', 'fr', 'es', 'pt' ]; | var SUPPORTED_LANGS = [ 'ru', 'en', 'fr', 'es', 'pt' ]; | ||
function getCookie( name ) { | function getCookie( name ) { | ||
var | var m = document.cookie.match( new RegExp( '(?:^|;\\s*)' + name + '=([^;]*)' ) ); | ||
return | return m ? decodeURIComponent( m[ 1 ] ) : null; | ||
} | } | ||
function deleteCookie( name ) { | |||
function | var hostname = window.location.hostname; | ||
var | 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; | |||
} ); | |||
} | } | ||
// ─── DETECTION MOBILE ROBUSTA ────────────────────────── | |||
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 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 ) { | |||
[ 'stopMobileRedirect', 'mf_useformat' ].forEach( function ( n ) { | |||
if ( getCookie( n ) !== null ) deleteCookie( n ); | |||
} ); | |||
} | } | ||
function | [ 'autolang_set', 'no_auto_lang_redirect' ].forEach( function ( n ) { | ||
if ( getCookie( n ) !== null ) deleteCookie( n ); | |||
} ); | |||
} | |||
// ─── Force ?useformat=mobile se mobile + su Vector ───── | |||
var | if ( isMobile && | ||
document.body && | |||
/skin-vector/.test( document.body.className ) ) { | |||
var url = new URL( window.location.href ); | |||
if ( url.searchParams.get( 'useformat' ) !== 'mobile' ) { | |||
url.searchParams.set( 'useformat', 'mobile' ); | |||
window.location.replace( url.toString() ); | |||
return; | |||
} | |||
} | } | ||
function | // ─── 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 | 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 ( | if ( currentUI === desiredLang ) return; | ||
var action = mw.config.get( 'wgAction' ); | |||
var | if ( action && action !== 'view' ) return; | ||
var ns = mw.config.get( 'wgNamespaceNumber' ); | |||
if ( ns < 0 ) return; | |||
var | |||
var url = new URL( window.location.href ); | |||
if ( url.searchParams.get( 'uselang' ) ) return; | |||
var | |||
if ( | |||
url.searchParams.set( 'uselang', desiredLang ); | |||
window.location.replace( url.toString() ); | |||
} | } | ||
if ( typeof mw !== 'undefined' && mw.config ) { | if ( typeof mw !== 'undefined' && mw.config ) { | ||
mw.loader.using( [ 'mediawiki.util | mw.loader.using( [ 'mediawiki.util' ] ).then( syncUILangFromURL ); | ||
} | } | ||
}( window.mediaWiki || window.mw ) ); | }( window.mediaWiki || window.mw ) ); | ||
Versione attuale delle 19:58, 9 giu 2026
/* ============================================================
* Common.js v7 — Wiki Methode Paret
* ============================================================
* REGOLA UNICA:
* URL /Titolo → UI italiana
* URL /Titolo/ru → UI russa
*
* Mobile detection v7:
* - Touch capability (funziona anche con "Versione Desktop" del browser)
* - Viewport width <= 1200 (più tollerante: cattura anche desktop mode 1080)
* - Una delle due basta
* ============================================================ */
( function ( mw ) {
'use strict';
var SUPPORTED_LANGS = [ 'ru', 'en', 'fr', 'es', 'pt' ];
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;
} );
}
// ─── DETECTION MOBILE ROBUSTA ──────────────────────────
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 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 ) {
[ 'stopMobileRedirect', 'mf_useformat' ].forEach( function ( n ) {
if ( getCookie( n ) !== null ) deleteCookie( n );
} );
}
[ 'autolang_set', 'no_auto_lang_redirect' ].forEach( function ( n ) {
if ( getCookie( n ) !== null ) deleteCookie( n );
} );
// ─── Force ?useformat=mobile se mobile + su Vector ─────
if ( isMobile &&
document.body &&
/skin-vector/.test( document.body.className ) ) {
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 ) );