Jump to content

User:RadicalOne/vector.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.
addOnloadHook(statusLinks);

function statusLinks() {
  if (document.getElementById('user-status')) {
    for (var sp in document.getElementById('user-status').getElementsByTagName('span')) {
      addHandler(document.getElementById('user-status').getElementsByTagName('span')[sp], 'click', updateMyStatus);
    };
  };
};

function updateMyStatus() {
  var statusText = this.innerHTML;
  var summ = 'updating status';
  var token;

  apicall('action=query&prop=info&intoken=edit&titles=User:'+wgUserName+'/Status', function(res) {
    for (var page in res.query.pages) {
      token= res.query.pages[page].edittoken.replace(/\+\\$/g,'%2B%5C');
    };
  });

  apicall('action=edit&title=User:'+wgUserName+'/Status&summary='+summ+'&recreate=1&text='+statusText+'&token='+token, function(res) {
    if (res.edit.result != "Success") {
      alert("Couldn't edit the status page.");
    };
  });

  window.location.reload();
};

function apicall(query, callback) {
  try {
    var xmlRequest = new XMLHttpRequest();
  } catch(e) {
    try {
      var xmlRequest = new ActiveXObject("Msxml2.XMLHTTP");
    } catch(e) {
      try {
      var xmlRequest = new ActiveXObject("Microsoft.XMLHTTP");
      } catch(e) {
        throw "Could not create an XmlHttpRequest";
        }
      }
    }

  xmlRequest.open('POST', wgScriptPath+'/api.php', false);
  xmlRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  xmlRequest.send(query+'&format=json');
  var xmlResponse = new Function("return "+xmlRequest.responseText)();
  if (!xmlResponse) {
    alert("Couldn't access the API.");
  } else {
    callback(xmlResponse);
  };
};