MediaWiki:Common.js

From semantic-hub.io
Revision as of 20:00, 23 April 2026 by YvonneDHBW (talk | contribs)

Note: After publishing, 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)
  • Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
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';
        });
});