Jump to content

User:Lupin/editblind.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.
//DOWNLOADER

function newXml() {      
  return window.XMLHttpRequest ? new XMLHttpRequest()
    : window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP")
    : false;
}

function blind_download(bundle) {
  // mandatory: bundle.url
  // bundle.onSuccess 
  // bundle.onFailure
  // bundle.otherStuff OK too

  var x = newXml();
  if (x) {
    if (x.overrideMimeType) { x.overrideMimeType('text/xml'); }
    // else alert('Unable to override mime type - this will probably fail.');
    x.onreadystatechange=function() {
      x.readyState==4 && blind_downloadComplete(x,bundle);
    };
    x.open("GET",bundle.url,true);
    // x.setRequestHeader('Accept','text/*');
    x.send(null); 
  }
}

function blind_downloadComplete(x,bundle) {
  x.status==200 && ( bundle.onSuccess && bundle.onSuccess(x,bundle) || true )
  || ( bundle.onFailure && bundle.onFailure(x,bundle) || alert(x.statusText));
}

function blind_post(bundle) {
  // mandatory: bundle.url
  // bundle.onSuccess 
  // bundle.onFailure
  // bundle.data (to be sent) - array of objects { name: 'wpStartTime', data: '20051111091512' }
  // bundle.otherStuff OK too

  var x = newXml();
  if (x) {
    x.onreadystatechange=function() {
      x.readyState==4 && blind_downloadComplete(x,bundle);
    };
    x.open("POST",bundle.url,true);
    var sendMe='';
    for (var i=0; i<bundle.data.length; ++i) {
      if (!bundle.data[i]) continue;
      if (sendMe.length>0) sendMe+='&';
      sendMe+=URLEncode(bundle.data[i].name) + '=' + URLEncode(bundle.data[i].data);
    }
    x.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    //alert(sendMe);
    x.send(sendMe);
  }
}

// Source: http://www.albionresearch.com/misc/urlencode.php
function URLEncode( plaintext ) {
  // The Javascript escape and unescape functions do not correspond
  // with what browsers actually do...
  var SAFECHARS = "0123456789" + "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "abcdefghijklmnopqrstuvwxyz" + "-_.!~*'()";
  var HEX = "0123456789ABCDEF";
  var encoded = "";
  for (var i = 0; i < plaintext.length; i++ ) {
    var ch = plaintext.charAt(i);
    if (ch == " ") {
      encoded += "+";				// x-www-urlencoded, rather than %20
    } else if (SAFECHARS.indexOf(ch) != -1) {
      encoded += ch;
    } else {
      var charCode = ch.charCodeAt(0);
      if (charCode > 255) {
        alert( "Unicode Character '" + ch + "' cannot be encoded using standard URL encoding.\n" +
               "(URL encoding only supports 8-bit characters.)\n" + 
               "A space (+) will be substituted." );
        encoded += "+";
      } else {
        encoded += "%";
        encoded += HEX.charAt((charCode >> 4) & 0xF);
        encoded += HEX.charAt(charCode & 0xF);
      }
    }
  } // for
  return encoded;
}

window.harvestNamedChildren=function(node) {
  var ret=[];
  for (var i=0; i<node.childNodes.length; ++i) {
    var child=node.childNodes[i];
    if (child.nodeType != 1) continue;
    var childName=child.getAttribute('name');
    if (!childName || typeof childName != typeof '') continue;
    if (childName=='wpTextbox1') {
      var wikiData=child.childNodes[0].data;
      ret.push({name: 'wpTextbox1', data: wikiData});
    } else if (child.getAttribute('checked')) {
      ret.push({name: childName, data: (child.getAttribute('checked')=='checked' ? 'on' : 'off') });      
    } else {
      ret.push({name: child.getAttribute('name'), data: child.getAttribute('value')});
    }
    if (child.childNodes.length > 0) ret=ret.concat(harvestNamedChildren(child));
  }
  return ret;
}

window.getXmlObj=function(req) {
  if(req.responseXML && req.responseXML.documentElement) return req.responseXML;
  // Note: this doesn't work in konqueror, and the natural fix
  // var doc=document.implementation.createDocument(); doc.loadXML(req.responseXML)
  // results in a segfault :-(
  try {
    doc=new ActiveXObject("Microsoft.XMLDOM");
    doc.async="false";
    doc.loadXML(req.responseText);
  } catch (err) {return null;}
  return doc;
}

window.processEditPage=function(req, bundle){  
  //  alert(req.responseText);
  //  alert(req.responseXML);
  var xmlobj=getXmlObj(req);
  if(!xmlobj) { alert('could not get xml :-('); }
  var doc=xmlobj.documentElement;
  if(!doc) { alert('could not get xml documentElement. grrrr'); }
  var forms=doc.getElementsByTagName('form');
  if (!forms.length) alert('No forms found. Bailing...');
  var form=null;
  for (var i=0; i<forms.length; ++i) {
    if (forms[i].getAttribute('name')=='editform') {
      form=forms[i]; break;
    }
  }
  if (!form) alert('No edit form found. Darn.');
  var action=form.getAttribute('action');
  var postUrl=action;
  // if the url doesn't include the hostname, we have to fix that. probably.
  if (action.indexOf('http')!=0) {
    postUrl=bundle.url.split('/');
    postUrl=[ postUrl[0], postUrl[1], postUrl[2]].join('/') + action;
  }
  //alert(postUrl);
  var formData=harvestNamedChildren(form);
  for (var i=0; i<formData.length; ++i) {
    if (!formData[i]) continue;
    switch (formData[i].name) {
    case 'wpTextbox1': if(bundle.processWikiData) {
      formData[i].data=bundle.processWikiData(formData[i].data);
    }
      break;
    case 'wpPreview': case 'wpDiff':
      formData[i]=null;
      break;
    }
  }
  blind_post({data: formData, url:postUrl, onSuccess: function(req, b){alert(req.responseText);}});
}

function testblind() {
  var url='http://en.wikipedia.org/wiki/User:Lupin/sandbox?action=edit';  
  blind_download({url: url, onSuccess: processEditPage, processWikiData: function(txt) {return 'hello!'+Math.random() + '\n\n'+txt;}});
}

/// Local Variables: ///
/// mode:c ///
/// End: ///