User:Evad37/WikidataWatchlistLabels.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
This user script seems to have a documentation page at User:Evad37/WikidataWatchlistLabels. |
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));
}
});
});