Jump to content

User:Mesidast/Fix ITN Talk.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.
// Script by [[User:Mesidast]] to fix ITN Talk templates
// Version 2.2

if (ITNautoSummary == null) var ITNautoSummary = true; //generate a short summary
if (ITNshowDiff == null) var ITNshowDiff = true; //show diff after pressing button
if (ITNmarkMinor == null) var ITNmarkMinor = true; //mark as minor

function ITN_Fix() {
	var textbox = $("#wpTextbox1");
	var summary = $("#wpSummary");
	if ( !textbox ) {
		return false;
	}
	
	var txt = textbox.val();
	var original = txt;
	txt = txt.replace(/\{\{ITN talk *\| *([0-9]{1,2}) *\| *([a-zA-Z]+) *\| *([0-9]{4}) *\| *oldid\d*=([0-9]+) *[^}]*\}\}/g, "{{ITN talk|date=$1 $2 $3|oldid=$4}}");
	txt = txt.replace(/(\{\{ITN talk[\S ]*\}\})\n{1}(\{\{[T|t]alk header[\S ]*\}\})/g, "$2\n$1");
	
	// Only insert the edit summary when something has changed
	if (txt !== original) {
		textbox.val(txt);
		if (ITNautoSummary) summary.val(ITN_EditSummary(summary.val()));
		if (ITNmarkMinor) document.editform.wpMinoredit.checked = true;
		if (ITNshowDiff) $("#wpDiff").click();
	}
}

function ITN_EditSummary(sum) {
	var ITNsum = "Fix [[Template:ITN talk|ITN]] parameters";
	if (sum.indexOf(ITNsum) == -1) {
		if (sum.match(/[^\*\/\s][^\/\s]?\s*$/)) {
				sum += " | ";
		}
		sum += ITNsum;
	}
	return sum;
}

$(function () {
	if(document.forms.editform) {
		mw.util.addPortletLink('p-tb', 'javascript:ITN_Fix()', 'Fix ITN Talk', 'ca-formatcitations', 'Fix old ITN talk date parameters');
	}
});