Jump to content

User:Enterprisey/search-links.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Enterprisey (talk | contribs) at 07:40, 27 August 2019 (create somewhat buggy script). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)
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.
// vim: ts=4 sw=4 et ai
// <nowiki>
$.when(
    $.ready,
    mw.loader.load( [ "mediawiki.util" ] )
).then( function () {
    var CHECK_INTERVAL = 50; // milliseconds

    // The main setup function that inserts links into the suggestions.
    function insertSearchLinks() {
        $( ".suggestions-results" ).children()
            .wrap( "<div class='suggestion-container'></div>" )
            .after( function () {
                return $( "<a>" )
                    .addClass( "suggestion-link" )
                    .attr( "href", mw.util.getUrl( $( this ).text(),
                        { action: "edit" } ) )
                    .text( "(edit)" );
            } );

        $( ".suggestion-container" ).on( "mouseover", function () {
            $( this ).addClass( "suggestions-result-current" );
        } );

        $( ".suggestion-container" ).on( "mouseout", function () {
            $( this ).removeClass( "suggestions-result-current" );
        } );
    }

    // Wire up the handler for searching
    $( "#searchInput" ).on( "input", function () {
        var newVal = $( this ).val();
        var suggestionsEl = $( ".suggestions" ).first();
        
        var checkForSearchUpdate = function () {
            if( suggestionsEl.find( ".special-query" ).text() === newVal ) {
                console.log(newVal);
                insertSearchLinks();
            } else {
                setTimeout( checkForSearchUpdate, CHECK_INTERVAL );
            }
        };

        checkForSearchUpdate();
    } );

    mw.loader.addStyleTag( ".suggestions-result { display: inline; }" +
        ".suggestion-link { z-index: 1; position: relative }" +
        ".suggestion-container { position: relative; }" +
    ".suggestions-results .mw-searchSuggest-link { position: static }" +
    ".suggestions-results .mw-searchSuggest-link::before { content: ''; position: absolute; z-index: 0; left: 0; top: 0; right: 0; bottom: 0 }"+
    //".suggestion-container:hover * { color: white; }");
    ".suggestions-result-current * { color: white; }");
} );

// </nowiki>