Jump to content

User:Invitatious/unsigned2.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.
//<pre><nowiki>

// Below (unsigned2.js version 1.3,) by Invitatious. Dual-licensed under the terms of the GFDL v1.2 or the GPL v2.
// New features in this version: you can now choose an edit from the last 16.
function addSigWikiCode() {
  // From revision 28011729 of [[en:Wikipedia:WikiProject_User_scripts/Scripts/Get_tidy_title]]
  function aswcGet_tidy_title() {
    var editlk = document.getElementById('ca-edit').getElementsByTagName('a')[0].href;
    // cut everything up to "title=" from the start and everything past "&action=edit" from the end
    editlk = editlk.substring(editlk.indexOf('title=') + 6, editlk.lastIndexOf('&action=edit'));
    return editlk;
  }
  // LOCALIZABLE STRINGS START.
  var lsMonth_names = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"],
  lsConflict = "Edit conflict.",
  lsDialog1 = "Please select an edit below:\n\n",
  lsDialog2 = "Unsigned edit number:",
  lsInvalid1 = "Please enter a valid number or cancel this operation.",
  lsInvalid2 = "This is not a valid choice. Please choose another option.",
  lsNoRev = "No revisions found. Does the page exist?",
  lsNoXMLHTTP = "Couldn't get XMLHTTP!";
  // LOCALIZABLE STRINGS END
  var xmlhttp = window.XMLHttpRequest ? new XMLHttpRequest() : false // Try to use native XMLHTTP
  if (!xmlhttp && window.ActiveXObject) {
 // ActiveX XMLHTTP
    xmlhttp = new ActiveXObject("Msxml2.XMLHTTP")
    if (!xmlhttp) xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  }
  if (xmlhttp) {
    xmlhttp.open("GET", "/w/query.php?format=xml&what=revisions&rvlimit=16&rvcomments&titles=" + aswcGet_tidy_title()); // Get timestamp, user, summary of last 16 edits
    xmlhttp.onreadystatechange = function() {


    if (xmlhttp.readyState != 4) return;
    var xmldoc = xmlhttp.responseXML;
    if (!xmldoc.documentElement && window.ActiveXObject) { // IE workaround suggested by [[User:Olliminatore]]
      document.body.style.backgroundColor = "#CCCCFF"; // Debugging aid
      xmldoc = new ActiveXObject("Microsoft.XMLDOM");
      xmldoc.async = false;
      xmldoc.loadXML(xmlhttp.responseText);
    } else {
      var revisions = xmldoc.documentElement.getElementsByTagName("rv"); // Get the revisions
    }
    if (revisions.length > 0) {
      var t = revisions[0].getAttribute("timestamp").replace(/[^0-9]/g, ""); // Get rid of non-numeric characters in timestamp
      if(t != document.editform.wpEdittime.value) { // Detect an edit conflict
        alert(lsConflict);
        return;
      }
      var dialog_text = lsDialog1;
      var edit_data = [];
      for (var n = 0; n < revisions.length; n++) { // Extract edit data and build dialog text
        if (revisions[n].getAttribute("minor") != null) continue; // Skip minor edits
        edit_data[n] = { timestamp: revisions[n].getAttribute("timestamp").replace(/[^0-9]/g, ""), 
                         user: revisions[n].getAttribute("user"),
                         comment: revisions[n].getAttribute("comment") };
        dialog_text += ("[" + n.toString(16).toUpperCase() + "] " + edit_data[n].timestamp + " " +
         edit_data[n].user + ": " + edit_data[n].comment).substring(0, 80) + "\n";
        
      }
      while (true) {
        if (navigator.product == "Gecko") {
          var unsigned_edit = prompt(dialog_text + "\n" + lsDialog2, "0"); // for Mozilla
        } else {
          alert(dialog_text); // for IE or other
          var unsigned_edit = prompt(lsDialog2, "0"); // for IE or other
        }
        if (unsigned_edit == null) { // Cancel button
          return;
        } else if (isNaN(unsigned_edit = parseInt(unsigned_edit, 16))) { // Non-numeric input
          alert(lsInvalid1);
        }
        else if (!(unsigned_edit = edit_data[unsigned_edit])) { // Non-existent edit
           alert(lsInvalid2);
        } else {
        t = unsigned_edit.timestamp; // So the full name doesn't have to be used
        // LOCALIZABLE STRINGS START (enwiki)
        insertTags("", " {{subst:unsigned2|" + t.substring(8, 10) + ":" + t.substring(10, 12) + ", " + (t.substring(6, 8) - 0) + " " + lsMonth_names[t.substring(4, 6) - 1] + " " + t.substring(0, 4) + "|" + unsigned_edit.user + "}}", "");
        // LOCALIZABLE STRINGS END
        return;
        }
      }
    } else { // No revisions
      alert(lsNoRev);
    }


    }; xmlhttp.send(null);
  } else {
    alert(lsNoXMLHTTP); // No XMLHTTP
    return;
  }
}
addOnloadHook(function(){
  // From revision 28011435 of [[en:Wikipedia:WikiProject_User_scripts/Scripts/Add_LI_link]]
  function aswcAddlilink(tabs, url, name, id, title, key) {
    var na = document.createElement('a');
    na.href = url;
    na.appendChild(document.createTextNode(name));
    var li = document.createElement('li');
    if(id) li.id = id;
    li.appendChild(na);
    tabs.appendChild(li);
    if(id)
    {
        if(key && title)
        {
            ta[id] = [key, title];
        }
        else if(key)
        {
            ta[id] = [key, ''];
        }
        else if(title)
        {
            ta[id] = ['', title];
        }
    }
    // re-render the title and accesskeys from existing code in wikibits.js
    akeytt();
    return li;
} 
//
  if (/[&?]action=edit/.test(location.search)) {
    aswcAddlilink(document.getElementById('p-cactions').getElementsByTagName('ul')[0], "javascript:addSigWikiCode();", /* LOCALIZABLE STRINGS START */ "unsigned2" , "ca-unsigned2", "Mark unsigned comment", "" /* LOCALIZABLE STRINGS END */);
  }
});
// Above by Invitatious. Dual-licensed under the terms of the GFDL v1.2 or the GPL v2.
//</nowiki></pre>