User:Fomafix/MediaWiki:Gadget-Descriptions.js

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.
// required modules: wikibase.store.EntityStore, jquery.wikibase.wbtooltip

( function ( mw, $, wb ) {

	// only works for items
	if ( !mw.config.exists( 'wbEntityId' ) ) {
		return;
	}

	function init() {
		var entityStore = new wb.store.EntityStore();
		wb.compileEntityStoreFromMwConfig( entityStore );

		$( '.wb-claimlistview a[href^="/wiki/"]' )
		.each( function() {
			var $anchor = $( this ),
				id = $anchor.attr( 'href' ).replace( /.*(Q\d+)/, '$1' ).replace( /.*Property:(P\d+)/, '$1' );
			if ( id.indexOf( 'Q' ) !== 0 && id.indexOf( 'P' ) !== 0 ) {
				return;
			}

			entityStore.get( id )
			.done( function ( entity ) {
				var description = entity.getContent().getDescription();
				if ( !description ) {
					return;
				}

				var $tooltip = $( '<span>' )
					.appendTo( 'body' )
					.wbtooltip( {
					        content: $( '<span>' ).text( description ),
					        permanent: true,
					        gravity: 'nw',
					        $anchor: $anchor
					} );

				$anchor.hover( function() {
					$tooltip.data( 'wbtooltip' ).show();
				}, function() {
					$tooltip.data( 'wbtooltip' ).hide();
				} );
			} );
		} );
	}

	mw.loader.using( ['wikibase.store.EntityStore', 'jquery.wikibase.wbtooltip'], function () {
		$( init );
	} );

} ( mediaWiki, jQuery, wikibase ) );