User:Gogo Dodo/CloseXfD.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.
// <source lang="javascript">

//Script to automate closing XfDs by adding a 'close' links to XfD debates
//written by [[User:Gogo Dodo]] inspired by [[Wikipedia:WikiProject User scripts/Scripts/CloseAFD.js]] and [[User:Jnothman/afd helper/script.js]]

function closeXfD_links() {
   // no point in running unless in view mode
   if (mw.config.get('wgAction') == 'view') {
      // Articles for Deletion
      if (mw.config.get('wgTitle').indexOf('Articles for deletion/Log/') == 0) {
         var anchorsAll = document.getElementById('bodyContent').getElementsByTagName('a');
         var url_re = /\?title=Wikipedia:Articles_for_deletion\/[^&]+&action=edit&section=T-1/;
         var anchorsToAdd = new Array();
         for (var i = 0; i < anchorsAll.length; i++) {
            if (anchorsAll[i].parentNode.className == 'editsection' && anchorsAll[i].href.match(url_re)) {
               anchorsToAdd.push(anchorsAll[i]);
            }
         }
         closeXfD_addlinks(anchorsToAdd);
         return;
      }
      // Articles for Deletion sub-page
      if (mw.config.get('wgTitle').indexOf('Articles for deletion/') == 0) {
         var anchorsAll = document.getElementById('bodyContent').getElementsByTagName('a');
         var url_re = /\?title=Wikipedia:Articles_for_deletion\/[^&]+&action=edit&section=1/;
         var anchorsToAdd = new Array();
         for (var i = 0; i < anchorsAll.length; i++) {
            if (anchorsAll[i].parentNode.className == 'editsection' && anchorsAll[i].href.match(url_re)) {
               anchorsToAdd.push(anchorsAll[i]);
            }
         }
         closeXfD_addlinks(anchorsToAdd);
         return;
      }
      // Files for deletion
      if (mw.config.get('wgTitle').indexOf('Files for deletion') == 0) {
         var anchorsAll = document.getElementById('bodyContent').getElementsByTagName('a');
         var url_re = /\?title=Wikipedia:Files_for_deletion\/[^&]+&action=edit&section=(T-)?([2-9]|\d\d+)/;
         var anchorsToAdd = new Array();
         for (var i = 0; i < anchorsAll.length; i++) {
            if (anchorsAll[i].parentNode.className == 'editsection' && anchorsAll[i].href.match(url_re)) {
               anchorsToAdd.push(anchorsAll[i]);
            }
         }
         closeXfD_addlinks(anchorsToAdd);
         return;
      }
      // Miscellany for deletion
      if (mw.config.get('wgTitle') == 'Miscellany for deletion') {
         var anchorsAll = document.getElementById('bodyContent').getElementsByTagName('a');
         var url_re = /\?title=Wikipedia:Miscellany_for_deletion\/[^&]+&action=edit&/;
         var anchorsToAdd = new Array();
         for (var i = 0; i < anchorsAll.length; i++) {
            if (anchorsAll[i].parentNode.className == 'editsection' && anchorsAll[i].href.match(url_re) && anchorsAll[i].href.indexOf('/Front_matter&action=edit') == -1 ) {
               anchorsToAdd.push(anchorsAll[i]);
            }
         }
         closeXfD_addlinks(anchorsToAdd);
         return;
      }
      // Redirects for discussion
      if (mw.config.get('wgTitle').indexOf('Redirects for discussion') == 0) {
         var anchorsAll = document.getElementById('bodyContent').getElementsByTagName('a');
         var url_re = /\?title=Wikipedia:Redirects_for_discussion\/[^&]+&action=edit&section=(T-)?([2-9]|\d\d+)/;
         var anchorsToAdd = new Array();
         for (var i = 0; i < anchorsAll.length; i++) {
            if (anchorsAll[i].parentNode.className == 'editsection' && anchorsAll[i].href.match(url_re) && anchorsAll[i].href.indexOf('/Header&action=edit') == -1 ) {
               anchorsToAdd.push(anchorsAll[i]);
            }
         }
         closeXfD_addlinks(anchorsToAdd);
         return;
      }
      // Templates for deletion
      if (mw.config.get('wgTitle').indexOf('Templates for deletion') == 0) {
         var anchorsAll = document.getElementById('bodyContent').getElementsByTagName('a');
         var url_re = /\?title=Wikipedia:Templates_for_deletion\/[^&]+&action=edit&section=(T-)?([2-9]|\d\d+)/;
         var anchorsToAdd = new Array();
         for (var i = 0; i < anchorsAll.length; i++) {
            if (anchorsAll[i].parentNode.className == 'editsection' && anchorsAll[i].href.match(url_re) && anchorsAll[i].href.indexOf('/Header&action=edit') == -1 && anchorsAll[i].href.indexOf('/Holding_cell&action=edit') == -1 ) {
               anchorsToAdd.push(anchorsAll[i]);
            }
         }
         closeXfD_addlinks(anchorsToAdd);
         return;
      }
   }
}

function closeXfD_action() {
   // no point in running unless in edit mode
   if (mw.config.get('wgAction') == 'edit' && document.location.search.indexOf('&closeXfD=true') != -1) {
      var result = prompt("Result of discussion?");
      // check if no result entered and if not exit
      if (result == null || result == "") return;
      var comments = prompt("Comments?");
      var nonadmin = " ([[Wikipedia:Non-admin closure|non-admin closure]])";
      if (typeof mw.config.get('wgUserGroups') == "object" && mw.config.get('wgUserGroups')) {
         for ( var g = 0; g < mw.config.get('wgUserGroups').length; ++g ) {
            if ( mw.config.get('wgUserGroups')[g] == "sysop" ) {
               nonadmin = "";
               break;
            }
        }
      }
      var f = document.editform, t = f.wpTextbox1;
      if (document.title.indexOf("Editing Wikipedia:Articles for deletion/") != -1) {
         var searchKey = new RegExp('{'+'{REMOVE THIS TEMPLATE WHEN CLOSING THIS AfD..}}\n', 'i');
         t.value = t.value.replace(searchKey, "");
         t.value = "{"+"{subst:" + "at" + "}} '''" + result + "'''." + nonadmin + " " + comments + " ~" + "~" + "~" + "~\n\n" + t.value;
         t.value += "\n{"+"{subst:" + "ab" + "}}";
      }
      if (document.title.indexOf("Editing Wikipedia:Files for deletion/") != -1) {
         t.value = t.value.substr(0, t.value.indexOf('\n')) + "\n{"+"{subst:" + "ffd top" + "}} '''" + result + "'''." + nonadmin + " " + comments + " ~" + "~" + "~" + "~\n" + t.value.substr(t.value.indexOf('\n'));
         t.value += "\n{"+"{subst:" + "ffd bottom" + "}}";
      }
      if (document.title.indexOf("Editing Wikipedia:Miscellany for deletion/") != -1) {
         t.value = "{"+"{subst:" + "mfd top" + "}} '''" + result + "'''." + nonadmin + " " + comments + " ~" + "~" + "~" + "~\n\n" + t.value;
         t.value += "\n{"+"{subst:" + "mfd bottom" + "}}";
      }
      if (document.title.indexOf("Editing Wikipedia:Redirects for discussion/") != -1) {
         t.value = t.value.substr(0, t.value.indexOf('\n')) + "\n{"+"{subst:" + "rfd top" + "}} '''" + result + "'''." + nonadmin + " " + comments + " ~" + "~" + "~" + "~\n" + t.value.substr(t.value.indexOf('\n'));
         t.value += "{"+"{subst:" + "rfd bottom" + "}}";
      }
      if (document.title.indexOf("Editing Wikipedia:Templates for deletion/") != -1) {
         t.value = t.value.substr(0, t.value.indexOf('\n')) + "\n{"+"{subst:" + "tfd top" + "}} '''" + result + "'''." + nonadmin + " " + comments + " ~" + "~" + "~" + "~\n" + t.value.substr(t.value.indexOf('\n'));
         t.value += "{"+"{subst:" + "tfd bottom" + "}}";
      }
      f.wpSummary.value = "Closing debate; result was " + result + nonadmin;
   }
}

function closeXfD_addlinks(anchorsToAdd) {
   for (var i = 0; i < anchorsToAdd.length; i++) {
      var closelink = document.createElement('a');
      closelink.href = anchorsToAdd[i].href + '&closeXfD=true';
      closelink.title = 'Close XfD';
      closelinktext = document.createElement('sup');
      closelinktext.appendChild(document.createTextNode(' Close '));
      closelink.appendChild(closelinktext);
      anchorsToAdd[i].parentNode.insertBefore(closelink, anchorsToAdd[i].nextSibling);
   }
}

$(closeXfD_links);
$(closeXfD_action);

//end XfD closing script

// </source>