MediaWiki: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.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);
});
});
/* =========================================================
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();
}
}
});
}
})();
