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
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 18: Zeile 18:
             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)
            // HTML in jQuery-Objekt umwandeln
                     .css({
            let html = $('<div>').html(data.parse.text['*']);
                        top: e.pageY + 10,
 
                        left: e.pageX + 10
            // Unerwünschte Elemente entfernen
                     })
            html.find('.infobox').remove();      // Infoboxen
                     .show();
            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, h2, h3, h4, 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();
         });
         });



Version vom 9. Juni 2026, 11:30 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, h2, h3, h4, 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();
    });

});