User:B. Wolterding/close-discussion.js

From Wikipedia, the free encyclopedia

Note: After saving, you have to bypass your browser's cache to see the changes. In Internet Explorer and Firefox, hold down the Ctrl key and click the Refresh or Reload button. Opera users have to clear their caches through Tools→Preferences, see the instructions for Opera. Konqueror and Safari users can just click the Reload button.

// "Close discussion" automation for mergers, etc.
 
addOnloadHook(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();
  }
 
}