MediaWiki:Common.js: Difference between revisions

From semantic-hub.io
No edit summary
Tag: Reverted
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');


// Test Q5
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;
        return;
    }


     const box = document.createElement('div');
     const wrapper = document.createElement('div');
     box.id = 'qudt-auto-box';
     wrapper.style.margin = '1em 0';
    box.style.border = '1px solid #c8ccd1';
     wrapper.innerHTML = `
    box.style.background = '#f8f9fa';
         <h2>QUDT-Daten</h2>
    box.style.padding = '12px';
         <table id="qudt-table" class="wikitable">
    box.style.margin = '12px 0';
            <tbody>
     box.style.fontFamily = 'sans-serif';
                <tr><th>ID</th><td id="qudt-id">Lade...</td></tr>
 
                <tr><th>idShort</th><td id="qudt-idshort">Lade...</td></tr>
    box.innerHTML = `
                <tr><th>Preferred Name</th><td id="qudt-preferredName">Lade...</td></tr>
         <h2 style="margin-top:0;">QUDT-Test</h2>
                <tr><th>Symbol</th><td id="qudt-symbol">Lade...</td></tr>
         <div id="qudt-auto-status">Lade Daten für <strong>Volt</strong>...</div>
            </tbody>
        <pre id="qudt-auto-result" style="white-space:pre-wrap; margin-top:10px;"></pre>
        </table>
     `;
     `;
    target.prepend(wrapper);


     target.prepend(box);
     fetch('http://127.0.0.1:5000/api/v3/search?search=Volt&lang=en&types=unit')
 
    const statusEl = document.getElementById('qudt-auto-status');
    const resultEl = document.getElementById('qudt-auto-result');
 
    const url = 'http://127.0.0.1:5000/api/v3/search?search=Volt&lang=en&types=unit';
 
    fetch(url)
         .then(function (response) {
         .then(function (response) {
             if (!response.ok) {
             if (!response.ok) {
Line 42: Line 32:
         })
         })
         .then(function (data) {
         .then(function (data) {
             statusEl.textContent = 'Daten erfolgreich geladen.';
             const result = data.result || {};
             resultEl.textContent = JSON.stringify(data, null, 2);
            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) {
             statusEl.textContent = 'Fehler beim Laden.';
             document.getElementById('qudt-id').textContent = 'Fehler';
             resultEl.textContent = error.message;
            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';
        });
});