Benutzer:Sarah/common.js

Aus Bahaiworks
Wechseln zu:Navigation, Suche

Hinweis: Leere nach dem Veröffentlichen den Browser-Cache, um die Änderungen sehen zu können.

  • Firefox/Safari: Umschalttaste drücken und gleichzeitig Aktualisieren anklicken oder entweder Strg+F5 oder Strg+R (⌘+R auf dem Mac) drücken
  • Google Chrome: Umschalttaste+Strg+R (⌘+Umschalttaste+R auf dem Mac) drücken
  • Edge: Strg+F5 drücken oder Strg drücken und gleichzeitig Aktualisieren anklicken
$( function() {
    mw.util.addCSS(
        '.paragraph-permalink { \
            position: absolute; \
            left: -20px; \
            color: #666; \
            text-decoration: none; \
            opacity: 0.5; \
            transition: opacity 0.3s; \
        } \
        .paragraph-permalink:hover { \
            opacity: 1; \
            color: #000; \
        } \
        .mw-parser-output > p { \
            position: relative; \
        }'
    );

    $( '.mw-parser-output > p' ).each( function ( idx, elem ) {
        // Ensure unique, predictable ID
        var paragraphId = 'paragraph-' + (idx + 1);
        $( elem ).attr('id', paragraphId);
        
        // Create a clickable link with multiple methods
        var $link = $( '<a>', {
            'class': 'paragraph-permalink',
            'href': '#' + paragraphId,
            'title': 'Direct link to this paragraph',
            'text': '§'
        }).on('click', function(e) {
            e.preventDefault();
            
            // Multiple methods to ensure copy works
            var url = window.location.href.split('#')[0] + '#' + paragraphId;
            
            // Attempt clipboard copy
            if (navigator.clipboard) {
                navigator.clipboard.writeText(url).then(() => {
                    mw.notify("Paragraph link copied!");
                });
            }
            
            // Fallback: select and copy
            var tempInput = $('<input>').val(url).appendTo('body');
            tempInput.select();
            document.execCommand('copy');
            tempInput.remove();
            
            // Ensure navigation works
            window.location.hash = paragraphId;
        });
        
        $( elem ).prepend($link);
    } );
} );