Jump to content

User:MZMcBride/delete.js

From Wikipedia, the free encyclopedia
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.
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.
function deleteReason() {
  mw.util.addPortletLink('p-tb','javascript:deleteReasonUpdate()','Delete reason','t-deletereason','Change the default delete reason');
}
addOnloadHook(deleteReason);
 
function deleteReasonUpdate() {
  var reason = parseInt(getCookie('defaultDeleteReason'));
  reason = prompt('Enter a default deletion reason\n\(tab name&&deletion summary\)',reason);
  setCookie('defaultDeleteReason',reason);
}
 
// Cookie helpers, modified from en.wiktionary
function setCookie(cookieName, cookieValue) {
 var today = new Date();
 var expire = new Date();
 var nDays = 365;
 expire.setTime( today.getTime() + (3600000 * 24 * nDays) );
 document.cookie = cookieName + "=" + escape(cookieValue)
                 + ";path=/"
                 + ";expires="+expire.toGMTString();
}
 
function getCookie(cookieName) {
  var start = document.cookie.indexOf( cookieName + "=" );
  if ( start == -1 ) return "";
  var len = start + cookieName.length + 1;
  if ( ( !start ) &&
    ( cookieName != document.cookie.substring( 0, cookieName.length ) ) )
      {
        return "";
      }
  var end = document.cookie.indexOf( ";", len );
  if ( end == -1 ) end = document.cookie.length;
  return unescape( document.cookie.substring( len, end ) );
}
 
function deleteCookie(cookieName) {
  if ( getCookie(cookieName) ) {
    document.cookie = name + "=" +
    ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
  }
}

addOnloadHook(checkdelete);
function checkdelete() {
    if(queryString("submitdelete")=="true") document.getElementById('deleteconfirm').wpConfirmB.click()
    if(queryString("submitdelete")=="true") document.getElementById('deleteconfirm').getElementById('mw-filedelete-submit').click()
}

if (mw.config.get('wgNamespaceNumber') != -1 && getCookie("defaultDeleteReason")) addOnloadHook(addDeleteButtons)
function addDeleteButtons() {
  if(!document.getElementById('ca-delete')) return
  var url=document.getElementById('ca-delete').firstChild.href

  var reason = getCookie('defaultDeleteReason');
  var reasontxt = reason;
  if(reasontxt.indexOf('&&')!=-1) reasontxt = reasontxt.substring(0,reasontxt.indexOf('&&'))
  if(reason.indexOf('&&')!=-1) reason = reason.substring(reason.indexOf('&&')+2)
  mw.util.addPortletLink('p-cactions', url + '&submitdelete=true&wpReason=' + encodeURIComponent(reason),reasontxt,'ca-delete1');
}

function queryString(p) {
  var re = RegExp('[&?]' + p + '=([^&]*)');
  var matches;
  if (matches = re.exec(document.location)) {
    try { 
      return decodeURI(matches[1]);
    } catch (e) {
    }
  }
  return null;
}