User:Alex 21/script-formatdates.js

$(function($) {	setTimeout(function { $.when( mw.loader.using( ['mediawiki.util']), $.ready ).then(function {			var portletlink = mw.util.addPortletLink('p-tv', '#', 'Format airdates');			$(portletlink).click( function(e) { e.preventDefault; // Textbox value var wpTextbox1 = document.getElementById('wpTextbox1'); var wpTextbox1_V = wpTextbox1.value.split("\n"); // Df parameter, update parameters, months and regex used throughout var df = false; var AltDate_U = false; var OriginalAirDate_U = false; var MDY = /(\|\s*(OriginalAirDate)\s*=\s*)(([A-Z][a-z]*)\s*)?((\d{1,2}),\s*)?(\d{4})/; var DMY = /(\|\s*(OriginalAirDate)\s*=\s*)((\d{1,2})\s*)?(([A-Z][a-z]*)\s*)?(\d{4})/; var ad_reg = /(\|\s*(AltDate|FirstEngAirDate)\s*=\s*)\{\{[Ss]tart\sdate\|(\d+)\|0?(\d+)\|0?(\d+)([^\}]*)\}\}/; var months = ['','January','February','March','April','May','June','July','August','September','October','November','December']; var months_short = ['','Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec']; // Iterate through each line of the textbox for (var i = 0; i < wpTextbox1_V.length; i++) { var line = wpTextbox1_V[i]; // Check for df and to see if the line actually needs changing at all if (line.match(/\|\s*df\s*\=\s*([^\s]+)/)) df = true; if (line.toLowerCase.indexOf('start date') > -1 && line.indexOf('OriginalAirDate') > -1) continue; if (line.toLowerCase.indexOf('start date') == -1 && (line.indexOf('AltDate') > -1 || line.indexOf('FirstEngAirDate') > -1)) continue; if ((line.indexOf('OriginalAirDate') == -1 && line.indexOf('AltDate') == -1 && line.indexOf('FirstEngAirDate') == -1) || line.indexOf('TableTBA') > -1) continue; // Replace OriginalAirDate var line_old = line; if (line.match(MDY)) { line = line.replace(MDY, "$1"); } else { line = line.replace(DMY, "$1"); }					if (line_old == line && line.indexOf("","}}").replace("","");					wpTextbox1_V[i] = line;				}				// Save content 				var wpTextbox1NewValue = '';				for (i = 0; i < wpTextbox1_V.length; i++) wpTextbox1NewValue += wpTextbox1_V[i]+"\n";				wpTextbox1.value = wpTextbox1NewValue;				document.getElementById('wpSummary').value += "Update OriginalAirDate and/or AltDate to use correct formats per template documentation via script.";			}); });	},200); });