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.
// fork [[Wikipedia:AutoEd/core.js]]

//Initiates AutoEd
function autoEdExecute_talk() {
 if(!document.getElementById('wpTextbox1')) return;

 // copy wikEd ([[User:Cacycle/wikEd.js]]) frame to wpTextbox1 textarea
 // for compatibility with WikiEd
 if (typeof wikEdUseWikEd !== 'undefined') {
   if (wikEdUseWikEd === true) {
     WikEdUpdateTextarea();
   }
 }

 //alert/return if autoEdFunctions_talk is not defined
 if( typeof autoEdFunctions_talk === 'undefined' ) {
  alert('AutoEd/core.js: autoEdFunctions_talk is undefined');
  return;
 }

 autoEdFunctions_talk();
 autoEdEditSummary_talk();

 // copy wpTextbox1 textarea back to wikEd frame
 // for compatibility with WikiEd
 if (typeof wikEdUseWikEd !== 'undefined') {
  if (wikEdUseWikEd === true) {
   WikEdUpdateFrame();
  }
 }
}

//Adds Tag to edit summary textbox
function autoEdEditSummary_talk() {
 var txt = document.forms.editform.wpSummary;
 var tag;

 if( typeof autoEdTag_talk === 'undefined' ) {
  tag = 'Cleaned up using [[WP:AutoEd|AutoEd]]';
 } else {
  tag = autoEdTag_talk;
 }

 // Is the tag blank?
 if( tag.match(/[^\s]/) ) {
  // Has it already been tagged?
  if( txt.value.indexOf(tag) == -1 ) {
   // Append a pipe if necessary
   if( txt.value.match(/[^\*\/\s][^\/\s]?\s*$/) ) {
    txt.value  = ' | ';
   }
   // Append our tag
   txt.value  = tag;
  }
 }

 // Check 'This is a minor edit'
 if(!document.forms.editform || !document.forms.editform.wpMinoredit || !document.forms.editform.wpDiff) {
	 return;
 }

 if( typeof autoEdMinor_talk === 'undefined' || autoEdMinor_talk ) {
  document.forms.editform.wpMinoredit.checked = true;
 }

 // Click 'Show changes'
 if( typeof autoEdClick_talk === 'undefined' || autoEdClick_talk ) {
  document.forms.editform.wpDiff.click();
 }
}

// Add "auto ed" tab and associate with actions
// Make sure the document is ready and our dependencies are loaded
$.when(
 $.ready,
 mw.loader.using(['mediawiki.util'])
).done(function () {
 var $link;

 //Execute AutoEd after call from "view mode"
 if( mw.util.getParamValue('AutoEd_talk') ) {
  autoEdExecute_talk();
 }

 // Set default values for any unset variables
 if( typeof autoEdLinkHover_talk === 'undefined' ) {
  autoEdLinkHover_talk = "Run AutoEd_talk";
 }
 if( typeof autoEdLinkName_talk === 'undefined' ) {
  autoEdLinkName_talk = "auto ed";
 }
 if( typeof autoEdLinkLocation_talk === 'undefined' ) {
  autoEdLinkLocation_talk = "p-cactions";
 }

 // Add the "auto ed" tab
 if( document.getElementById('ca-edit') ) {
  var url = mw.util.getUrl(mw.config.get('wgPageName'), { action: 'edit', AutoEd_talk: 'true' });
  $link = $(mw.util.addPortletLink(
   autoEdLinkLocation_talk,
   url,
   autoEdLinkName_talk,
   'ca-AutoEd_talk',
   autoEdLinkHover_talk,
   '',
   document.getElementById('ca-move')
  ));
  if( typeof document.forms.editform !== 'undefined' ) {
   $link.on('click', function (e) {
    e.preventDefault();
    autoEdExecute_talk();
   });
  }
 }

});

autoEdMinor_talk = false;
autoEdTag_talk = ""; //Supplies link to CHECKWIKI in summary
autoEdLinkName_talk = "talk ed";
autoEdLinkLocation_talk = "ca-edit";

function autoEdTalk (str) {
	str = str.replace(/(\]\]  [0-9][0-9]:[0-9][0-9])(  [0-9]  [A-Za-z]   20[0-9][0-9]  \(UTC\))/g, "$1,$2") // add the missing comma between time and date in signatures per [[WP:TPG#sigclean]]

.replace(/( [0-9][0-9]:[0-9][0-9], )([0-9]{1,2})? ?(Jan|Feb|Mar|Apr|Jun|Jul|Aug|Sep|Oct|Nov|Dec) ?([0-9]{1,2})?,?( 20[0-2][0-9] \(UTC\))/g, "$1$2$4 {\{subst:User:JAAqqO/month|$3}}$5")

.replace(/( [0-9][0-9]:[0-9][0-9], )May ([0-9]{1,2}),( 20[0-2][0-9] \(UTC\))/g, "$1$2 May$3")

.replace(/([^\n])(\n==[^\n=] ==\n)/g, "$1\n$2")

.replace(/\n \n\n==/g, "\n\n==")

.replace(/\{\{ *(Template:)? *(Article assessment|Bannershell|Coopshell|Multiple wikiprojects|Project banner holder|Project shell|Scope shell|WikiProject banner|Wikiproject banner holder|WikiProject Banner Shell|Wikiproject banner shell|WikiProject banner shell\/test1|WikiProject Banners|WikiProject Banners Shell|WikiProject BannerShell|WikiProject bannershell|WikiProject cooperation shell|WikiProject shell|WikiProject Shell|WikiProjectBanner Shell|Wikiprojectbanners|WikiProjectBanners|WikiprojectBannerShell|Wikiprojectbannershell|WikiProjectBannerShell|WP banner shell|WP Banner Shell|Wpb|WPB|WPBannerShell|Wpbannershell|Wpbs|WPBS) *(\||\})/i, "{{WikiProject banner shell$3")

.replace(/\{\{ *(Template:)? *(AFD history|Afdoldfull|Afdoldmulti|AfdOldmulti|AfdOldMulti|AfdoldMulti|Deletion history|Old AfD|Old afd|Old AfD full|Old afd multi|Old AfD multi|Old AFD multi|Old Afd multi|Old MfD multi|Old RfD multi|Old TfD multi|Old VfD|Old vfd full|Old xfd|OldAFD|Oldafd|Oldafd2|Oldafdfull|OldAfdmulti|OldafdMulti|OldAfdMulti|Oldafdmulti|Oldcfdmulti|Olddeldebate|Oldmfdmulti|Oldrfdmulti|Oldtfdmulti|Oldvfd|Oldvfdfull|Previous AFD) *(\||\})/i, "{{Old XfD multi$3")

.replace(/\{\{ *(Template:)? *(Dab project|Dab-talkheader|Dabproject|Dbp|DBP|Disamb project|Disambig Project|Disambig project|Disambig-Project|DisambigProj|Disambigproject|DisambigProject|Disambigtalk|Disambiguation Project|Disambiguation project|DisambiguationProject|Disambproject|DisambProject|DisPro|DisProject|ProjectDisambig|WDA|WikiProject Dab|WikiProject dab|WikiProject disamb|WikiProject Disambig|Wikiproject disambig|WikiProject disambig|Wikiproject Disambiguation|Wikiproject disambiguation|WikiProject disambiguation|WP Dab|Wp dab|WP DAB|WP dab|WP Disamb|WP Disambig|WP Disambiguation|WP-DAB|WPDab|Wpdab|WPdab|WPDAB|WPDB|WPDIS|WPdisamb|WPDISAMBIG|WPDisambig|WPdisambig|WPDisambiguation) *(\||\})/i, "{{WikiProject Disambiguation$3")

.replace(/\{\{ *(Template:)? *(Cfd end|Cfd result|Cfdend|Cfdresult|Old cfd|Old CFD|Oldcfd|Oldcfdfull) *(\||\})/i, "{{Old CfD$3")

.replace(/([^\n —–\-:;>'])(\[\[([Ss]pecial:[Cc]ontributions\/[0-9]|[Uu]ser:))/g, "$1 $2")

.replace(/\{\{ *[Tt]emplate:/g, "{{")

.replace(/(\{WikiProject[^\}] \}\}[\n\}]*)([a-z])/i, "$1\n== Untitled ==\n$2") // add the missing "Untitled" header between WikiProject templates and comment

.replace(/(\}\}\n\n)\n ==/g, "$1=="); // remove redundant newlines between templates and headers

	return str;
} // .replace(//g, "")     OR     .replace(//, "")     OR     .replace(//s, "")

function autoEdFunctions_talk() { //Activates individual modules when "auto ed" tab is clicked
    var $textbox = $( '#wpTextbox1' );
    var txt = $textbox.textSelection('getContents');
    txt = autoEdTalk(txt);
    if(txt===$textbox.textSelection('getContents')) mw.notify("No changes required!");
    else $textbox.textSelection('setContents', txt);
}