MediaWiki:Common.js: Difference between revisions

From semantic-hub.io
No edit summary
Tag: Reverted
No edit summary
Tag: Reverted
Line 6: Line 6:
     }
     }


     const target = document.querySelector('#mw-content-text') || document.body;
     const apiUrl = 'http://127.0.0.1:5000/api/v3/search?search=Volt&lang=en&types=unit';
    if (!target) return;


     const wrapper = document.createElement('div');
     function normalizeText(text) {
    wrapper.style.margin = '1em 0';
        return (text || '')
    wrapper.innerHTML = `
             .replace(/\s+/g, ' ')
        <h2>QUDT-Daten</h2>
            .trim()
        <table id="qudt-table" class="wikitable">
             .toLowerCase();
             <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')
     function extractValues(data) {
        const result = data?.result || {};
        const embedded = (result.embeddedDataSpecifications || [])[0] || {};
        const content = embedded.dataSpecificationContent || {};
 
        const preferredNameRaw = content.preferredName?.value;
        const symbolRaw = content.Symbol?.value;
 
        let preferredName = '—';
        if (Array.isArray(preferredNameRaw)) {
            preferredName = preferredNameRaw
                .map(v => typeof v === 'object' && v !== null ? (v.value || JSON.stringify(v)) : String(v))
                .join(', ');
        } else if (preferredNameRaw) {
            preferredName = typeof preferredNameRaw === 'object'
                ? (preferredNameRaw.value || JSON.stringify(preferredNameRaw))
                : String(preferredNameRaw);
        }
 
        let symbol = '—';
        if (symbolRaw) {
            symbol = typeof symbolRaw === 'object'
                ? (symbolRaw.value || JSON.stringify(symbolRaw))
                : String(symbolRaw);
        }
 
        return {
            'id': result.id || '—',
            'idshort': result.idShort || '—',
            'preferred name': preferredName,
            'preferredname': preferredName,
            'symbol': symbol
        };
    }
 
    function setCellValue(row, value) {
        if (!row) return false;
 
        let valueCell =
            row.querySelector('td') ||
            row.querySelector('.wikibase-statementview-mainsnak-container') ||
            row.querySelector('.wikibase-snakview-value') ||
            row.querySelector('.wikibase-statementview-mainsnak');
 
        if (!valueCell) return false;
 
        const target =
            valueCell.querySelector('.wikibase-snakview-value') ||
            valueCell;
 
        target.textContent = value;
        return true;
    }
 
    function fillExistingTable(values) {
        const rows = document.querySelectorAll('tr, .wikibase-statementview');
 
        let filled = 0;
 
        rows.forEach(function (row) {
            const headerText = normalizeText(row.textContent);
 
            if (!headerText) return;
 
            if (headerText.includes('idshort')) {
                if (setCellValue(row, values['idshort'])) filled++;
            } else if (headerText.includes('preferred name') || headerText.includes('preferredname')) {
                if (setCellValue(row, values['preferredname'])) filled++;
            } else if (headerText.includes('symbol')) {
                if (setCellValue(row, values['symbol'])) filled++;
            } else if (headerText === 'id' || headerText.includes(' id ')) {
                if (setCellValue(row, values['id'])) filled++;
            }
        });
 
        return filled;
    }
 
    function showDebugFallback(values, errorText) {
        const target = document.querySelector('#mw-content-text') || document.body;
        if (!target) return;
 
        const box = document.createElement('div');
        box.style.border = '1px solid #c8ccd1';
        box.style.background = '#fff8e1';
        box.style.padding = '12px';
        box.style.margin = '12px 0';
        box.innerHTML = `
            <strong>QUDT-Testdaten</strong><br>
            ${errorText ? '<div style="color:#a33; margin-top:6px;">' + errorText + '</div>' : ''}
            <pre style="white-space:pre-wrap; margin-top:8px;"></pre>
        `;
        box.querySelector('pre').textContent = JSON.stringify(values, null, 2);
        target.prepend(box);
    }
 
    fetch(apiUrl)
         .then(function (response) {
         .then(function (response) {
             if (!response.ok) {
             if (!response.ok) {
Line 32: Line 119:
         })
         })
         .then(function (data) {
         .then(function (data) {
             const result = data.result || {};
             const values = extractValues(data);
             const embedded = (result.embeddedDataSpecifications || [])[0] || {};
             const filled = fillExistingTable(values);
            const content = embedded.dataSpecificationContent || {};


             const preferredName = content.preferredName?.value;
             if (filled === 0) {
            const symbol = content.Symbol?.value;
                showDebugFallback(values, 'Keine passende Property-Tabelle bzw. Zeile gefunden.');
 
             }
            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';
             showDebugFallback({}, 'Fehler beim Laden: ' + 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:04, 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 apiUrl = 'http://127.0.0.1:5000/api/v3/search?search=Volt&lang=en&types=unit';

    function normalizeText(text) {
        return (text || '')
            .replace(/\s+/g, ' ')
            .trim()
            .toLowerCase();
    }

    function extractValues(data) {
        const result = data?.result || {};
        const embedded = (result.embeddedDataSpecifications || [])[0] || {};
        const content = embedded.dataSpecificationContent || {};

        const preferredNameRaw = content.preferredName?.value;
        const symbolRaw = content.Symbol?.value;

        let preferredName = '—';
        if (Array.isArray(preferredNameRaw)) {
            preferredName = preferredNameRaw
                .map(v => typeof v === 'object' && v !== null ? (v.value || JSON.stringify(v)) : String(v))
                .join(', ');
        } else if (preferredNameRaw) {
            preferredName = typeof preferredNameRaw === 'object'
                ? (preferredNameRaw.value || JSON.stringify(preferredNameRaw))
                : String(preferredNameRaw);
        }

        let symbol = '—';
        if (symbolRaw) {
            symbol = typeof symbolRaw === 'object'
                ? (symbolRaw.value || JSON.stringify(symbolRaw))
                : String(symbolRaw);
        }

        return {
            'id': result.id || '—',
            'idshort': result.idShort || '—',
            'preferred name': preferredName,
            'preferredname': preferredName,
            'symbol': symbol
        };
    }

    function setCellValue(row, value) {
        if (!row) return false;

        let valueCell =
            row.querySelector('td') ||
            row.querySelector('.wikibase-statementview-mainsnak-container') ||
            row.querySelector('.wikibase-snakview-value') ||
            row.querySelector('.wikibase-statementview-mainsnak');

        if (!valueCell) return false;

        const target =
            valueCell.querySelector('.wikibase-snakview-value') ||
            valueCell;

        target.textContent = value;
        return true;
    }

    function fillExistingTable(values) {
        const rows = document.querySelectorAll('tr, .wikibase-statementview');

        let filled = 0;

        rows.forEach(function (row) {
            const headerText = normalizeText(row.textContent);

            if (!headerText) return;

            if (headerText.includes('idshort')) {
                if (setCellValue(row, values['idshort'])) filled++;
            } else if (headerText.includes('preferred name') || headerText.includes('preferredname')) {
                if (setCellValue(row, values['preferredname'])) filled++;
            } else if (headerText.includes('symbol')) {
                if (setCellValue(row, values['symbol'])) filled++;
            } else if (headerText === 'id' || headerText.includes(' id ')) {
                if (setCellValue(row, values['id'])) filled++;
            }
        });

        return filled;
    }

    function showDebugFallback(values, errorText) {
        const target = document.querySelector('#mw-content-text') || document.body;
        if (!target) return;

        const box = document.createElement('div');
        box.style.border = '1px solid #c8ccd1';
        box.style.background = '#fff8e1';
        box.style.padding = '12px';
        box.style.margin = '12px 0';
        box.innerHTML = `
            <strong>QUDT-Testdaten</strong><br>
            ${errorText ? '<div style="color:#a33; margin-top:6px;">' + errorText + '</div>' : ''}
            <pre style="white-space:pre-wrap; margin-top:8px;"></pre>
        `;
        box.querySelector('pre').textContent = JSON.stringify(values, null, 2);
        target.prepend(box);
    }

    fetch(apiUrl)
        .then(function (response) {
            if (!response.ok) {
                throw new Error('HTTP ' + response.status);
            }
            return response.json();
        })
        .then(function (data) {
            const values = extractValues(data);
            const filled = fillExistingTable(values);

            if (filled === 0) {
                showDebugFallback(values, 'Keine passende Property-Tabelle bzw. Zeile gefunden.');
            }
        })
        .catch(function (error) {
            showDebugFallback({}, 'Fehler beim Laden: ' + error.message);
        });
});