Jump to content

User:Evad37/WikidataWatchlistLabels.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
function getLabel(itemnumbers) {
	var langCode = mw.config.get('wgUserLanguage');
	$.ajax( {
		url: '//www.wikidata.org/w/api.php',
		data: {
			'format': 'json',
			'action': 'wbgetentities',
			'ids': itemnumbers.join('|'),
			'props': 'labels',
			'languages': langCode,
			'maxage': 30,
			'smaxage': 30
		},
		dataType: 'jsonp',
		headers: { 'Api-User-Agent': 'WikidataWatchlistLabels ( https://en.wikipedia.org/wiki/User:Evad37/WikidataWatchlistLabels.js )' },
		cache: true
	} )
	.done( function ( data ) {
		if ( data.success ) {
			for ( var item in data.entities ) {
				// Get label
				var label = '[?]';
				if ( data.entities[item].labels && data.entities[item].labels[langCode] && data.entities[item].labels[langCode].value ) {
					label = data.entities[item].labels[langCode].value;
                }
				// Add labels to items
				$( "span." + item + " > a").empty().append(
					$('<span>').addClass('labelFromWikidata').text(label + ' '),
					$('<span>').addClass('wikidataItemParenthesis').css('font-size', '85%').text('('),
					$('<span>').css('font-size', '85%').text(item),
					$('<span>').addClass('wikidataItemParenthesis').css('font-size', '85%').text(')')
				);
            }
            // Hide any labels that duplicate local page titles
            $("a.wb-entity-link > span.labelFromWikidata").each(function() {
                var $pageTitle = $(this).parent().parent().siblings('.mw-title');
                if ( $pageTitle.length && $pageTitle.text() === $(this).text().trim() ) {
                    $(this).hide();
                    $(this).siblings(".wikidataItemParenthesis").hide();
                }
            });
		}
	});
}

$( function($) {
	if( mw.config.get('wgNamespaceNumber') != -1 ) {
	// only operate in Special: namespace
	return;
	}
	// Hook on to changes in page content - initial load, 'New filters for edit review' changes, etc
	mw.hook( 'wikipage.content' ).add( function ( $content ) {
		var items = [];
		// Find item and property links in comments
		$content.find(
            // Standard watchlist/rc
            "li.mw-changeslist-src-mw-wikibase > span.comment > a.external, "+
            // "Group changes by page" watchlist/rc, single edit
            "table.mw-changeslist-src-mw-wikibase td.mw-changeslist-line-inner span.comment > a.external, "+
            // "Group changes by page" watchlist/rc, multiple edits
			"table.mw-changeslist-line.mw-collapsible tr.mw-changeslist-src-mw-wikibase td.mw-enhanced-rc-nested span.comment > a.external"
		).each(function() {
			if ( $(this).attr('href').indexOf('//www.wikidata.org/wiki/') !== -1 ) {
				var pq = $( this ).text();
				var pqnumber = pq.replace("Property:","");
				// Test for validity
				if ( !/^(p|q)\d+$/i.test(pqnumber) ) {
					return;
				}
				$( this ).wrap("<span class='" + pqnumber + "'></span>" );
				if ( items.indexOf(pqnumber) === -1 ) {
					items.push(pqnumber);
				}
			}
		});
		// Find items following page titles
		$content.find(
            // Standard watchlist/rc
			"li.mw-changeslist-src-mw-wikibase > a.wb-entity-link, "+
            // "Group changes by page" watchlist/rc, single edit
			"table.mw-changeslist-src-mw-wikibase td.mw-changeslist-line-inner a.wb-entity-link, "+
            // "Group changes by page" watchlist/rc, multiple edits
			"table.mw-changeslist-line.mw-collapsible tr.mw-changeslist-src-mw-wikibase td.mw-enhanced-rc-nested > a.wb-entity-link"
		).each(function() {
			var qnum = $( this ).text();
			$( this ).wrap("<span class='" + qnum + "'></span>" );
			if ( items.indexOf(qnum) === -1 ) {
				items.push(qnum);
			}
		});
		// Get labels in batches of 50 (max for Wikidata api)
		for ( var i=0; i<items.length; i += 50 ) {
			getLabel(items.slice(i, i+49));
		}
	});
});