User:Xenocidic/autofill.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.
// restores "autofill" functionality that used to be available when deleting pages
// it was removed per [[Wikipedia:Village pump (proposals)/Archive 40#autofill]]
// portion of content gets pasted into deletion reason ("Content was..." and "only contributor was...")
// this was written by [[User:CharlotteWebb]], I take no credit whatsoever.

// *** REMEMBER TO REMOVE INAPPROPRIATE CONTENT WHERE APPROPRIATE, i.e. attack pages, etc. ***

s1 = "content was: '$1'"; s2 = "content before blanking was: '$1'";
s3 = " (and the only contributor was \"[[Special:Contributions/$2|$2]]\")";
function bytes(s){ return encodeURIComponent(s).replace(/\%[0-9A-F]{2}/gi, '_').length; }
function contentwas(){
  if(mw.config.get('wgAction')!="delete" || !(field = document.getElementById("wpReason"))) return;
  x = new XMLHttpRequest();
  x.open("GET", mw.config.get('wgServer') + "/w/api.php?action=query&format=xml&prop=revisions&rvprop=user|content&rvlimit=25&titles=" + encodeURIComponent(mw.config.get('wgPageName')), true);
  x.onreadystatechange = function() {
    if(x.readyState != 4) return; z = new DOMParser().parseFromString(x.responseText,"text/xml");
    rev = z.getElementsByTagName("rev"); content = "";
    if(rev[0].childNodes.length) { content = rev[0].childNodes[0].nodeValue; s = s1; }
    else { s = s2; for(i = 1; i < rev.length && !content.length; i++)
      if(rev[i].childNodes.length) content = rev[i].childNodes[0].nodeValue; }
    if(!content.length) { field.value = "page was blank"; return; }
    content = content.replace(/\s+/, " ");
    only = rev[0].getAttribute("user");
    for(i = 1; i < rev.length; i++) if(rev[i].getAttribute("user") != only) { only = null; break; }
    if(only && bytes(only) > 64) only = null;
    if(only) s += s3.replace("$2", only, "g");
    limit = 257-bytes(s); // SIC!
    if(bytes(content) > limit){ content = content.substring(0, limit);
      while(bytes(content) > limit - 3) content = content.substring(0, content.length-1);
      content += "..."; }
    field.value = s.replace("$1", content);
    }
  x.send("");
  }
addOnloadHook(function(){ setTimeout("contentwas()", 1000); });