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)
- Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
- Opera: Press Ctrl-F5.
// required modules: mediawiki.api, jquery.wikibase.wbtooltip
( function ( mw, $, wb ) {
// only works for entities
if ( !mw.config.exists( 'wbEntityId' ) ) {
return;
}
function init() {
var api = new mw.Api(),
userLang = mw.config.get( 'wgUserLanguage' ),
ids = [],
anchors = {},
$tooltip = $( '<span>' )
.appendTo( 'body' );
$(
'.wikibase-statementgroupview a[href^="/wiki/"], ' +
'.language-lexical-category-widget a[href^="/wiki/"], ' +
'.wikibase-lexeme-form-grammatical-features-values 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;
}
if ( ids.indexOf( id ) === -1 ) {
ids.push( id );
}
if ( anchors[ id ] ) {
anchors[ id ].push( $anchor );
} else {
anchors[ id ] = [ $anchor ];
}
} );
while ( ids.length > 0 ) {
var subset = ids.splice( 0, 50 );
api.get( {
action: 'wbgetentities',
ids: subset,
props: [ 'descriptions' ],
languages: userLang,
languagefallback: 1,
} )
.then( function ( data ) {
$.each( data.entities || {}, function ( id, entity ) {
if ( !entity.descriptions[ userLang ] || !anchors[ id ] ) {
return;
}
$.each( anchors[ id ], function ( i, $anchor ) {
$anchor.hover( function () {
var tooltip = $tooltip
.wbtooltip( {
content: $( '<span>' ).text( entity.descriptions[ userLang ].value ),
permanent: true,
gravity: 'nw',
$anchor: $anchor
} )
.data( 'wbtooltip' );
if ( tooltip ) tooltip.show();
}, function () {
var tooltip = $tooltip.data( 'wbtooltip' );
if ( tooltip ) tooltip.hide();
} );
} );
} );
} );
}
}
mw.hook( 'wikibase.entityPage.entityView.rendered' ).add( init );
} ( mediaWiki, jQuery, wikibase ) );