User:B. Wolterding/onthisday.js

// "OnThisDay" template" automation, test

addOnloadHook(addOTDLink);

function addOTDLink{ if (/[?&]action=edit/.test(window.location.href) && (/[?&]title=Talk:/.test(window.location.href)) ) { addPortletLink ('p-tb', 'javascript:addOTD', 'Add "On this day"'); } }

function addOTD {

var editsummary = "adding/updating template {"+"{OnThisDay}}";

var textbox = document.editform.wpTextbox1;

var otdRegex = /{({)[oO]nThisDay.*?}}/;

var now = new Date; var monthname = new Array("January", "February", "March", "April", "May", "June",                     "July", "August", "September", "October", "November", "December");

var otdTemplate = '{'+'{OnThisDay|'+now.getDate+' '+monthname[now.getMonth]+'|'+now.getFullYear+'}}'; var newvalue = textbox.value;

if (newvalue.match(otdRegex)) { newvalue=newvalue.replace(otdRegex,otdTemplate); }        else { newvalue = otdTemplate+'\n'+newvalue; }

textbox.value=newvalue;

var edits = document.editform.wpSummary;

edits.value = editsummary;

document.editform.wpMinoredit.checked = true;

document.editform.wpPreview.click;

}