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.
var delReason = new function(){

//*** On article page: try to find wpReason from template

var caDelete

this.onArticlePage = function (){
 if (caDelete = document.getElementById('ca-delete'))
   caDelete.onclick = delReason.fixDelLink
}

this.fixDelLink = function (){
 var template, i, links, wpReason = ''
 if (template = document.getElementById('delete')){ //search for CSD criteria inside {Db-xxx} 
   links = template.getElementsByTagName('a')
   for (i=0; i<links.length; i++) 
     if (links[i].title == 'WP:CSD') {
        wpReason = links[i].href.split('#')[1]
        break
     } 
 }else if (template = document.getElementById('afd')){
   links = template.getElementsByTagName('a')
   for (i=0; i<links.length; i++) //search for discussion link inside {afd}
       if (links[i].title.indexOf('Wikipedia:Articles for deletion/') != -1) {
         wpReason = 'After discussion at ['+'[' + links[i].title + ']]'
         break
     } 
 }else if (template = document.getElementById('prod')){
   wpReason = '[[WP:PROD|Prod]] left uncontested for five days'
   if (i = document.getElementById('delete-reason'))
     wpReason += ' (' + i.innerHTML + ')'
 }
 if (wpReason)
   caDelete.firstChild.href += '&wpReason=' + wpReason
}



//*** On delete page

var reasons = {
'CSD Reasons':'',
"G1 - Nonsense":"[[WP:CSD#G1|CSD G1]]: [[WP:PN|Patent Nonsense]]",
"G2 - Test Pages":"[[WP:CSD#G2|CSD G2]]: Test Pages",
"G3 - Pure Vandalism":"[[WP:CSD#G3|CSD G3]]: [[WP:VAND|Pure Vandalism]]",
"G4 - Recreated Deleted":"[[WP:CSD#G4|CSD G4]]: Recreation of deleted material",
"G5 - Banned User":"[[WP:CSD#G5|CSD G5]]: Contributions by [[Wikipedia:List of banned users|Banned User]]",
"G6 - Housekeeping":"[[WP:CSD#G6|CSD G6]]: Housekeeping",
"G7 - Author Request":"[[WP:CSD#G7|CSD G7]]: Author Requests Deletion",
"G8 - Talk Page":"[[WP:CSD#G8|CSD G8]]: Talkpage of non-Existent Article",
"G9 - Office Actions":"[[WP:CSD#G9|CSD G9]]: [[WP:OFFICE|Office Actions]]",
"G10 - Attack Pages":"[[WP:CSD#G10|CSD G10]]: [[Wikipedia:Attack page|Attack Page]]",
"G11 - Advertising":"[[WP:CSD#G11|CSD G11]]: [[WP:SPAM|Blatant Advertising]]",
"G12 - Copyright":"[[WP:CSD#G12|CSD G12]]: [[WP:C|Blatant Copyright infringement]]",
"A1 - No Context":"[[WP:CSD#A1|CSD A1]]: Very short article providing little or no context",
"A2 - Foreign Language":"[[WP:CSD#A2|CSD A2]]: Foreign language article that exists on another Wikipedia",
"A3 - No Content":"[[WP:CSD#A3|CSD A3]]: No content",
"A5 - Transwikied":"[[WP:CSD#A5|CSD A5]]: Has been transwikied",
"A7 - Unremarkable Bio":"[[WP:CSD#A7|CSD A7 (Bio)]]: Biographical article that does not assert significance",
"A7 - Unremarkable Group":"[[WP:CSD#A7|CSD A7 (Group)]]: Article about a club or group that does not assert significance",
"A7 - Unremarkable Band":"[[WP:CSD#A7|CSD A7 (Band)]]: Article about a band that does not assert [[WP:MUSIC|significance]]",
"A7 - Unremarkable Firm":"[[WP:CSD#A7|CSD A7 (Corp)]]: Article about a company that doesn't assert [[WP:CORP|significance]]",
"A7 - Unremarkable Web":"[[WP:CSD#A7|CSD A7 (Web)]]: Article about a web site that does not assert [[WP:WEB|significance]]",
"A7 - Other":"[[WP:CSD#A7|CSD A7]]: Article about subject that does not assert significance.",
"R1 - Bad Target":"[[WP:CSD#R1|CSD R1]]: Redirect to non-existent page",
"R2 - Crossnamespace":"[[WP:CSD#R2|CSD R2]]: Redirect to User/User talk namespaces from mainspace",
"R3 - Bad Typo":"[[WP:CSD#R3|CSD R3]]: Implausible typo",
"I1 - Redundant":"[[WP:CSD#I1|CSD I1]]: Redundant to another image",
"I2 - Corrupt/Empty":"[[WP:CSD#I2|CSD I2]]: Image is corrupted",
"I3 - Improper License":"[[WP:CSD#I3|CSD I3]]: Improper license",
"I4 - Lack of License":"[[WP:CSD#I4|CSD I4]]: No license or No Source Information",
"I5 - Unused Unfree":"[[WP:CSD#I5|CSD I5]]: Is unused and [[WP:NFCC|not free]]",
"I6 - No Fair Use":"[[WP:CSD#I6|CSD I6]]: No [[WP:FURG|justification]] given for fair use",
"I7 - Invalid Fair Use":"[[WP:CSD#I7|CSD I7]]: Bad [[WP:FURG|justification]] given for fair use",
"I8 - On Commons":"[[WP:CSD#I8|CSD I8]]: Image exists on the Commons",
"C1 - Empty":"[[WP:CSD#G1|CSD C1]]: Category is Empty (and has been for 4 days or more)",
"C2 - Speedy Rename":"[[WP:CSD#C2|CSD C2]]: Speedy Renaming",
"C3 - Template":"[[WP:CSD#C3|CSD C3]]: Corresponding template was deleted",
"U1 - User Request":"[[WP:CSD#U1|CSD U1]]: User requested deletion",
"U2 - No User":"[[WP:CSD#U2|CSD U2]]: Userpage of non-existent user",
"U3 - Fair Use Gallery":"[[WP:CSD#U3|CSD U3]]: Userpage that is primarily a gallery of fair use images",
"T1 - Divisive":"[[WP:CSD#T1|CSD T1]]: Template that is divisive and inflammatory.",
"P2 - Underpopulated":"[[WP:CSD#P2|CSD P2]]: Underpopulated",
"AFD close":"[[WP:AFD|AFD]]: Deleted after discussion at Articles for Deletion",
"Prod close":"[[WP:PROD|Prod]] left uncontested for five days."
} 

var wpReason, wpSelect, csdLink, defReason

this.onDeletePage = function(){
 //get wpReason
 wpReason = document.getElementById('wpReason')
 if (!wpReason) return
 if (window.wpReasonSize) wpReason.size = wpReasonSize
 //create drop-down box
 wpSelect = document.createElement('select')
 wpSelect.style.marginLeft = '5px'
 var i, ma, option
 for (i in reasons){
   option = document.createElement('option')
   option.value = reasons[i]
   option.title = reasons[i]
   option.appendChild(document.createTextNode(i))
   wpSelect.appendChild(option)
 }
 wpSelect.options[0].style.color = 'gray'
 wpReason.parentNode.insertBefore(wpSelect, wpReason.nextSibling) 
 //CSD link 
 csdLink = document.createElement('span')
 wpSelect.parentNode.insertBefore(csdLink, wpSelect.nextSibling)
 //'append default reason' link
 if (document.URL.indexOf('&wpReason=') == -1){
   defReason = wpReason.value
   var a = document.createElement('a')
   a.innerHTML = '<'
   a.title = 'Append default reason'
   a.href = 'javascript:delReason.appendDefaultReason()'
   a.style.marginLeft = '4px'
   wpReason.parentNode.insertBefore(a, wpReason.nextSibling)
 }
 //assign events
 addEvent(wpSelect, 'change', delReason.replaceReason)
 addEvent(wpReason, 'keyup', delReason.autoReplace)
 addEvent(wpReason, 'mouseup', delReason.autoReplace)
 delReason.autoReplace()
}

this.replaceReason = function(){
 wpReason.value = wpSelect.value
 var ma = wpSelect.value.match(/\[\[([^\]]+)#([^\]]+)\|.+\]\]/)
 if (ma && ma.length==3)  //show CSD link
   csdLink.innerHTML = ' (<a href="' 
     + wgArticlePath.replace(/\$1/,'') + ma[1] + '#' + ma[2] + '">' + ma[2] + '</a>)'
 else 
   csdLink.innerHTML = ''
}

this.autoReplace = function(){
 if (!wpReason.value.match(/^[GARICUTP]\d\d?$/)) return
 for (var i=1; i<wpSelect.options.length; i++)
   if (wpSelect.options[i].value.indexOf('#'+wpReason.value+'|') != -1){
     wpSelect.selectedIndex = i
     delReason.replaceReason() 
     break
   }
}

this.appendDefaultReason = function(){
 if (wpReason.value.indexOf(defReason.substring(0,15)) != -1) return
 if (wpReason.value) wpReason.value += '; '
 wpReason.value += defReason
}

}//delReason


function addEvent(obj, event, func) {
 if (obj.addEventListener) obj.addEventListener( event, func, false );
 else if (obj.attachEvent) obj.attachEvent ('on'+event, func)
}


if (wgAction == 'delete') addOnloadHook(delReason.onDeletePage)
else if (wgNamespaceNumber >= 0) addOnloadHook(delReason.onArticlePage)


//[[:ru:User:Alex Smotrov/adm/delreason.js]]