MediaWiki

Difference between revisions of "Common.js"

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

Revision as of 07:53, 22 May 2018

// Replace Wikidata IDs with their [label, description]
if ( $( '' ).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 ].value,
						description = entity.descriptions[ lang ].value;
					
					node.html( label + ' <i>(' + node.text() + ')</i><br><small>' + description + '</small>' )
				} );
			}
		} );
	} );
}