MediaWiki:Common.js: Difference between revisions

From semantic-hub.io
No edit summary
Tag: Reverted
Undo revision 574 by Admin (talk)
 
(4 intermediate revisions by 2 users not shown)
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');


// Note: I had to comment out the following parts since it crashed the global JS
//      23.04.26, 23:30 P.Wagner (FoP Consult)


// 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') {
//        return;
         return;
//    }
     }
//
 
//    const apiUrl = 'http://127.0.0.1:5000/api/v3/search?search=Volt&lang=en&types=unit';
     const target = document.querySelector('#mw-content-text') || document.body;
//
    if (!target) {
//    function normalizeText(text) {
         return;
//         return (text || '')
     }
//            .replace(/\s+/g, ' ')
 
//            .trim()
     const box = document.createElement('div');
//            .toLowerCase();
    box.id = 'qudt-auto-box';
//     }
    box.style.border = '1px solid #c8ccd1';
//
    box.style.background = '#f8f9fa';
//     function extractValues(data) {
    box.style.padding = '12px';
//        const result = data?.result || {};
    box.style.margin = '12px 0';
//        const embedded = (result.embeddedDataSpecifications || [])[0] || {};
    box.style.fontFamily = 'sans-serif';
//        const content = embedded.dataSpecificationContent || {};
 
//
    box.innerHTML = `
//        const preferredNameRaw = content.preferredName?.value;
        <h2 style="margin-top:0;">QUDT-Test</h2>
//        const symbolRaw = content.Symbol?.value;
        <div id="qudt-auto-status">Lade Daten für <strong>Volt</strong>...</div>
//
        <pre id="qudt-auto-result" style="white-space:pre-wrap; margin-top:10px;"></pre>
//        let preferredName = '—';
    `;
//        if (Array.isArray(preferredNameRaw)) {
 
//            preferredName = preferredNameRaw
    target.prepend(box);
//                .map(v => typeof v === 'object' && v !== null ? (v.value || JSON.stringify(v)) : String(v))
 
//                .join(', ');
     const statusEl = document.getElementById('qudt-auto-status');
//        } else if (preferredNameRaw) {
     const resultEl = document.getElementById('qudt-auto-result');
//            preferredName = typeof preferredNameRaw === 'object'
 
//                ? (preferredNameRaw.value || JSON.stringify(preferredNameRaw))
     const url = 'http://127.0.0.1:5000/api/v3/search?search=Volt&lang=en&types=unit';
//                : String(preferredNameRaw);
 
//        }
    fetch(url)
//
         .then(function (response) {
//        let symbol = '—';
            if (!response.ok) {
//        if (symbolRaw) {
                throw new Error('HTTP ' + response.status);
//            symbol = typeof symbolRaw === 'object'
            }
//                ? (symbolRaw.value || JSON.stringify(symbolRaw))
            return response.json();
//                : String(symbolRaw);
         })
//        }
         .then(function (data) {
//
             statusEl.textContent = 'Daten erfolgreich geladen.';
//        return {
            resultEl.textContent = JSON.stringify(data, null, 2);
//            'id': result.id || '—',
        })
//            'idshort': result.idShort || '—',
        .catch(function (error) {
//            'preferred name': preferredName,
            statusEl.textContent = 'Fehler beim Laden.';
//            'preferredname': preferredName,
            resultEl.textContent = error.message;
//            '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);
//        });
// });
//
//
// /* =========================================================
//    Design
//    ========================================================= */
// (function () {
//  function ready(fn) {
//    if (document.readyState !== "loading") fn();
//    else document.addEventListener("DOMContentLoaded", fn);
//  }
//
//  ready(function () {
//    enhanceExternalLinks();
//    wrapWideTables();
//    improveTOC();
//    addCopyButtonsToCode();
//    addBackToTop();
//    improveSearchFocus();
//  });
//
//  function enhanceExternalLinks() {
//    var links = document.querySelectorAll('#mw-content-text a[href^="http"]');
//     links.forEach(function (link) {
//      if (location.hostname && !link.href.includes(location.hostname)) {
//        link.setAttribute('target', '_blank');
//        link.setAttribute('rel', 'noopener noreferrer');
//        if (!link.querySelector('.sh-external-indicator')) {
//          var mark = document.createElement('span');
//          mark.className = 'sh-external-indicator';
//          mark.setAttribute('aria-hidden', 'true');
//          mark.textContent = ' ↗';
//          link.appendChild(mark);
//        }
//      }
//    });
//  }
//
//  function wrapWideTables() {
//     var tables = document.querySelectorAll('.wikitable');
//    tables.forEach(function (table) {
//       if (table.parentElement && !table.parentElement.classList.contains('sh-table-wrap')) {
//         var wrap = document.createElement('div');
//         wrap.className = 'sh-table-wrap';
//        wrap.style.overflowX = 'auto';
//        wrap.style.margin = '1rem 0';
//        table.parentNode.insertBefore(wrap, table);
//         wrap.appendChild(table);
//      }
//    });
//  }
//
//  function improveTOC() {
//    var toc = document.querySelector('#toc, .toc');
//    if (!toc) return;
//
//    var title = toc.querySelector('.toctitle');
//    var list = toc.querySelector('ul');
//    if (!title || !list) return;
//
//    var button = document.createElement('button');
//    button.type = 'button';
//    button.className = 'sh-toc-toggle';
//    button.textContent = 'Inhalt ein-/ausblenden';
//    button.style.marginTop = '0.5rem';
//    button.style.fontSize = '0.95rem';
//    button.style.cursor = 'pointer';
//
//    button.addEventListener('click', function () {
//      var hidden = list.style.display === 'none';
//      list.style.display = hidden ? '' : 'none';
//    });
//
//    title.appendChild(button);
//  }
//
//  function addCopyButtonsToCode() {
//    var blocks = document.querySelectorAll('pre');
//    blocks.forEach(function (block) {
//      if (block.querySelector('.sh-copy-btn')) return;
//
//      block.style.position = 'relative';
//
//      var btn = document.createElement('button');
//      btn.type = 'button';
//      btn.className = 'sh-copy-btn';
//      btn.textContent = 'Kopieren';
//      btn.style.position = 'absolute';
//      btn.style.top = '8px';
//      btn.style.right = '8px';
//      btn.style.padding = '6px 10px';
//      btn.style.fontSize = '0.85rem';
//      btn.style.borderRadius = '8px';
//
//      btn.addEventListener('click', function () {
//         var text = block.innerText;
//         navigator.clipboard.writeText(text).then(function () {
//          btn.textContent = 'Kopiert';
//          setTimeout(function () {
//             btn.textContent = 'Kopieren';
//          }, 1500);
//        });
//      });
//
//      block.appendChild(btn);
//    });
//  }
//
//  function addBackToTop() {
//    var btn = document.createElement('button');
//    btn.type = 'button';
//    btn.textContent = '↑ Oben';
//    btn.setAttribute('aria-label', 'Zurück nach oben');
//    btn.style.position = 'fixed';
//    btn.style.right = '16px';
//    btn.style.bottom = '16px';
//    btn.style.zIndex = '999';
//    btn.style.padding = '10px 14px';
//    btn.style.borderRadius = '999px';
//    btn.style.display = 'none';
//    btn.style.boxShadow = '0 8px 20px rgba(0,0,0,.15)';
//
//    btn.addEventListener('click', function () {
//      window.scrollTo({ top: 0, behavior: 'smooth' });
//    });
//
//    document.body.appendChild(btn);
//
//    window.addEventListener('scroll', function () {
//      btn.style.display = window.scrollY > 500 ? 'block' : 'none';
//    });
//  }
//
//  function improveSearchFocus() {
//    var search = document.querySelector('#searchInput, input[name="search"]');
//    if (!search) return;
//
//    search.setAttribute('placeholder', 'Semantic Definition, semanticId oder Begriff suchen …');
//
//    document.addEventListener('keydown', function (e) {
//      if (e.key === '/' && document.activeElement !== search) {
//        var tag = document.activeElement && document.activeElement.tagName;
//        if (tag !== 'INPUT' && tag !== 'TEXTAREA') {
//          e.preventDefault();
//          search.focus();
//         }
//      }
//    });
//  }
// })();

Latest revision as of 21:31, 23 April 2026

mw.loader.load('/w/index.php?title=MediaWiki:CustomExtensions&action=raw&ctype=text/javascript');

// Note: I had to comment out the following parts since it crashed the global JS
//       23.04.26, 23:30 P.Wagner (FoP Consult)

// 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);
//         });
// });
// 
// 
// /* =========================================================
//    Design
//    ========================================================= */
// (function () {
//   function ready(fn) {
//     if (document.readyState !== "loading") fn();
//     else document.addEventListener("DOMContentLoaded", fn);
//   }
// 
//   ready(function () {
//     enhanceExternalLinks();
//     wrapWideTables();
//     improveTOC();
//     addCopyButtonsToCode();
//     addBackToTop();
//     improveSearchFocus();
//   });
// 
//   function enhanceExternalLinks() {
//     var links = document.querySelectorAll('#mw-content-text a[href^="http"]');
//     links.forEach(function (link) {
//       if (location.hostname && !link.href.includes(location.hostname)) {
//         link.setAttribute('target', '_blank');
//         link.setAttribute('rel', 'noopener noreferrer');
//         if (!link.querySelector('.sh-external-indicator')) {
//           var mark = document.createElement('span');
//           mark.className = 'sh-external-indicator';
//           mark.setAttribute('aria-hidden', 'true');
//           mark.textContent = ' ↗';
//           link.appendChild(mark);
//         }
//       }
//     });
//   }
// 
//   function wrapWideTables() {
//     var tables = document.querySelectorAll('.wikitable');
//     tables.forEach(function (table) {
//       if (table.parentElement && !table.parentElement.classList.contains('sh-table-wrap')) {
//         var wrap = document.createElement('div');
//         wrap.className = 'sh-table-wrap';
//         wrap.style.overflowX = 'auto';
//         wrap.style.margin = '1rem 0';
//         table.parentNode.insertBefore(wrap, table);
//         wrap.appendChild(table);
//       }
//     });
//   }
// 
//   function improveTOC() {
//     var toc = document.querySelector('#toc, .toc');
//     if (!toc) return;
// 
//     var title = toc.querySelector('.toctitle');
//     var list = toc.querySelector('ul');
//     if (!title || !list) return;
// 
//     var button = document.createElement('button');
//     button.type = 'button';
//     button.className = 'sh-toc-toggle';
//     button.textContent = 'Inhalt ein-/ausblenden';
//     button.style.marginTop = '0.5rem';
//     button.style.fontSize = '0.95rem';
//     button.style.cursor = 'pointer';
// 
//     button.addEventListener('click', function () {
//       var hidden = list.style.display === 'none';
//       list.style.display = hidden ? '' : 'none';
//     });
// 
//     title.appendChild(button);
//   }
// 
//   function addCopyButtonsToCode() {
//     var blocks = document.querySelectorAll('pre');
//     blocks.forEach(function (block) {
//       if (block.querySelector('.sh-copy-btn')) return;
// 
//       block.style.position = 'relative';
// 
//       var btn = document.createElement('button');
//       btn.type = 'button';
//       btn.className = 'sh-copy-btn';
//       btn.textContent = 'Kopieren';
//       btn.style.position = 'absolute';
//       btn.style.top = '8px';
//       btn.style.right = '8px';
//       btn.style.padding = '6px 10px';
//       btn.style.fontSize = '0.85rem';
//       btn.style.borderRadius = '8px';
// 
//       btn.addEventListener('click', function () {
//         var text = block.innerText;
//         navigator.clipboard.writeText(text).then(function () {
//           btn.textContent = 'Kopiert';
//           setTimeout(function () {
//             btn.textContent = 'Kopieren';
//           }, 1500);
//         });
//       });
// 
//       block.appendChild(btn);
//     });
//   }
// 
//   function addBackToTop() {
//     var btn = document.createElement('button');
//     btn.type = 'button';
//     btn.textContent = '↑ Oben';
//     btn.setAttribute('aria-label', 'Zurück nach oben');
//     btn.style.position = 'fixed';
//     btn.style.right = '16px';
//     btn.style.bottom = '16px';
//     btn.style.zIndex = '999';
//     btn.style.padding = '10px 14px';
//     btn.style.borderRadius = '999px';
//     btn.style.display = 'none';
//     btn.style.boxShadow = '0 8px 20px rgba(0,0,0,.15)';
// 
//     btn.addEventListener('click', function () {
//       window.scrollTo({ top: 0, behavior: 'smooth' });
//     });
// 
//     document.body.appendChild(btn);
// 
//     window.addEventListener('scroll', function () {
//       btn.style.display = window.scrollY > 500 ? 'block' : 'none';
//     });
//   }
// 
//   function improveSearchFocus() {
//     var search = document.querySelector('#searchInput, input[name="search"]');
//     if (!search) return;
// 
//     search.setAttribute('placeholder', 'Semantic Definition, semanticId oder Begriff suchen …');
// 
//     document.addEventListener('keydown', function (e) {
//       if (e.key === '/' && document.activeElement !== search) {
//         var tag = document.activeElement && document.activeElement.tagName;
//         if (tag !== 'INPUT' && tag !== 'TEXTAREA') {
//           e.preventDefault();
//           search.focus();
//         }
//       }
//     });
//   }
// })();