Jump to content

User:Year2000Prob/Y2P.Wijits.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.
Y2P.Wijits = new Object();

Y2P.Wijits.StatusChanger = function(StatusNames) {
  try {
    var li = document.createElement("li");
    li.id = "pt-status";
    Y2P.Wijits._StatusNames = StatusNames;
    for (var i=0; i<StatusNames.length; i++) {
      var a = document.createElement("a");
      var span = document.createElement("span");
      a.onclick = function(e) {
        var textbox = document.createElement("input");
        textbox.type = "text";
        if (e.which == 3) {
          a.parentNode.innerHTML = textbox;
          textbox.onblur = Y2P.Wijits._SetStatus(i, textbox.value);
        } else {
          Y2P.Wijits._SetStatus(i, StatusNames[i].defaultText);
        };
      };
      a.appendChild(document.createTextNode(StatusNames[i].defaultText));
      span.appendChild(a);
      if (i != 0) li.appendChild(document.createTextNode("|"));
      li.appendChild(span);
    };
    document.getElementById("p-personal").appendChild(li);
  } catch(e) {
    if (Y2P.Debug.Enabled) {
      Y2P.Debug.Retry("Y2P Status Changer\nError: " + e, {
        onAccept: function() {
          Y2P.Wijits.StatusChanger(StatusNames);
        }
      });
    }
  };
};

Y2P.Wijits._SetStatus = function(number, text) {
  var prelimresp = new XMLHttpRequest();
  prelimresp.open("GET", "/index.php?title="+pagename+"&action=submit", false);
  var edittime = prelimresp.responseText.match(/\<input type='hidden' value="(\d+)" name="wpEdittime" \/\>/)[1];
  var edittoken = prelimresp.responseText.match(/\<input type='hidden' value="([0-9a-f]+\\?)" name="wpEditToken" \/\>/)[1];
  var XHR = new XMLHttpRequest();
  XHR.open("POST", mw.config.get('wgServer')+mw.config.get('wgScript')+"?title=User:"+wgUserName+"/Status&action=submit", false);
  XHR.send("wpEdittime="+edittime+"&wpEditToken="+edittoken+"&wpTextbox1="+escape(Y2P.Wijits._StatusNames[number].templatePre)+escape(text)+escape(Y2P.Wijits._StatusNames[number].templateSuf)+"&wpSummary="+escape(text)+"&wpSection=&wpSave=Save+page");
  var headers = {"Content-Type":"application/x-www-form-urlencoded"};
  for (header in headers) {
    XHR.setRequestHeader(header,headers[header]);
  };
};

Y2P.Wijits._CreateStatusType = function() {
  var StatusNames = [];
  var name = prompt("Enter status name:");
  var template = prompt("Enter status template (place \"(statusText)\" in the middle):");
  var pre = template.substr(0, "(statusText)");
  var suf = template.substr("(statusText)");
  StatusNames.push({templatePre:pre,templateSuf:suf,defaultText:name});
  if (confirm("Do you want to add another status type?")) {
    Y2P.Wijits._createStatusType(StatusNames);
  } else {
    return StatusNames;
  }
};

if (typeof Wijits == "object") {
  Wijits.List.push({
    name: "Y2P Status Changer",
    onEnabled: function() {
      Y2P.Wijits.StatusChanger(Y2P.Wijits._CreateStatusType());
    },
    onDisabled: function() {
      if (document.getElementById("pt-status")) document.getElementById("p-personal").removeChild(document.getElementById("pt-status"));
      if (Y2P.Wijits._StatusNames) delete Y2P.Wijits._StatusNames;
    },
    retriveConfigBeforeFullyEnabled: true
  });
}