Utilisateur:JoKerozen/ajax.js

Un article de Wikipédia, l'encyclopédie libre.

Note : Après avoir publié la page, vous devrez forcer son rechargement pour voir les changements : Mozilla / Konqueror / Firefox : Shift-Ctrl-R, Internet Explorer / Opera : Ctrl-F5, Safari : Cmd-R.

//<source lang="javascript" line>
/*
 * PapierAjax
 *
 * La fonction PapierAjax sert à rédiger sur wikipédia de manière naturelle, 
 * par simple doubleclic et confimation de sauvegarde. 
 * La définition de ce script se trouve ici : 
 * http://fr.wikipedia.org/wiki/Wikip%C3%A9dia:Brouillons/interface_ajax
 *
 * Le code du script est librement inspiré du système de modification-inline de Olivier Lance
 * http://olance.developpez.com/articles/web/javascript/modification-inline/
 *
 * Auteurs : ivo talvet (fr:user:Tavernier)
 * Date de dernière révision : 13 septembre 2007
 */
 
function findBlocPapierAjax(numSection) {
    return function(){
this.ou = numSection
alert(numSection);
var contenu = wpajaxPapierAjax.http({ url: wgServer+wgScriptPath+'/index.php?title='+encodeURIComponent(wgTitle)+'&action=raw'+'&section='+numSection, onSuccess: afficheBlocPapierAjax});};
}
 
function afficheBlocPapierAjax(contenu, ou) {
    var texte = gml_XMLParse(contenu.responseText);
    alert(contenu.responseText);
    alert(ou);
    var bloc = document.getElementsByTagName('a')[0];
    var textarea = document.createElement("textarea");
    textarea.appendChild(document.createTextNode(contenu.responseText));
    bloc.appendChild(textarea);
}
 
function PapierAjax() {
   if (!wgIsArticle) return; //si non editable, sortie
   var intro = getElementsByClass("firstHeading")[0];
   intro.addEventListener("dblclick", findBlocPapierAjax(0), false);
   var sections = getElementsByClass("mw-headline");
   for each (var section in sections) {
       var hrefSection = getElementsByClass("editsection", section.parentNode)[0].getElementsByTagName('a')[0].getAttribute('href');
       var numSection = hrefSection.replace(/.*section=([0-9]+)/g, '$1');
       section.addEventListener("dblclick", findBlocPapierAjax(numSection), false);
   }
}
 
var wpajaxPapierAjax = { // depuis http://fr.wikipedia.org/wiki/Utilisateur:EDUCA33E/LiveRC.js
  http: function(bundle) {
    // mandatory: bundle.url
    // optional:  bundle.async
    // optional:  bundle.method
    // optional:  bundle.headers
    // optional:  bundle.data
    // optional:  bundle.onSuccess (xmlhttprequest, bundle)
    // optional:  bundle.onFailure (xmlhttprequest, bundle)
    // optional:  bundle.otherStuff OK too, passed to onSuccess and onFailure
    var xmlhttp;
    try {
      xmlhttp = new XMLHttpRequest(); 
    } catch(e) { 
      try {
        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
      } catch (e) { 
        try {
          xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (e) {
          xmlhttp = false
        }
      }
    }
 
    if (xmlhttp) {
      xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4)
          wpajaxPapierAjax.httpComplete(xmlhttp,bundle);
      };
      xmlhttp.open(bundle.method ? bundle.method : "GET",bundle.url,bundle.async == false ? false : true);
      if (bundle.headers) {
        for (var field in bundle.headers)
          xmlhttp.setRequestHeader(field,bundle.headers[field]);
      }
      xmlhttp.send(bundle.data ? bundle.data : null); 
    }
    return xmlhttp;
  },
 
  httpComplete: function(xmlhttp,bundle) {
    if (xmlhttp.status == 200 || xmlhttp.status == 302) {
      if (bundle.onSuccess)
        bundle.onSuccess(xmlhttp,bundle);
    } else if (bundle.onFailure) {
      bundle.onFailure(xmlhttp,bundle);
    } else {
      // A activer en debug mode ?
      // alert(xmlhttp.statusText);
    }
  }
};
 
// Parser
if (document.implementation.createDocument) {
  var gml_xmlparser = new DOMParser();
}
 
function gml_XMLParse(string) {
  if (document.implementation.createDocument) {
    return gml_xmlparser.parseFromString(string, "text/xml");
  } else if (window.ActiveXObject) {
    var gml_xmldoc = new ActiveXObject("Microsoft.XMLDOM");
    gml_xmldoc.async = "false";
    ret = gml_xmldoc.loadXML(string);      
    if (!ret)
      return null;
    return gml_xmldoc.documentElement;
  }
  return null;
}
 
addLoadEvent(PapierAjax);
//</source>[[Catégorie:MediaWiki:Fonction Monobook en JavaScript]]