User:Mr. Stradivarius/gadgets/SearchEditLink.js

From Wikipedia, the free encyclopedia
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.
/*                            SearchEditLink
 * 
 * This gadget adds an [edit] link next to each search result when using the
 * internal wiki search engine.
 * 
 * To install it, add the following to your [[Special:MyPage/common.js]]:

importScript('User:Mr. Stradivarius/gadgets/SearchEditLink.js') // Linkback: [[User:Mr. Stradivarius/gadgets/SearchEditLink.js]]

 */

mw.loader.using( [ 'mediawiki.Uri' ], function () {
	if ( mw.config.get( 'wgCanonicalSpecialPageName' ) !== 'Search' ) {
		return;
	}
	$( '.mw-search-result-heading' )
		.append( function () {
			var url = new mw.Uri( this.children[0].href );
			url.extend( {
				title: mw.Uri.decode( url.path.replace( /^\/wiki\//, '' ) ),
				action: 'edit'
			} );
			url.path = '/w/index.php';
			return $( '<span>' )
				.addClass( 'mw-editsection' )
				.append( $( '<span>' )
					.addClass( 'mw-editsection-bracket' )
					.text( '[' )
				)
				.append( $( '<a>' )
					.attr( 'href', url.toString() )
					.text( 'edit' )
				)
				.append( $( '<span>' )
					.addClass( 'mw-editsection-bracket' )
					.text( ']' )
				);
		} );
} );