User:DaxServer/test.js

From Wikipedia, the free encyclopedia

This is an old revision of this page, as edited by DaxServer (talk | contribs) at 17:57, 15 October 2021. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
// <nowiki>
$.when(
    $.ready,
    mw.loader.using( [ "mediawiki.util", "oojs-ui-core", "oojs-ui-widgets" ] )
).then( function () {
    $( "span.mw-headline" ).each( function () {
        let popup = null;
        $( this ).after( " ", $( "<a>", { "class": "copy-section-link-pilcrow" } )
                .text( "¶" )
                .click( function () {
                    if( popup === null ) {
                        var hash = "#" + $( this ).prev().attr( "id" ).replace( /</g, '&lt;' );
                        var oldid = mw.util.getParamValue( "oldid" );
                        var popupContent;
                        function makeContent( pageName, id ) {
                            var wikitext = "[[" + (pageName + hash).replace( /_/g, " " ).replace( /</g, '&lt;' ) + "]]";
                            return $( '<p>', { "class": "copy-section-link-content" } ).append(
                                $( "<code>", { "id": "copy-section-wikilink" + id } ).text( wikitext ),
                                $( "<button>" )
                                    .text( "Copy" )
                                    .css( { "padding": "0.5em", "cursor": "pointer", "margin-left": "0.5em" } )
                                    .click( function () {
                                        navigator.clipboard.writeText( $( "#copy-section-wikilink" + id ).text() );
                                    } ),
                                    "<br /><a href='" + mw.util.getUrl( pageName ) + hash + "' class='external free'>external</a>"
                                );
                        }

                        var generalCss = { 'font-size': '0.9rem', 'font-family': 'sans-serif' };

                        var index;
                        if( oldid ) {
                            popupContent = makeContent( "Special:Permalink/" + oldid );
                            popupContent.css( generalCss );
                            popupContent.css( { 'padding-top': '0.5em', 'font-weight': 'normal' } );
                        } else {
                            var normalPanel = new OO.ui.TabPanelLayout( 'normal', {
                                label: 'Link',
                                $content: makeContent( mw.config.get( 'wgPageName' ), 'normal' )
                            } );
                            var permalinkPanel = new OO.ui.TabPanelLayout( 'permalink', {
                                label: 'Permalink',
                                $content: makeContent( 'Special:Permalink/' + mw.config.get( 'wgCurRevisionId' ), 'permalink' )
                            } );
                            index = new OO.ui.IndexLayout();
                            index.addTabPanels( [ normalPanel, permalinkPanel ] );
                            popupContent = index.$element;
                        }
                        popup = new OO.ui.PopupWidget( {
                            $content: popupContent,
                            $floatableContainer: $( this ),
                            padded: true,
                            width: 400,
                            height: 190,
                            align: 'forwards',
                            autoClose: true,
                        } );
                        $( this ).after( popup.$element );
                        if( index ) {
                            index.$menu.find( 'span.oo-ui-labelElement-label' ).css( generalCss );
                            index.$content.css( generalCss );
                        }

                        popup.toggle( true );
                    } else {
                        popup.toggle();
                    }
                } ) )
        ;
    } );
    mw.util.addCSS( "h2 .copy-section-link-pilcrow," +
                    "h3 .copy-section-link-pilcrow," +
                    "h4 .copy-section-link-pilcrow," +
                    "h5 .copy-section-link-pilcrow," +
                    "h6 .copy-section-link-pilcrow" +
                        "{ color: lightgray }" +
                    "h2:hover .copy-section-link-pilcrow," +
                    "h3:hover .copy-section-link-pilcrow," +
                    "h4:hover .copy-section-link-pilcrow," +
                    "h5:hover .copy-section-link-pilcrow," +
                    "h6:hover .copy-section-link-pilcrow" +
                        "{ color: inherit }" );
} );
// </nowiki>