User:Ricordisamoa/DiffAutoLinker.js

From Meta, a Wikimedia project coordination wiki

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
/* <nowiki>
 *
 * DiffAutoLinker.js
 * @author [[User:Ricordisamoa]]
*/
$( document ).ready( function () {
	'use strict';
	mw.loader.using( 'mediawiki.Uri' )
	.done( function () {
		var replaceUrls = function ( text ) {
			var spl = text.split( ' ' ),
				length = spl.length;
			for ( var i = 0; i < length; i++ ) {
				var uri;
				try {
					uri = new mw.Uri( spl[i] );
				} catch ( e ) {
					continue;
				}
				if (
					uri.host === mw.config.get( 'wgServerName' ) &&
					uri.query.diff !== undefined &&
					uri.query.diff !== ''
				) {
					spl[i] = '[[Special:Diff/' +
						( uri.query.oldid ? uri.query.oldid + '/' : '' ) +
						uri.query.diff + ']]';
				}
			}
			return spl.join( ' ' );
		};
		$( '#wpTextbox1' ).on( 'paste', function ( event ) {
			if ( event.originalEvent ) {
				event = event.originalEvent;
			}
			if ( event ) {
				var text = event.clipboardData.getData( 'text/plain' );
				if ( text ) {
					var newText = replaceUrls( text );
					if ( newText !== undefined && newText !== text ) {
						window.document.execCommand( 'insertText', false, newText );
						event.preventDefault();
					}
				}
			}
		} );
	} );
} );