MediaWiki
Difference between revisions of "Common.js"
Line 1: | Line 1: | ||
// Replace Wikidata IDs with their [label, description] | // Replace Wikidata IDs with their [label, description] | ||
− | if ( $( '' ).length > 0 ) { | + | if ( $( '.wb-external-id' ).length > 0 ) { |
mw.loader.using( 'mediawiki.ForeignApi', function() { | mw.loader.using( 'mediawiki.ForeignApi', function() { | ||
$( '.wb-external-id' ).each( function() { | $( '.wb-external-id' ).each( function() { | ||
Line 20: | Line 20: | ||
'origin': '*' | 'origin': '*' | ||
} ).then( function( data ) { | } ).then( function( data ) { | ||
+ | |||
var entity = data.entities[ node.text() ], | var entity = data.entities[ node.text() ], | ||
− | label = entity.labels[ lang ].value, | + | label = ( entity.labels[ lang ] !== undefined ? entity.labels[ lang ].value + ' <i>(' + node.text() + ')</i>' : node.text() ), |
− | description = entity.descriptions[ lang ].value; | + | description = ( entity.descriptions[ lang ] !== undefined ? '<small>' + entity.descriptions[ lang ].value + '</small>' : '' ); |
− | node.html( label + ' | + | node.html( label + '<br>' + description ) |
} ); | } ); | ||
} | } |
Revision as of 08:01, 22 May 2018
// Replace Wikidata IDs with their [label, description]
if ( $( '.wb-external-id' ).length > 0 ) {
mw.loader.using( 'mediawiki.ForeignApi', function() {
$( '.wb-external-id' ).each( function() {
if ( $( this ).attr( 'href' ).lastIndexOf( 'https://www.wikidata.org', 0 ) === 0 ) {
var wikidataApi = new mw.ForeignApi( 'https://www.wikidata.org/w/api.php', {
anonymous: true,
parameters: { 'origin': '*' },
ajax: { timeout: 10000 }
} ),
lang = mw.config.get( 'wgUserLanguage' ),
node = $( this );
wikidataApi.get( {
'action': 'wbgetentities',
'format': 'json',
'ids': node.text(),
'props': 'labels|descriptions',
'languages': lang,
'languagefallback': 1,
'origin': '*'
} ).then( function( data ) {
var entity = data.entities[ node.text() ],
label = ( entity.labels[ lang ] !== undefined ? entity.labels[ lang ].value + ' <i>(' + node.text() + ')</i>' : node.text() ),
description = ( entity.descriptions[ lang ] !== undefined ? '<small>' + entity.descriptions[ lang ].value + '</small>' : '' );
node.html( label + '<br>' + description )
} );
}
} );
} );
}