MediaWiki:Common.js
Aspetto
Nota: dopo aver pubblicato, potrebbe essere necessario pulire la cache del proprio browser per vedere i cambiamenti.
- Firefox / Safari: tieni premuto il tasto delle maiuscole Shift e fai clic su Ricarica, oppure premi Ctrl-F5 o Ctrl-R (⌘-R su Mac)
- Google Chrome: premi Ctrl-Shift-R (⌘-Shift-R su un Mac)
- Edge: tieni premuto il tasto Ctrl e fai clic su Aggiorna, oppure premi Ctrl-F5.
/* LoroPedia: elenco di articoli casuali in Pagina principale.
Cambia a ogni caricamento della pagina. */
mw.loader.using( [ 'mediawiki.api', 'mediawiki.util' ] ).then( function () {
var box = document.getElementById( 'loropedia-random' );
if ( !box ) {
return;
}
function carica() {
new mw.Api().get( {
action: 'query',
list: 'random',
rnnamespace: 0,
rnfilterredir: 'nonredirects',
rnlimit: 9,
formatversion: 2
} ).then( function ( data ) {
var ul = document.createElement( 'ul' );
( data.query.random || [] )
.filter( function ( p ) {
return p.title !== 'Pagina principale';
} )
.slice( 0, 8 )
.forEach( function ( p ) {
var li = document.createElement( 'li' ),
a = document.createElement( 'a' );
a.href = mw.util.getUrl( p.title );
a.textContent = p.title;
li.appendChild( a );
ul.appendChild( li );
} );
box.innerHTML = '';
box.appendChild( ul );
var btn = document.createElement( 'button' );
btn.className = 'lp-btn';
btn.textContent = '🎲 Altri articoli';
btn.onclick = carica;
box.appendChild( btn );
}, function () {
box.textContent = 'Impossibile caricare gli articoli.';
} );
}
carica();
} );
/* LoroPedia: carosello di immagini in Pagina principale.
Pesca a caso fino a 10 immagini caricate e porta all'articolo che le usa. */
mw.loader.using( [ 'mediawiki.api', 'mediawiki.util' ] ).then( function () {
var box = document.getElementById( 'loropedia-carousel' );
if ( !box ) {
return;
}
new mw.Api().get( {
action: 'query',
generator: 'random',
grnnamespace: 6,
grnlimit: 10,
prop: 'imageinfo|fileusage',
iiprop: 'url|mime',
iiurlwidth: 900,
funamespace: 0,
fulimit: 50,
formatversion: 2
} ).then( function ( data ) {
var pages = ( ( data.query && data.query.pages ) || [] ).filter( function ( p ) {
return p.imageinfo && /^image\//.test( p.imageinfo[ 0 ].mime );
} );
if ( !pages.length ) {
return; // nessuna immagine: il carosello resta nascosto
}
var slides = pages.map( function ( p ) {
var info = p.imageinfo[ 0 ],
uso = p.fileusage && p.fileusage[ 0 ] && p.fileusage[ 0 ].title,
nome = p.title.replace( /^[^:]+:/, '' ).replace( /\.[^.]+$/, '' ).replace( /_/g, ' ' ),
slide = document.createElement( 'a' ),
img = document.createElement( 'img' ),
cap = document.createElement( 'span' );
slide.className = 'lp-slide';
slide.href = mw.util.getUrl( uso || p.title );
img.src = info.thumburl || info.url;
img.alt = nome;
img.loading = 'lazy';
cap.className = 'lp-caption';
cap.textContent = uso || nome;
slide.appendChild( img );
slide.appendChild( cap );
box.appendChild( slide );
return slide;
} );
var i = 0, timer;
var dots = document.createElement( 'div' );
dots.className = 'lp-dots';
slides.forEach( function ( s, n ) {
var d = document.createElement( 'button' );
d.setAttribute( 'aria-label', 'Immagine ' + ( n + 1 ) );
d.onclick = function () { vai( n ); };
dots.appendChild( d );
} );
function vai( n ) {
i = ( n + slides.length ) % slides.length;
slides.forEach( function ( s, k ) { s.classList.toggle( 'lp-active', k === i ); } );
Array.prototype.forEach.call( dots.children, function ( d, k ) { d.classList.toggle( 'lp-active', k === i ); } );
clearInterval( timer );
timer = setInterval( function () { vai( i + 1 ); }, 5000 );
}
if ( slides.length > 1 ) {
[ [ 'lp-prev', '‹', -1 ], [ 'lp-next', '›', 1 ] ].forEach( function ( b ) {
var btn = document.createElement( 'button' );
btn.className = 'lp-arrow ' + b[ 0 ];
btn.textContent = b[ 1 ];
btn.onclick = function () { vai( i + b[ 2 ] ); };
box.appendChild( btn );
} );
box.appendChild( dots );
}
box.style.display = '';
vai( 0 );
} );
} );