User:Erutuon/scripts/etylCleanup.js

Note – after saving, you may have to bypass your browser’s cache to see the changes.

  • Mozilla / Firefox / Safari: hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (Command-R on a Macintosh);
  • Konqueror and Chrome: click Reload or press F5;
  • Opera: clear the cache in Tools → Preferences;
  • Internet Explorer: hold Ctrl while clicking Refresh, or press Ctrl-F5.

/*	<nowiki>
	If there are {{etyl}} templates in the edit box,
	add a link to convert {{etyl}} to {{der}}.
	Beware: after using this tool, you should change some cases of {{der}}
	to {{inh}} or {{bor}}, as appropriate.
*/

var namespaceNumber = mw.config.values.wgNamespaceNumber;
var action = mw.config.values.wgAction;

if ( ( namespaceNumber === 0 || namespaceNumber === 118 ) &&
		action === "edit"		&& /\{\{etyl\|/.test($("#wpTextbox1").val()) )
{
	if ( !$("#wikitext-cleanup-button-wrapper").length )
		$("#editform").prepend('<div id="wikitext-cleanup-button-wrapper"></div>');
	
	$("#wikitext-cleanup-button-wrapper").append('<div id="etyl-cleanup" class="wikitext-cleanup-button"><a href="javascript:etylToDer()">replace {{etyl}} with {{der}}</a></div>');

	importStylesheetURI("//en.wiktionary.org/w/index.php?title=User:Erutuon/styles/wikitext-cleanup.css&action=raw&ctype=text/css");
	
	var count = 0;
	
	var etylToDer = function()
	{
		var content = $("#wpTextbox1").val();
		
		content = content.replace(
			/\{\{etyl\|[^\|\}\n]+\|([^\|\}\n]+)\}\} \{\{(?:l|m)\|([^\|\}\n]+)\|([^\}\n]+)\}\}/g,
			function(wholeMatch, entryLang, sourceLang, term)
			{
				count += 1;
				if ( entryLang === "-" )
					return `{{cog|${sourceLang}|${term}}}`;
				else
					return `{{der|${entryLang}|${sourceLang}|${term}}}`;
			}
		);
		
		if ( count > 0 )
		{
			if ( count > 1 )
				mw.notify(`${count} replacements made.`);
			else
				mw.notify(`${count} replacement made.`);
		
			$("#wpSummary").val(
				function(index, summary)
				{
					addition = "replaced [[T:etyl]] with [[T:der]], [[T:inh]], or [[T:bor]], with the help of [[User:Erutuon/scripts/etylCleanup.js|JavaScript]]";
					
					afterSectionName = summary.match(/^\/\*[^\*]+\*\/\s+(.+)/);
					
					if ( afterSectionName && afterSectionName[1].length > 1 )
						addition = "; " + addition;
					
					if ( ( !afterSectionName || !afterSectionName[1].includes(addition) ) )
						return summary + addition;
				}
			);
		}
		else
			mw.notify("No replacements made.");
		
		$("#wpTextbox1").val(content);
	};
}

// </nowiki>