Jump to content

User:Est. 2021/Script/AutoCleanup/Functions.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.
/*
Subpage of [[User:Est. 2021/Script/AutoCleanup.js]]
*/

// <syntaxhighlight lang="js">

$(function() {
	
	function clean_wikitext(text) {
		const replacements = [

// Wikilinks

	[/(?<!\[)\[https?:\/\/en\.wikipedia\.org\/wiki\/([^ \]]*)(?: ([^\]]*))?\]/gs, function(match, p1, p2) {
		p1 = decodeURIComponent(p1);
		p1 = p1.replace(/_/g, ' ');
		if (p2 == undefined) {
		return `[[${p1}]]`;
		} else {
		return `[[${p1}|${p2}]]`;
		} }], // format wikilinks

	[/\[\[([^\]]*)\]\]/gs, function(match, p1) {
		p1 = decodeURIComponent(p1);
		p1 = p1.replace(/_/g, ' ');
		return `[[${p1}]]`;
		}], // remove "_" from wikilinks #OWN

// Images (cleanup)

	[/((Image:|File:)([^\.\n])*(\.\w*))/g,  function(match, p1) {
		p1 = decodeURIComponent(p1);
		p1 = p1.replace(/_/g, ' ');
		return `${p1}`;
		}], // remove "_" from file names #OWN

	[/(\| *(?:image\d*|image_\w*|cover|flag|insignia|logo|photo|seal|shield) *= *)(?:\[\[)(?:(?:Image|File):)?([^\.\n]*\.\w*)(?:\]\])/g,  function(match, p1, p2) {
		p2 = decodeURIComponent(p2);
		p2 = p2.replace(/_/g, ' ');
		return `${p1}${p2}`;
		}], // if "|image=[[File:name]]" is used, remove unneeded "[[File:]]" #OWN

	[/(\| *(?:image\d*|image_\w*|cover|flag|insignia|logo|photo|seal|shield) *= *)(?:(?:Image|File):)?([^\.\n]*\.\w*)/g,  function(match, p1, p2) {
		p2 = decodeURIComponent(p2);
		p2 = p2.replace(/_/g, ' ');
		return `${p1}${p2}`;
		}], // if "|image=File:name" is used, remove unneeded "File:" #OWN

// Lowercase #OWN

	[/\{\{(ill|lang|(note|ref)list|infobox former country)/gs, function(match, p1) {
		p1 = decodeURIComponent(p1);
		p1 = p1.replace(/_/g, ' ');
		return `{{${p1.toLowerCase()}`;
		}],

     ];
		for (const replacement of replacements) {
			text = text.replace(...replacement);
		}
		return text;
	}
	
	function process_summary(summary) {
		const ac_summary = 'minor fixes';
		if (summary) {
			if (/^\/\*.*?\*\/ $/.test(summary)) { // auto summary
				return summary + ac_summary;
			} else {
				return summary + '; ' + ac_summary;
			}
		} else {
			return ac_summary;
		}
	}
	
	function main() {
		let text_box = $('#wpTextbox1');
		let old_text = text_box.textSelection('getContents');
		let new_text = clean_wikitext(old_text);
		if (old_text != new_text) {
			text_box.textSelection('setContents', new_text);
			if (mw.util.getParamValue('section') != 'new') { // not section title instead of summary
			let summary_box = $('#wpSummary');
			summary_box.val(
				process_summary(summary_box.val())
			)}
			mw.notify(
				'Done',
				{
					tag: 'tol-autocleanup-f',
					title: 'AC Functions',
					type: 'success'
				}
			);
		}
	}
	
	function add_button() {
		try {
			if ($('editform').length) {
				mw.loader.using(['oojs-ui', 'oojs-ui-widgets'], function() {
					let button = new OO.ui.ButtonWidget({label: 'AC Functions'});
					button.on('click', main);
					$('.editButtons').append(button.$element);
				});
			}
		} catch(error) {
			alert(error);
		}
	}
	
	//$(add_button);
            
    if (mw.config.get('wgPageContentModel') === 'wikitext' && mw.config.get('wgAction') === 'edit' && mw.util.getParamValue( "AC/Fdisable" ) != "1" ) {
	    mw.util.addPortletLink( "p-cactions", window.location.href + '&AC/Fdisable=1', "Disable AC Functions", "pt-disableAC/F" );
		$(main);
	}
});

// </syntaxhighlight>