MediaWiki:Gadget-CustomPopups.js: Unterschied zwischen den Versionen
Erscheinungsbild
Gerard (Diskussion | Beiträge) KKeine Bearbeitungszusammenfassung |
Gerard (Diskussion | Beiträge) KKeine Bearbeitungszusammenfassung |
||
| Zeile 11: | Zeile 11: | ||
if (!title) return; | if (!title) return; | ||
$.getJSON(mw.util.wikiScript('api'), { | $.getJSON(mw.util.wikiScript('api'), { | ||
action: 'parse', | action: 'parse', | ||
| Zeile 21: | Zeile 20: | ||
if (!data.parse || !data.parse.text) return; | if (!data.parse || !data.parse.text) return; | ||
let html = $('<div>').html(data.parse.text['*']); | let html = $('<div>').html(data.parse.text['*']); | ||
// | // 1. Versuch: ersten sinnvollen Absatz nehmen | ||
let firstParagraph = null; | |||
html.find('p').each(function() { | |||
html.find(' | const text = $(this).text().trim(); | ||
if (text.length >= 3) { | |||
firstParagraph = $(this).html(); | |||
return false; | |||
} | } | ||
}); | }); | ||
// | // 2. Fallback: gesamten sichtbaren Text, wenn kein Absatz gefunden | ||
if (!firstParagraph) { | if (!firstParagraph) { | ||
const fallbackText = html.text().trim(); | |||
if ( | if (fallbackText.length >= 3) { | ||
firstParagraph = | firstParagraph = fallbackText; | ||
} else { | } else { | ||
firstParagraph = "(Kein Vorschautext verfügbar)"; | firstParagraph = "(Kein Vorschautext verfügbar)"; | ||
| Zeile 79: | Zeile 43: | ||
} | } | ||
$('#custom-popup') | $('#custom-popup') | ||
.html(firstParagraph) | .html(firstParagraph) | ||
Version vom 9. Juni 2026, 12:01 Uhr
mw.hook('wikipage.content').add(function() {
// Popup-Container erzeugen (einmalig)
if (!$('#custom-popup').length) {
$('body').append('<div id="custom-popup"></div>');
}
// Hover-Effekt für interne Links
$('a').hover(function(e) {
const title = $(this).attr('title');
if (!title) return;
$.getJSON(mw.util.wikiScript('api'), {
action: 'parse',
page: title,
prop: 'text',
format: 'json'
}).done(function(data) {
if (!data.parse || !data.parse.text) return;
let html = $('<div>').html(data.parse.text['*']);
// 1. Versuch: ersten sinnvollen Absatz nehmen
let firstParagraph = null;
html.find('p').each(function() {
const text = $(this).text().trim();
if (text.length >= 3) {
firstParagraph = $(this).html();
return false;
}
});
// 2. Fallback: gesamten sichtbaren Text, wenn kein Absatz gefunden
if (!firstParagraph) {
const fallbackText = html.text().trim();
if (fallbackText.length >= 3) {
firstParagraph = fallbackText;
} else {
firstParagraph = "(Kein Vorschautext verfügbar)";
}
}
$('#custom-popup')
.html(firstParagraph)
.css({
top: e.pageY + 10,
left: e.pageX + 10
})
.show();
});
}, function() {
$('#custom-popup').hide();
});
});