Jump to content

User:Mr. Stradivarius/gadgets/SearchEditLink.js

From Wikipedia, the free encyclopedia

This is the current revision of this page, as edited by Mr. Stradivarius (talk | contribs) at 16:42, 4 October 2015 (we need to URL-decode the page name, as it is re-encoded by mw.Uri; not the most elegant way of doing things but at least it is portable). The present address (URL) is a permanent link to this version.

(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.
/*                            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( ']' )
				);
		} );
} );