MediaWiki
ItemsSugar.js
Note: After saving, 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: Go to Menu → Settings (Opera → Preferences on a Mac) and then to Privacy & security → Clear browsing data → Cached images and files.
/* *************************************************************** */
/* GENERAL SUGAR (?) ********************************************* */
// Enhance external ids, such as Property:P12 (Wikidata ID), adds to its value their [label, description]
// Schematic example: "P12: Q25167" ⇒ "P12: bokmål (Q25167) langue/dialect"
// Currently Wikidata only.
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(), // some Qid
'props': 'labels|descriptions', // get labels and descriptions...
'languages': lang, // in user's language
'languagefallback': 1,
'origin': '*'
}).then(function(data) {
// Use returned data:
var entity = data.entities[node.text()],
label = (entity.labels[lang] !== undefined ?
entity.labels[lang].value + ' <i source="Common.js">(' + node.text() + ')</i>'
:'<text source="Common.js">' + node.text() + '</text>'),
description = (entity.descriptions[lang] !== undefined ?
'<small source="Common.js">' + entity.descriptions[lang].value + '</small>'
: '<small source="Common.js"></small>');
// Inject created elements
node.html(label + '<br>' + description);
});
}
});
});
}
// On record wikibase item, in title, fix language label so it match the word's language.
// If has records, has language, has language indicator in its title...
if ($('#P3').length > 0 && $('#P4') && $('.wikibase-title .wb-language-fallback-indicator')) {
$('#P4 .wikibase-statementview-mainsnak a').each(function() {
$('.wikibase-title .wb-language-fallback-indicator').text('<text source="Common.js">'+ $(this).text()+'</text>');
});
}
// Create audio player element, in OO.ui style
const BASE_FILE_URL = 'https://commons.wikimedia.org/wiki/Special:Redirect/file?wptype=file&wpvalue=';
function playButton(audioUrl) {
var button = new OO.ui.ButtonWidget({
framed: false,
icon: 'play',
title: 'play'
});
button.on('click', function() {
var audio = new Audio(audioUrl);
audio.play();
});
return button.$element;
}
// On record wikibase item, add audio player element
if ($('#P3 a.extiw').length > 0) {
mw.loader.using(['oojs-ui-widgets', 'oojs-ui.styles.icons-media'], function() {
$('#P3 a.extiw').each(function() {
var $node = $(this);
$node.before(playButton(BASE_FILE_URL + $node.text()));
});
});
}