Jump to content

User:Zackmann08/scripts/AWBRenameHelper.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.
//<nowiki>
jQuery(document).ready(function($) {
	if(mw.config.get('wgPageName') == 'Wikipedia:AutoWikiBrowser/Rename_template_parameters' && document.getElementsByName('wpTextbox1')[0]) {
		mw.loader.using(['mediawiki.util']).done( function() {
			var portletlink = mw.util.addPortletLink('p-tb','#','@GenParams','t-gentempparams');
			$(portletlink).click(function(e) {
				e.preventDefault();
				generateParams();
			});
		});
	}
	
	function generateParams()
	{
		var mycontent = document.getElementById('wpTextbox1');
		var original_text = mycontent.value;
		
		const replace_regex = /\|\s([\w\-\s]+)\s=\s(\w+).*\n?/g;
		const replaces = original_text.matchAll(replace_regex);

		let template = prompt('What is the template?');
		var final_text = original_text.replaceAll(replace_regex, "");

		final_text += '===' + template + '===\n'
		
		for (const replace of replaces) {
			final_text = final_text + '* <code>{{AWB rename template parameter|' + template + '|' + replace[1] + '|' + replace[2] + '}}</code>\n';
		}
		
		mycontent.value = final_text;
		mw.notify('Success');
	}
});
	
//</nowiki>