MediaWiki
Difference between revisions of "ItemsSugar.js"
(Create MediaWiki:ItemsSugar.js) |
m |
||
(2 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
+ | /* ************************************************************************** */ | ||
+ | /* ItemsSugar.js ******************************************* */ | ||
+ | // Description: enhance Qid and Property pages via small, Lingualibre-specific improvements. | ||
+ | // Manual: n.a. See code and inline comments. | ||
+ | // Usage: on records, languages Qid pages. | ||
+ | // Hack pad: n.a. | ||
+ | // Documentations: n.a. | ||
+ | // Author: 0x010C, Yug. | ||
/* *************************************************************** */ | /* *************************************************************** */ | ||
Line 28: | Line 36: | ||
var entity = data.entities[node.text()], | var entity = data.entities[node.text()], | ||
label = (entity.labels[lang] !== undefined ? | label = (entity.labels[lang] !== undefined ? | ||
− | entity.labels[lang].value + ' <i source="Common.js">(' + node.text() + ')</i>' | + | entity.labels[lang].value + ' <i script-source="MediaWiki:Common.js">(' + node.text() + ')</i>' |
− | :'<text source="Common.js">' + node.text() + '</text>'), | + | :'<text script-source="MediaWiki:Common.js">' + node.text() + '</text>'), |
description = (entity.descriptions[lang] !== undefined ? | description = (entity.descriptions[lang] !== undefined ? | ||
− | '<small source="Common.js">' + entity.descriptions[lang].value + '</small>' | + | '<small script-source="MediaWiki:Common.js">' + entity.descriptions[lang].value + '</small>' |
− | : '<small source="Common.js"></small>'); | + | : '<small script-source="MediaWiki:Common.js"></small>'); |
// Inject created elements | // Inject created elements | ||
node.html(label + '<br>' + description); | node.html(label + '<br>' + description); | ||
Line 45: | Line 53: | ||
if ($('#P3').length > 0 && $('#P4') && $('.wikibase-title .wb-language-fallback-indicator')) { | if ($('#P3').length > 0 && $('#P4') && $('.wikibase-title .wb-language-fallback-indicator')) { | ||
$('#P4 .wikibase-statementview-mainsnak a').each(function() { | $('#P4 .wikibase-statementview-mainsnak a').each(function() { | ||
− | $('.wikibase-title .wb-language-fallback-indicator'). | + | $('.wikibase-title .wb-language-fallback-indicator').html('<text script-source="MediaWiki:Common.js">'+ $(this).text()+'</text>'); |
}); | }); | ||
} | } |
Latest revision as of 21:48, 27 May 2022
/* ************************************************************************** */
/* ItemsSugar.js ******************************************* */
// Description: enhance Qid and Property pages via small, Lingualibre-specific improvements.
// Manual: n.a. See code and inline comments.
// Usage: on records, languages Qid pages.
// Hack pad: n.a.
// Documentations: n.a.
// Author: 0x010C, Yug.
/* *************************************************************** */
/* 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 script-source="MediaWiki:Common.js">(' + node.text() + ')</i>'
:'<text script-source="MediaWiki:Common.js">' + node.text() + '</text>'),
description = (entity.descriptions[lang] !== undefined ?
'<small script-source="MediaWiki:Common.js">' + entity.descriptions[lang].value + '</small>'
: '<small script-source="MediaWiki: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').html('<text script-source="MediaWiki: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()));
});
});
}