MediaWiki:ShortDescription.js

Revision as of 18:00, 21 November 2026 by Shraga (talk | contribs) (Show the short description under the article title)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
/* Short description under the article title (like Wikipedia's mobile site).
 * Shows the text of {{Short description|...}} when the article has one. Loaded from Common.js and Mobile.js. */
( function () {
	if ( mw.config.get( 'wgNamespaceNumber' ) !== 0 || mw.config.get( 'wgAction' ) !== 'view' ||
		mw.config.get( 'wgIsMainPage' ) || !mw.config.get( 'wgIsArticle' ) ) {
		return;
	}
	$.getJSON( mw.config.get( 'wgScriptPath' ) + '/api.php', {
		action: 'query', prop: 'revisions', rvprop: 'content', rvslots: 'main', rvsection: 0,
		pageids: mw.config.get( 'wgArticleId' ), format: 'json', formatversion: 2
	} ).then( function ( d ) {
		var page = d.query && d.query.pages && d.query.pages[ 0 ];
		var text = page && page.revisions && page.revisions[ 0 ].slots.main.content;
		var m = text && text.match( /\{\{\s*[Ss]hort description\s*\|\s*([^|}]+)/ );
		if ( !m ) {
			return;
		}
		var desc = m[ 1 ].trim();
		if ( !desc || desc.toLowerCase() === 'none' ) {
			return;
		}
		var css = document.createElement( 'style' );
		css.textContent = '.cp-shortdesc{display:block;font-family:sans-serif;font-size:14px;line-height:1.4;' +
			'font-weight:normal;color:#54595d;margin:4px 0 0;}' +
			'.cp-shortdesc-mobile{margin:-4px 0 8px;}';
		document.head.appendChild( css );
		var el = document.createElement( 'div' );
		el.className = 'cp-shortdesc';
		el.textContent = desc;
		var h = document.getElementById( 'firstHeading' );
		if ( document.querySelector( '.vector-page-titlebar' ) && h ) {
			h.appendChild( el );
			return;
		}
		var heading = document.querySelector( '.page-heading' ) || h;
		if ( heading ) {
			el.className += ' cp-shortdesc-mobile';
			heading.parentNode.insertBefore( el, heading.nextSibling );
		}
	} );
}() );