Zum Inhalt springen

MediaWiki:Gadget-CustomPopups.js: Unterschied zwischen den Versionen

Aus TerranianStar
Die Seite wurde neu angelegt: „mw.hook('wikipage.content').add(function() { // Popup-Container erzeugen 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; // API-Aufruf $.getJSON(mw.util.wikiScript('api'), { action: 'parse', page: title,…“
 
KKeine Bearbeitungszusammenfassung
 
(5 dazwischenliegende Versionen desselben Benutzers werden nicht angezeigt)
Zeile 1: Zeile 1:
mw.hook('wikipage.content').add(function() {
mw.hook('wikipage.content').add(function() {


     // Popup-Container erzeugen
     // Popup-Container erzeugen (einmalig)
     if (!$('#custom-popup').length) {
     if (!$('#custom-popup').length) {
         $('body').append('<div id="custom-popup"></div>');
         $('body').append('<div id="custom-popup"></div>');
Zeile 11: Zeile 11:
         if (!title) return;
         if (!title) return;


        // API-Aufruf
         $.getJSON(mw.util.wikiScript('api'), {
         $.getJSON(mw.util.wikiScript('api'), {
             action: 'parse',
             action: 'parse',
Zeile 18: Zeile 17:
             format: 'json'
             format: 'json'
         }).done(function(data) {
         }).done(function(data) {
            if (data.parse && data.parse.text) {
                const html = data.parse.text['*'];


                // Popup anzeigen
            if (!data.parse || !data.parse.text) return;
                $('#custom-popup')
 
                    .html(html)
            let html = $('<div>').html(data.parse.text['*']);
                    .css({
 
                        top: e.pageY + 10,
            // 1. Versuch: ersten sinnvollen Absatz finden
                        left: e.pageX + 10
let firstParagraph = null;
                    })
 
                    .show();
html.find('p').each(function() {
    const text = $(this).text().trim();
    if (text.length >= 3) {
        firstParagraph = $(this).html();
        return false;
    }
});
 
// 2. Wenn kein Absatz → ersten sichtbaren Textknoten suchen
if (!firstParagraph) {
    let fallbackText = "";
 
    // Alle Textknoten durchsuchen
    html.find('*').contents().each(function() {
        if (this.nodeType === 3) { // Textknoten
            const text = this.nodeValue.trim();
            if (text.length >= 3) {
                fallbackText = text;
                return false;
             }
             }
        }
    });
    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();
         });
         });



Aktuelle Version vom 9. Juni 2026, 12:08 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 finden
let firstParagraph = null;

html.find('p').each(function() {
    const text = $(this).text().trim();
    if (text.length >= 3) {
        firstParagraph = $(this).html();
        return false;
    }
});

// 2. Wenn kein Absatz → ersten sichtbaren Textknoten suchen
if (!firstParagraph) {
    let fallbackText = "";

    // Alle Textknoten durchsuchen
    html.find('*').contents().each(function() {
        if (this.nodeType === 3) { // Textknoten
            const text = this.nodeValue.trim();
            if (text.length >= 3) {
                fallbackText = text;
                return false;
            }
        }
    });

    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();
    });

});