Jump to content

User:Enterprisey/search-links.js

From Wikipedia, the free encyclopedia
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.
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>
$( function () {
    function insertSearchLinks() {
        $( ".suggestions-results" )
            .children()
            .children()
            .append( function () {
                try {
                    var titleOfSuggestion = $( this ).text(),
                        editUrl = mw.util.getUrl( titleOfSuggestion, { "action": "edit" } );
                    return $( "<a>" )
                        .addClass( "suggestion-link" )
                        .attr( "href", editUrl )
                        .text( "(edit)" )
                } catch (e) { console.error(e); }
            } )
    }

    // Useful for troubleshooting
    //window.addEventListener( "blur", function (e) { e.stopPropagation(); e.stopImmediatePropagation(); return false; }, true );

    // Wire up the handler for searching. Note that this is HACKY and depends on
    // implementation details. As of October 2019, the mediawiki.searchSuggest
    // module depends on the jquery.suggestions module. The former uses
    // mediawiki.track to fire a bunch of searchSuggest tracking events, and the
    // final one called when everything's updated is impression-results. That's
    // the one we look for here. Ideally we would use the actual
    // jquery.suggestions hooks, but I couldn't figure out how to do that.
    mw.loader.using( "mediawiki.util", function () {
        mw.trackSubscribe( "mediawiki.searchSuggest", function ( topic, value ) {
            if( value.action === "impression-results" ) {
                insertSearchLinks();
            }
        } );
    } );

    mw.loader.addStyleTag(
        ".suggestion-link { z-index: 1; float: right; position:relative; left: 0.2em; }" +
        ".suggestions-result-current a { color: white; }"
    );
} );
// </nowiki>