Zum Inhalt springen

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

Aus TerranianStar
KKeine Bearbeitungszusammenfassung
KKeine Bearbeitungszusammenfassung
Zeile 34: Zeile 34:
                 if (
                 if (
                     !$(this).is('p') &&
                     !$(this).is('p') &&
                     !$(this).is('h1, h2, h3, h4, h5')
                     !$(this).is('h1') &&
                    !$(this).is('h2') &&
                    !$(this).is('h3') &&
                    !$(this).is('h4') &&
                    !$(this).is('h5')
                 ) {
                 ) {
                     $(this).remove();
                     $(this).remove();

Version vom 9. Juni 2026, 11:34 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;

        // API-Aufruf
        $.getJSON(mw.util.wikiScript('api'), {
            action: 'parse',
            page: title,
            prop: 'text',
            format: 'json'
        }).done(function(data) {

            if (!data.parse || !data.parse.text) return;

            // HTML in jQuery-Objekt umwandeln
            let html = $('<div>').html(data.parse.text['*']);

            // Unerwünschte Elemente entfernen
            html.find('.infobox').remove();      // Infoboxen
            html.find('table').remove();         // Tabellen (Navigation, Layout)
            html.find('.catlinks').remove();     // Kategorien
            html.find('style, script').remove(); // Skripte/Styles

            // Vorlagen entfernen (alles was direkt in mw-parser-output steht und kein Absatz/Überschrift ist)
            html.find('.mw-parser-output > *').each(function() {
                if (
                    !$(this).is('p') &&
                    !$(this).is('h1') &&
                    !$(this).is('h2') &&
                    !$(this).is('h3') &&
                    !$(this).is('h4') &&
                    !$(this).is('h5')
                ) {
                    $(this).remove();
                }
            });

            // Ersten echten Absatz finden
            let firstParagraph = null;

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

            // Wenn kein Absatz existiert → Überschrift als Fallback
            if (!firstParagraph) {
                let heading = html.find('h1, h2, h3').first().text().trim();
                if (heading.length > 0) {
                    firstParagraph = heading;
                } else {
                    firstParagraph = "(Kein Vorschautext verfügbar)";
                }
            }

            // Popup anzeigen
            $('#custom-popup')
                .html(firstParagraph)
                .css({
                    top: e.pageY + 10,
                    left: e.pageX + 10
                })
                .show();

        });

    }, function() {
        $('#custom-popup').hide();
    });

});