MediaWiki:Common.js: Unterschied zwischen den Versionen
Erscheinungsbild
L (Diskussion | Beiträge) Keine AdSense-Werbelinks (Ad intents) im Artikeltext: Klasse google-anno-skip für Artikelbereich und Banner |
L (Diskussion | Beiträge) Hauptseite: neueste Artikel und Artikelzahl für Besucher aktuell halten (Datei-Cache) |
||
| Zeile 52: | Zeile 52: | ||
$( ohneWerbelinks ); | $( ohneWerbelinks ); | ||
}() ); | }() ); | ||
/* ===== Hauptseite: neue Artikel und Artikelzahl aktuell halten ===== | |||
Besucher bekommen die Hauptseite aus dem Datei-Cache, also im Stand der letzten Änderung der Seite. | |||
Dieses Skript holt die neuesten Artikel und die Artikelzahl frisch aus der API und tauscht beides aus. | |||
Ohne JavaScript bleibt die Liste aus dem Wikitext stehen ({{Spezial:Neue Seiten}}). */ | |||
mw.loader.using( 'mediawiki.util' ).then( function () { | |||
if ( !mw.config.get( 'wgIsMainPage' ) || mw.config.get( 'wgAction' ) !== 'view' ) { | |||
return; | |||
} | |||
$( function () { | |||
var box = document.querySelector( '.hpa-neu-auto' ); | |||
if ( !box || !window.fetch || !window.Intl ) { | |||
return; | |||
} | |||
/* Anzahl wie im Wikitext eingestellt (data-anzahl), sonst so viele, wie schon in der Liste stehen */ | |||
var anzahl = parseInt( box.getAttribute( 'data-anzahl' ), 10 ) || box.querySelectorAll( 'li' ).length || 10; | |||
var url = mw.util.wikiScript( 'api' ) + '?action=query&format=json&formatversion=2' + | |||
'&list=recentchanges&rctype=new&rcnamespace=0&rcshow=!redirect&rcprop=title%7Ctimestamp&rclimit=' + anzahl + | |||
'&meta=siteinfo&siprop=statistics&maxage=300&smaxage=300'; | |||
fetch( url ).then( function ( antwort ) { | |||
return antwort.json(); | |||
} ).then( function ( daten ) { | |||
var neu = ( daten.query && daten.query.recentchanges ) || []; | |||
var statistik = daten.query && daten.query.statistics; | |||
var zahl = document.querySelector( '.hpa-zahl-ziffer' ); | |||
if ( zahl && statistik && statistik.articles ) { | |||
zahl.textContent = statistik.articles.toLocaleString( 'de-DE' ); | |||
} | |||
if ( !neu.length ) { | |||
return; | |||
} | |||
var alt = Array.prototype.map.call( box.querySelectorAll( '.mw-newpages-pagename' ), function ( a ) { | |||
return a.textContent; | |||
} ).join( '\n' ); | |||
var frisch = neu.map( function ( e ) { | |||
return e.title; | |||
} ).join( '\n' ); | |||
if ( alt === frisch ) { | |||
return; | |||
} | |||
var datum = new Intl.DateTimeFormat( 'de-DE', { day: 'numeric', month: 'long', year: 'numeric', timeZone: 'Europe/Berlin' } ); | |||
var teil = document.createDocumentFragment(); | |||
var tag = ''; | |||
var liste; | |||
neu.forEach( function ( e ) { | |||
var t = datum.format( new Date( e.timestamp ) ); | |||
if ( t !== tag ) { | |||
tag = t; | |||
var kopf = document.createElement( 'h4' ); | |||
kopf.className = 'mw-index-pager-list-header'; | |||
kopf.textContent = t; | |||
teil.appendChild( kopf ); | |||
liste = document.createElement( 'ul' ); | |||
teil.appendChild( liste ); | |||
} | |||
var li = document.createElement( 'li' ); | |||
var bdi = document.createElement( 'bdi' ); | |||
var a = document.createElement( 'a' ); | |||
a.className = 'mw-newpages-pagename'; | |||
a.href = mw.util.getUrl( e.title ); | |||
a.title = e.title; | |||
a.textContent = e.title; | |||
bdi.appendChild( a ); | |||
li.appendChild( bdi ); | |||
liste.appendChild( li ); | |||
} ); | |||
box.textContent = ''; | |||
box.appendChild( teil ); | |||
} ).catch( function () {} ); | |||
} ); | |||
} ); | |||