Jump to content

User:Aaron Liu/AutowIll.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.
/**
 * This script is an indepedently-usable AutoEd module. If you already have AutoEd installed, please see [[Wikipedia:AutoEd/Customization]] for how to load this module. If you don't have AutoEd installed, then you don't need to do anything; just install this script the normal way.
 * Known false positive occurs when interlanguage link is placed after a template invocation within a citation template. Make sure this does not happen by checking the page preview for errors.
 */
function autoEdwIll( str ) {
	str = str.replace( /\[https?:\/\/([^\.]*)\.(?:m\.)?wikipedia\.org\/wiki\/(\S*)(?:\s*([^\]]*)])/g, ( match, p1, p2, p3 ) => {
		if ( p3 === undefined ) {
			return `[[:${ p1 }:${ p2 }]]`;
		}
		return `[[:${ p1 }:${ p2 }|${ p3 }]]`;
	} );
	str = str.replace( /(?<!{{\s*cit[^}]*)\[\[:(.*?):([^|\]]*)(?:\|([^\]]*))?]]/g, ( match, p1, p2, p3 ) => {
		if ( p3 === undefined ) {
			return `{{ill|${ p2 }|${ p1 }}}`;
		}
		return `{{ill|${ p3 }|${ p1 }|${ p2 }}}`;
	} );
	return str;
}

$.when( $.ready ).then( () => {
	if ( importScript( 'Wikipedia:AutoEd/core.js' ) ) { // if not otherwise using AutoEd
		console.log( 'imported' );

		window.autoEdFunctions = function () {
			const $box = $( '#wpTextbox1' );
		    const str = $box.textSelection('getContents');
			const newstr = autoEdwIll( str );
			if ( newstr !== str ) { // don't confuse or tag if no fixes
			    $box.textSelection('setContents', newstr);
			} else {
				autoEdTag = '';
				autoEdClick = false;
				mw.notify( 'No changes needed!' );
			}
		};
		autoEdMinor = false;
		autoEdLinkName = 'AutowIll';
		autoEdLinkHover = 'Automatically wikify and template straightforward foreign-language links';
		autoEdTag = 'converted foreign-language links to use {{ill}} with [[User:Aaron Liu/AutowIll.js]]';
	}
} );