User:Kevinalewis/bioassessmentsave.js

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.
// <nowiki>
// the code organization is horribly organized, but at this point--it works!
 var linkColor = "#002BB8";
 var linkLead = '<a style="color:' + linkColor + ';" href="/wiki/Category:Wikipedia 1.0 assessments">';
 var templateSelect;
 var livingSelect;
 var prioritySelect;
 var workgroupSelect;
 var extraSelect;
 var talkpage;
 var talkpagehtml; 
 var templateArray = new Array("WPBiography", "NovelsWikiProject");
 var livingArray = new Array("", "|living=yes", "|living=no");
 var priorityArray = new Array("", "Low", "Mid", "High", "Top");
 var workgroupArray = new Array("", "|a&e-work-group=yes", "|politician-work-group=yes", "|british-royalty=yes", "|royalty-work-group=yes", "|military-work-group=yes", "|sports-work-group=yes", "|s&a-work-group=yes", "|musician-work-group=yes");
 var extraArray = new Array("", "|listas=", "|listas=|needs-photo=yes", "|listas=|needs-infobox=yes", "|listas=|needs-infobox=yes|needs-photo=yes", "|needs-infobox=yes", "|needs-photo=yes");

function loadXMLDoc(url)
{
    // branch for native XMLHttpRequest object
    if (window.XMLHttpRequest) {
        var tpreq = new XMLHttpRequest();
    }
    // branch for IE/Windows ActiveX version
    else if (window.ActiveXObject) {
        var tpreq = new ActiveXObject("Microsoft.XMLHTTP");
    }

   if (tpreq) {
     tpreq.onreadystatechange = function () { 
       if (tpreq.readyState==4) {
         // if "OK"
         if (tpreq.status==200) {
           getAssessment(tpreq);
         }
         // else { alert("Problem retrieving data:" + tpreq.statusText); }
       }
      // else { alert("Problem retrieving data:" + tpreq.statusText); }
     };

     tpreq.open("GET", url, true);
     tpreq.send("");  
   }
}

function loadXMLDoc2(grade, url) //
{
    // branch for native XMLHttpRequest object
    if (window.XMLHttpRequest) {
        var tpreq = new XMLHttpRequest();
    }
    // branch for IE/Windows ActiveX version
    else if (window.ActiveXObject) {
        var tpreq = new ActiveXObject("Microsoft.XMLHTTP");
    }

   if (tpreq) {
     tpreq.onreadystatechange = function () { 
       if (tpreq.readyState==4) {
         // if "OK"
         if (tpreq.status==200) {
           postAssessment(grade, tpreq);
         }
         // else { alert("Problem retrieving data:" + tpreq.statusText); }
       }
      // else { alert("Problem retrieving data:" + tpreq.statusText); }
     };

     tpreq.open("GET", url, true);
     tpreq.send("");  
   }
}

function getAssessment(myreq)
{
  talkpage = myreq.responseText;
  var otherInfo = "";

  if ( talkpage.match( /\{\{ganominee/i ) ) { otherInfo='. <i>Currently a <a href="' + wgServer + '/w/index.php?title=Wikipedia:Good_article_candidates" style="color:' + linkColor + ';">Good Article candidate</a></i>.';}

  else if ( talkpage.match(/\{\{fac}/i ) ) { otherInfo='. <i>Currently a <a href="' + wgServer + '/w/index.php?title=Wikipedia:Featured_article_candidates/' + wgPageName + '&redirect=no" style="color:' + linkColor + ';">Featured Article candidate</a></i>.';}

  if ( talkpage.match(/class *= *a/i) ) { update("#66ffff", "An " + linkLead + "A-class</a> article", otherInfo);}

  else if ( talkpage.match(/\{\{ga[|}]/i ) || talkpage.match(/currentstatus *= *ga/i) ) { update("#66ff66", 'A <a style="color:' + linkColor + ';" href="/wiki/Wikipedia:Good Articles">Good Article</a>', otherInfo);}

  else if ( talkpage.match(/class *= *b/i) ) { update("#c2ff66", "A " + linkLead + "B-class</a> article", otherInfo);}

  else if ( talkpage.match(/class *= *c/i) ) { update("#ffff66", "A " + linkLead + "C-class</a> article", otherInfo);}

  else if ( talkpage.match(/class *= *start/i) ) { update("#ffaa66", "A " + linkLead + "Start-class</a> article", otherInfo);}

  else if ( talkpage.match(/class *= *stub/i) ) { update("#ff6666", "A " + linkLead + "Stub-class</a> article", otherInfo);}

  else { update("#000000", "An " + linkLead + "unassessed</a> article", otherInfo);}
}

function addAssessment(grade) {
  template = templateSelect.options[templateSelect.selectedIndex].value;
  living = livingSelect.options[livingSelect.selectedIndex].value;
  priority = prioritySelect.options[prioritySelect.selectedIndex].value;
  workgroup = workgroupSelect.options[workgroupSelect.selectedIndex].value;
  extra = extraSelect.options[extraSelect.selectedIndex].value;
  if (talkpage == null) { xxtalkpage = talkpage; talkpage = "";}
  if ( talkpage.match(new RegExp("(\{\{"+template+"[^}]*)([A-Za-z]*)[^}]*}}", "i" ) ) )
    { talkpage = talkpage.replace(RegExp.$1+RegExp.$2, "\r\n{{"+template+living+"|class="+grade+"|priority="+priority+workgroup+extra); }
  else if ( talkpage.match(new RegExp("(\{\{"+template+" *)([}|] *[^}]*}})", "i" ) ) )
    { talkpage = talkpage.replace(RegExp.$1+RegExp.$2, RegExp.$1 + living + "|class=" + grade + "|priority=" + priority + workgroup+extra); }
  else if ( talkpage.match(/(\{\{[^}]*}})/ ) )
    { talkpage = talkpage.replace(RegExp.$1, RegExp.$1 + "\r\n{{"+template+living+"|class="+grade+"|priority="+priority+workgroup+extra+"}}\r\n"); }
  else { talkpage = "{{"+template+living+"|class="+grade+"|priority="+priority+workgroup+extra+"}}\r\n" + xxtalkpage + talkpage; }
  loadXMLDoc2(grade, mw.config.get('wgServer') + mw.config.get('wgScriptPath') + "/index.php?title=Talk:" + encodeURIComponent(wgTitle) + "&action=edit");
}

function postAssessment(grade, myreq) {
  talkpagehtml = myreq.responseText;
  var minorEdit = '<input name="wpMinoredit" type="hidden" value="1" id="wpMinoredit" />';
  if (typeof assessmentMarkAsMinor != "undefined" ) {
    if ( !assessmentMarkAsMinor ) { minorEdit = ""; }
  }
  else
  if ( talkpagehtml.match(/<input type=.hidden. value="([\w\d]+)." name="wpEditToken"/i) ) {
    var editToken = RegExp.$1;
  }
  if ( talkpagehtml.match(/<input name="wpAutoSummary" type="hidden" value="([\w\d]+)"/i) ) {
    var autoSummary = RegExp.$1;
  }
  if ( talkpagehtml.match(/<input type=.hidden. value="([\d]+)" name="wpStarttime"/i) ) {
    var startTime = RegExp.$1;
  }
  if ( talkpagehtml.match(/<input type=.hidden. value="([\d]+)" name="wpEdittime"/i) ) {
    var editTime = RegExp.$1;
  }
  mydiv = document.createElement("div");
  mydiv.innerHTML = '<form style="visibility: hidden;" id="editform" name="editform" method="post" action="/w/index.php?title=Talk:' + encodeURIComponent(mw.config.get('wgPageName')) + '&action=edit" enctype="multipart/form-data"><textarea tabindex="1" accesskey="," name="wpTextbox1" id="wpTextbox1"></textarea><input tabindex="2" type="text" value="" name="wpSummary" id="wpSummary" maxlength="200" size="60" />' + minorEdit + '<input id="wpPreview" name="wpPreview" type="submit"/><input type="hidden" value="' + startTime + '" name="wpStarttime" /><input type="hidden" value="' + editTime + '" name="wpEdittime" /><input type="hidden" value="'+ editToken + '\" name="wpEditToken" /><input name="wpAutoSummary" type="hidden" value="' + autoSummary + '" /></form>';
  document.body.appendChild(mydiv);
  document.editform.elements["wpTextbox1"].value = talkpage;
  document.editform.elements["wpSummary"].value = 'Assessed [[Wikipedia:WikiProject Biography|WikiProject Biography]] article (' + grade + ' class)';
  document.editform.submit();
}

function update(color, slogan, info) {
  document.getElementsByTagName("h1")[0].style.cssText = "color: " + color + ";";
  t = document.getElementById("siteSub").innerHTML;
  document.getElementById("siteSub").innerHTML = "<i>" + slogan + "</i> " +             t.charAt(0).toLowerCase() + t.substring(1) + info;
  templateSelect = document.createElement("select");
  for (x = 0; x < templateArray.sort().length ; x++) {
    templateSelect.options[x] = new Option(templateArray[x], templateArray[x]);
    if (typeof defaultProject != "undefined" && templateArray[x] == defaultProject) {
      var projectIndex = x;
    }
  }
  if (projectIndex >= 0 ) { templateSelect.selectedIndex = projectIndex; }
  document.getElementById("contentSub").appendChild(templateSelect);
  livingSelect = document.createElement("select");
  for (x = 0; x < livingArray.sort().length ; x++) {
    livingSelect.options[x] = new Option(livingArray[x], livingArray[x]);
    if (typeof defaultLiving != "undefined" && livingArray[x] == defaultLiving) {
      var livingIndex = x;
    }
  }
  if (livingIndex >= 0 ) { livingSelect.selectedIndex = livingIndex; }
  document.getElementById("contentSub").appendChild(livingSelect);
  prioritySelect = document.createElement("select");
  for (x = 0; x < priorityArray.sort().length ; x++) {
    prioritySelect.options[x] = new Option(priorityArray[x], priorityArray[x]);
    if (typeof defaultPriority != "undefined" && priorityArray[x] == defaultPriority) {
      var priorityIndex = x;
    }
  }
  if (priorityIndex >= 0 ) { prioritySelect.selectedIndex = priorityIndex; }
  document.getElementById("contentSub").appendChild(prioritySelect);
  workgroupSelect = document.createElement("select");
  for (x = 0; x < workgroupArray.sort().length ; x++) {
    workgroupSelect.options[x] = new Option(workgroupArray[x], workgroupArray[x]);
    if (typeof defaultWorkgroup != "undefined" && workgroupArray[x] == defaultWorkgroup) {
      var workgroupIndex = x;
    }
  }
  if (workgroupIndex >= 0 ) { workgroupSelect.selectedIndex = workgroupIndex; }
  document.getElementById("contentSub").appendChild(workgroupSelect);
  extraSelect = document.createElement("select");
  for (x = 0; x < extraArray.sort().length ; x++) {
    extraSelect.options[x] = new Option(extraArray[x], extraArray[x]);
    if (typeof defaultExtra != "undefined" && extraArray[x] == defaultExtra) {
      var extraIndex = x;
    }
  }
  if (extraIndex >= 0 ) { extraSelect.selectedIndex = extraIndex; }
  document.getElementById("contentSub").appendChild(extraSelect);
    var grades = document.createElement("div");
  grades.innerHTML = '<a href="javascript:addAssessment(\'Stub\')">Stub</a> <a href="javascript:addAssessment(\'Start\')">Start</a> <a href="javascript:addAssessment(\'C\')">C</a> <a href="javascript:addAssessment(\'B\')">B</a> <a href="javascript:addAssessment(\'GA\')">GA</a> <a href="javascript:addAssessment(\'A\')">A</a>';
  document.getElementById("contentSub").appendChild(templateSelect);
  document.getElementById("contentSub").appendChild(livingSelect);
  document.getElementById("contentSub").appendChild(prioritySelect);
  document.getElementById("contentSub").appendChild(workgroupSelect);
  document.getElementById("contentSub").appendChild(extraSelect);
  document.getElementById("contentSub").appendChild(grades);
}

function begin() {
  if ( wgNamespaceNumber==0 && !location.pathname.match('/w/index.php') ) {
    if ( document.getElementById("ca-talk").className == "new" ) {
      update("#000000", "An " + linkLead + "unassessed</a> article", "");
    }
    else if ( document.getElementById("featured-star") ) {update("#0066CC", 'A <a style="color:' + linkColor + ';" href="/wiki/Wikipedia:Featured Articles">Featured Article</a>', "");}
    else {
      var url = mw.config.get('wgServer') + mw.config.get('wgScriptPath') + "/index.php?title=Talk:" + encodeURIComponent(wgTitle) + "&action=raw";
      loadXMLDoc(url);
    }
  }
}

addOnloadHook(begin);
// </nowiki>