Zum Inhalt springen

MediaWiki:Common.js: Unterschied zwischen den Versionen

Keine AdSense-Werbelinks (Ad intents) im Artikeltext: Klasse google-anno-skip für Artikelbereich und Banner
Lektoratsangabe: Kategorie auch in der mobilen Ansicht erkennen
 
(2 dazwischenliegende Versionen desselben Benutzers werden nicht angezeigt)
Zeile 51: Zeile 51:
ohneWerbelinks();
ohneWerbelinks();
$( 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 () {} );
} );
} );
/* ===== Lektoratsangabe in Medikamentenartikeln =====
  Jede Seite der Kategorie „Medikamente“ zeigt am Artikelanfang die Zeile aus Vorlage:Lektorat,
  auch wenn die Vorlage (noch) nicht im Artikel steht. Der Text kommt immer aus der Vorlage
  und wird hier nicht wiederholt. Ausnahme für einzelne Seiten: {{Lektorat|aus}} im Artikel. */
( function () {
var conf = mw.config.get( [ 'wgNamespaceNumber', 'wgAction', 'wgIsArticle', 'wgCategories', 'wgDiffNewId' ] );
var speicher = 'adhspedia-lektorat';
var haltbar = 60 * 60 * 1000;
if ( conf.wgNamespaceNumber !== 0 || conf.wgAction !== 'view' || !conf.wgIsArticle || conf.wgDiffNewId ) {
return;
}
/* Die mobile Ansicht liefert wgCategories nicht mit. Dort zählt die Kategorienzeile am Seitenende. */
function istMedikament() {
if ( conf.wgCategories ) {
return conf.wgCategories.indexOf( 'Medikamente' ) !== -1;
}
return $( '#catlinks a[title="Kategorie:Medikamente"]' ).length > 0;
}
function einsetzen( $ausgabe, html ) {
var $zeile = $( '<div>' ).append( $.parseHTML( html ) ).find( '.lektorat' ).first();
var $ziel = $ausgabe.children( 'section' ).first();
if ( !$zeile.length || $ausgabe.find( '.lektorat, .kein-lektorat' ).length ) {
return;
}
( $ziel.length ? $ziel : $ausgabe ).prepend( $zeile );
}
mw.hook( 'wikipage.content' ).add( function () {
var $ausgabe = $( '#mw-content-text' ).find( '.mw-parser-output' ).first();
var gemerkt = null;
if ( !$ausgabe.length || !istMedikament() || $ausgabe.find( '.lektorat, .kein-lektorat' ).length ) {
return;
}
try {
gemerkt = JSON.parse( window.sessionStorage.getItem( speicher ) || 'null' );
} catch ( e ) {}
if ( gemerkt && gemerkt.html && Date.now() - gemerkt.zeit < haltbar ) {
einsetzen( $ausgabe, gemerkt.html );
return;
}
mw.loader.using( 'mediawiki.api' ).then( function () {
return new mw.Api().get( {
action: 'parse',
text: '{{Lektorat}}',
contentmodel: 'wikitext',
prop: 'text',
wrapoutputclass: '',
disablelimitreport: 1,
formatversion: 2,
maxage: 3600,
smaxage: 3600
} );
} ).then( function ( daten ) {
var html = ( daten && daten.parse && daten.parse.text ) || '';
try {
window.sessionStorage.setItem( speicher, JSON.stringify( { html: html, zeit: Date.now() } ) );
} catch ( e ) {}
einsetzen( $ausgabe, html );
} );
} );
}() );
}() );