Utilisateur:JoKerozen/ajax.js

Une page de Wikipédia, l'encyclopédie libre.
Note : après avoir enregistré la page, vous devrez forcer le rechargement complet du cache de votre navigateur pour voir les changements.

Mozilla / Firefox / Konqueror / Safari : maintenez la touche Majuscule (Shift) en cliquant sur le bouton Actualiser (Reload) ou pressez Maj-Ctrl-R (Cmd-R sur Apple Mac) ;

Chrome / Internet Explorer / Opera : maintenez la touche Ctrl en cliquant sur le bouton Actualiser ou pressez Ctrl-F5.
//<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: mw.config.get('wgServer')+mw.config.get('wgScriptPath')+'/index.php?title='+encodeURIComponent(mw.config.get('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 (!mw.config.get('wgIsArticle')) return; //si non editable, sortie
   var intro = document.querySelector(".firstHeading");
   intro.addEventListener("dblclick", findBlocPapierAjax(0), false);
   var sections = document.querySelectorAll(".mw-headline");
   for (var i = 0, l = sections.length; i < l; ++i) {
       // renamed to mw-editsection in MediaWiki 1.22
       var hrefSection = sections[i].parentNode.querySelector(".mw-editsection a, .editsection a").getAttribute('href');
       var numSection = hrefSection.replace(/.*section=([0-9]+)/g, '$1');
       sections[i].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;
}

$(PapierAjax);
//</source>{{Catégorisation JS|Ajax}}