User:YvonneDHBW/common.js

From semantic-hub.io

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.using(['mediawiki.util']).then(function () {
    if (mw.config.get('wgPageName') !== 'Item:Q5') {
        return;
    }

    const container = document.createElement('div');
    container.style.border = '1px solid #ccc';
    container.style.padding = '1em';
    container.style.margin = '1em 0';
    container.style.background = '#f8f9fa';

    container.innerHTML = `
        <h2>QUDT-Test</h2>
        <input id="qudtSearchInput" type="text" value="Volt" />
        <button id="qudtSearchBtn">Volt laden</button>
        <pre id="qudtSearchResult">Noch keine Anfrage gesendet.</pre>
    `;

    const target = document.querySelector('#mw-content-text') || document.body;
    target.prepend(container);

    document.getElementById('qudtSearchBtn').addEventListener('click', async function () {
        const term = document.getElementById('qudtSearchInput').value.trim();
        const resultBox = document.getElementById('qudtSearchResult');

        if (!term) {
            resultBox.textContent = 'Bitte Suchbegriff eingeben.';
            return;
        }

        resultBox.textContent = 'Lade...';

        const url = 'http://127.0.0.1:5000/api/v3/search?search='
            + encodeURIComponent(term)
            + '&lang=en&types=unit';

        try {
            const response = await fetch(url);
            if (!response.ok) {
                throw new Error('HTTP ' + response.status);
            }

            const data = await response.json();
            resultBox.textContent = JSON.stringify(data, null, 2);
        } catch (err) {
            resultBox.textContent = 'Fehler: ' + err.message;
        }
    });
});