Jump to content

User:Random832/simple.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Random832 (talk | contribs) at 15:21, 4 February 2008 (fix for opera, fix spurious transclusions too while we're at it). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
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.
// Script for automating deletion reason dropdown
window.disableSysopJS = true; // conflicts with current script in Mediawiki:sysop.js
// -----------------------------------------------------------------
addOnloadHook(function delete_dropdown() {
  var list = document.getElementById('wpDeleteReasonList');
  if(!list) return;
  var summaryBox = document.getElementById('wpReason') || document.getElementById('wpComment');
  if(!summaryBox) return;
  var re = /\[\[([^\]\|]*\|)?([^\]]*)\]\]/g; // convert pipe links to their display text.
  for(var i=0;i<list.options.length;i++) {
     var o=list.options[i];
     o.text = o.text.replace(re,'$2');
  }
  function csdChangeBox() {
   if(summaryBox.value.indexOf('content was:') != -1) summaryBox.value = '';
  }
  function addEvent(obj, evType, fn, useCapture){
       //alert(obj);
       if (obj.addEventListener) {
         obj.addEventListener(evType, fn, useCapture);
         return true;
       } else if (obj.attachEvent) {
         var r = obj.attachEvent("on"+evType, fn);
         return r;
       } else {
         alert("Handler could not be attached");
       }
  }
  function findCSD(s) { // s e.g. "U1", "G10"
    for(var i=0;i<list.options.length;i++) {
       var o=list.options[i];
       if (o.text.indexOf("CSD "+s+":") == 0) return o;
       if (o.text.indexOf(s+":") == 0) return o;
       //TODO fine-tuning for A7 and G6
    }
  }
  var reasons_regex = {
     'G10': // copied from [[User:Ilmari Karonen]]'s script, covers more bases than the others since it's more important (this script should blank the summary in _all_ cases where the other script does
/\{\{\s*(msg:\s*)?(template:\s*)?(db-a(p|tk)?|((csd|db)[-:])?(a6|attack(pages?|template)?|g10))\s*[|}]/i,
// other regexes are simpler and cover probably 99% of actual tags.
     'G1': /\{\{\s*(db-|csd[-:])?(nonsense|ns|g1)\s*\}\}/i,
     'G2': /\{\{\s*(db-|csd[-:])?(test|g2)\s*\}\}/i,
     'G3': /\{\{\s*(db-|csd[-:])?(vandalism|vandal|vand|g3)\s*\}\}/i,
     'G4': /\{\{\s*((db-|csd[-:])?(repost|g4)|deleteagain)\s*\}\}/i,
     'G5': /\{\{\s*(db-|csd[-:])?(banned|ban|g5)\s*\}\}/i,
     'G6': /\{\{\s*(db-|csd[-:])?(maintenance|move|histmerge|g6)\s*\}\}/i, // db-move and db-histmerge should be covered elsewhere if possible
     'G7': /\{\{\s*(db-|csd[-:])?(author|blanked|g7)\s*\}\}/i,
     'G8': /\{\{\s*(db-|csd[-:])?(talk|g8)\s*\}\}/i,
     // G9 is for office, there is no tag.
     // G10 is covered above
     'G11': /\{\{\s*(db-|csd[-:])?(spam|ad|advert|g11)\s*\}\}/i,
     'G12': /\{\{\s*(db-|csd[-:])?(copyvio|cv|cvio|g12|a8)\s*(\}\}|\|)/i,
     'A1': /\{\{\s*(db-|csd[-:])?((no)?context|a1)\s*\}\}/i,
     'A3': /\{\{\s*(db-|csd[-:])?((no)?content|a3|contact|a4)\s*\}\}/i,
     // A4 = A3
     // A6 = G10
     'A7 (bio)': /\{\{\s*(nn-|db-|csd[-:])?(a7|notability|bio)\s*\}\}/i,
     'A7 (web)': /\{\{\s*(nn-|db-|csd[-:])?(web(site)?|inter(net|tubes))\s*\}\}/i,
     'A7 (group)': /\{\{\s*(db-|csd[-:]|nn-)?(band|club|music|inc|corp|comp(any)?)\s*\}\}/i,
              // TODO separate for music/band?
      // A8 = G12
     'C1': /(\{\{\s*(db-|csd[-:])?(emptycat|c1)\s*\}\}|empty category)/i,
     'U1': /\{\{\s*(db-|csd[-:])?(userreq|u1|user)\s*\}\}/i,
     'I2': /\{\{\s*(db-|csd[-:])?(noimage|i2)\s*\}\}/i,
     // no point in including the other image categories here, this will only work
     // when deleting a description page with no image
     'dummy': /this will never be reached, it is there to allow every entry above to end in a comma/
  }
  function autopick_deletion_reason() {
   for(var csd in reasons_regex) {
     if(reasons_regex[csd].test(summaryBox.value)) {
        var option = findCSD(csd);
        if(!option) {
            if(csd == 'G10')
            summaryBox.value = "[[WP:CSD#G10|CSD G10]]: [[Wikipedia:Attack page|Attack Page]]"; // last resort, if G10 can't be found in the menu.
            return;
        }
        list.selectedIndex = option.index;
        csdChangeBox(); // clear summary box
     }
   }
   // TODO "content before blanking" and A7, etc.
   // TODO We can autodetect G8 from the namespace and a redlinked tab.
   var xfdregex = /.*{{AfDM\|page=([^\|]*)\|.*/;
   if(xfdregex.test(summaryBox.value))  { 
      summaryBox.value = summaryBox.value.replace(xfdregex,'[[Wikipedia:Articles for deletion/$1]]');
      return;
   }
   xfdregex = /.*{{mfdtag\|([^}]*)}}.*/;
   if(xfdregex.test(summaryBox.value))  { 
      summaryBox.value = summaryBox.value.replace(xfdregex,'[[Wikipedia:Miscellany for deletion/$1]]');
      return;
   }
   xfdregex = /.*{{dated prod\|concern = ({{{concern\|)?(.*?)(}}})?\|month.*/;
   if(xfdregex.test(summaryBox.value))  { 
      summaryBox.value = summaryBox.value.replace(xfdregex,'[[WP:PROD]], reason was \'$2\'.');
      return;
   }
   if(summaryBox.value.indexOf('{{rfd}}') != -1) {
      // best we can do without a direct link
      summaryBox.value = '[[WP:RFD|RFD]]';
      return;
   }
   xfdregex = /.*{{((db|delete).*?)}}.*/;
   if(xfdregex.test(summaryBox.value))  { // last resort for unhandled speedy tags
      summaryBox.value = summaryBox.value.replace(xfdregex,'$1');
      return;
   }
  }
  addEvent(list, "change", csdChangeBox, false);
  autopick_deletion_reason();
});
// -----------------------------------------------------------------