User:B. Wolterding/close-discussion.js

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.
// "Close discussion" automation for mergers, etc.

$(addCloseDiscLink);

function addCloseDiscLink(){
  if (/[?&]action=edit/.test(window.location.href) && /[?&]title=Talk:/.test(window.location.href) 
      && /[?&]section=/.test(window.location.href) ) {
	addPortletLink ('p-tb', 'javascript:closeDiscussion()', 'Close discussion');
  }
}

function closeDiscussion() {

  var result = prompt("Enter the result of the discussion:","");

  if (result) {
  	var discheader = "{"+"{Discussion top}}\n"
                         + "The result was: "+result+" --~~"+"~~\n----\n";

        var discfooter = "\n{"+"{Discussion bottom}}";

  	var editsummary = "Closing discussion";

  	var textbox = document.editform.wpTextbox1;

        var prevText = textbox.value;

        var toppos = 0;
        var headerSearch =  (/==(.*)==\n/.exec(prevText));
        if (headerSearch.index >= 0) {
           toppos = headerSearch.index + headerSearch[0].length;
           while (toppos < prevText.length-1 && prevText[toppos]=="\n") {
                 toppos++;
           }
        }
        
       
  	textbox.value = prevText.substring(0,toppos) + discheader + prevText.substring(toppos) + discfooter;

  	var edits = document.editform.wpSummary;
  	edits.value = editsummary;

	document.editform.wpWatchthis.checked = false;

	document.editform.wpPreview.click();
  }

}