MediaWiki:Common.js: Difference between revisions
From semantic-hub.io
YvonneDHBW (talk | contribs) No edit summary Tag: Reverted |
YvonneDHBW (talk | contribs) No edit summary Tag: Reverted |
||
| Line 1: | Line 1: | ||
mw.loader.load('/w/index.php?title=MediaWiki:CustomExtensions&action=raw&ctype=text/javascript'); | mw.loader.load('/w/index.php?title=MediaWiki:CustomExtensions&action=raw&ctype=text/javascript'); | ||
mw.loader.using(['mediawiki.util']).then(function () { | mw.loader.using(['mediawiki.util']).then(function () { | ||
if (mw.config.get('wgPageName') !== 'Item:Q5' || mw.config.get('wgAction') !== 'view') { | if (mw.config.get('wgPageName') !== 'Item:Q5' || mw.config.get('wgAction') !== 'view') { | ||
| Line 9: | Line 7: | ||
const target = document.querySelector('#mw-content-text') || document.body; | const target = document.querySelector('#mw-content-text') || document.body; | ||
if (!target) | if (!target) return; | ||
const | const wrapper = document.createElement('div'); | ||
wrapper.style.margin = '1em 0'; | |||
wrapper.innerHTML = ` | |||
<h2>QUDT-Daten</h2> | |||
<table id="qudt-table" class="wikitable"> | |||
<tbody> | |||
<tr><th>ID</th><td id="qudt-id">Lade...</td></tr> | |||
<tr><th>idShort</th><td id="qudt-idshort">Lade...</td></tr> | |||
<tr><th>Preferred Name</th><td id="qudt-preferredName">Lade...</td></tr> | |||
<h2 | <tr><th>Symbol</th><td id="qudt-symbol">Lade...</td></tr> | ||
< | </tbody> | ||
</table> | |||
`; | `; | ||
target.prepend(wrapper); | |||
fetch('http://127.0.0.1:5000/api/v3/search?search=Volt&lang=en&types=unit') | |||
.then(function (response) { | .then(function (response) { | ||
if (!response.ok) { | if (!response.ok) { | ||
| Line 42: | Line 32: | ||
}) | }) | ||
.then(function (data) { | .then(function (data) { | ||
const result = data.result || {}; | |||
const embedded = (result.embeddedDataSpecifications || [])[0] || {}; | |||
const content = embedded.dataSpecificationContent || {}; | |||
const preferredName = content.preferredName?.value; | |||
const symbol = content.Symbol?.value; | |||
document.getElementById('qudt-id').textContent = result.id || '—'; | |||
document.getElementById('qudt-idshort').textContent = result.idShort || '—'; | |||
document.getElementById('qudt-preferredName').textContent = | |||
Array.isArray(preferredName) ? preferredName.map(v => v.value || v).join(', ') : (preferredName || '—'); | |||
document.getElementById('qudt-symbol').textContent = | |||
typeof symbol === 'object' && symbol !== null ? (symbol.value || JSON.stringify(symbol)) : (symbol || '—'); | |||
}) | }) | ||
.catch(function (error) { | .catch(function (error) { | ||
document.getElementById('qudt-id').textContent = 'Fehler'; | |||
document.getElementById('qudt-idshort').textContent = 'Fehler'; | |||
document.getElementById('qudt-preferredName').textContent = error.message; | |||
document.getElementById('qudt-symbol').textContent = 'Fehler'; | |||
}); | }); | ||
}); | }); | ||
Revision as of 20:00, 23 April 2026
mw.loader.load('/w/index.php?title=MediaWiki:CustomExtensions&action=raw&ctype=text/javascript');
mw.loader.using(['mediawiki.util']).then(function () {
if (mw.config.get('wgPageName') !== 'Item:Q5' || mw.config.get('wgAction') !== 'view') {
return;
}
const target = document.querySelector('#mw-content-text') || document.body;
if (!target) return;
const wrapper = document.createElement('div');
wrapper.style.margin = '1em 0';
wrapper.innerHTML = `
<h2>QUDT-Daten</h2>
<table id="qudt-table" class="wikitable">
<tbody>
<tr><th>ID</th><td id="qudt-id">Lade...</td></tr>
<tr><th>idShort</th><td id="qudt-idshort">Lade...</td></tr>
<tr><th>Preferred Name</th><td id="qudt-preferredName">Lade...</td></tr>
<tr><th>Symbol</th><td id="qudt-symbol">Lade...</td></tr>
</tbody>
</table>
`;
target.prepend(wrapper);
fetch('http://127.0.0.1:5000/api/v3/search?search=Volt&lang=en&types=unit')
.then(function (response) {
if (!response.ok) {
throw new Error('HTTP ' + response.status);
}
return response.json();
})
.then(function (data) {
const result = data.result || {};
const embedded = (result.embeddedDataSpecifications || [])[0] || {};
const content = embedded.dataSpecificationContent || {};
const preferredName = content.preferredName?.value;
const symbol = content.Symbol?.value;
document.getElementById('qudt-id').textContent = result.id || '—';
document.getElementById('qudt-idshort').textContent = result.idShort || '—';
document.getElementById('qudt-preferredName').textContent =
Array.isArray(preferredName) ? preferredName.map(v => v.value || v).join(', ') : (preferredName || '—');
document.getElementById('qudt-symbol').textContent =
typeof symbol === 'object' && symbol !== null ? (symbol.value || JSON.stringify(symbol)) : (symbol || '—');
})
.catch(function (error) {
document.getElementById('qudt-id').textContent = 'Fehler';
document.getElementById('qudt-idshort').textContent = 'Fehler';
document.getElementById('qudt-preferredName').textContent = error.message;
document.getElementById('qudt-symbol').textContent = 'Fehler';
});
});
