User:Lupin/editblind.js

In this article we will delve into the fascinating world of User:Lupin/editblind.js, exploring its different facets and characteristics that make it so relevant in today's society. From its origin to its impact today, we will delve into its history, evolution and relevance in the contemporary context. Through a detailed analysis, we will discover how User:Lupin/editblind.js has influenced various aspects of daily life, as well as more specific areas such as culture, economics or politics. With a broad and diverse approach, we aim to shed light on this very relevant topic to better understand the world around us.
//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) continue;
      if (sendMe.length>0) sendMe+='&';
      sendMe+=URLEncode(bundle.data.name) + '=' + URLEncode(bundle.data.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;
    if (child.nodeType != 1) continue;
    var childName=child.getAttribute('name');
    if (!childName || typeof childName != typeof '') continue;
    if (childName=='wpTextbox1') {
      var wikiData=child.childNodes.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.getAttribute('name')=='editform') {
      form=forms; 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, postUrl].join('/') + action;
  }
  //alert(postUrl);
  var formData=harvestNamedChildren(form);
  for (var i=0; i<formData.length; ++i) {
    if (!formData) continue;
    switch (formData.name) {
    case 'wpTextbox1': if(bundle.processWikiData) {
      formData.data=bundle.processWikiData(formData.data);
    }
      break;
    case 'wpPreview': case 'wpDiff':
      formData=null;
      break;
    }
  }
  blind_post({data: formData, url:postUrl, onSuccess: function(req, b){alert(req.responseText);}});
}

function testblind() {
  var url='http://en.wikipedia.orghttps://wikifreehand.com/en/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: ///