Jump to content

User:B. Wolterding/onthisday.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.
// "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();

}