Utilisateur:Dr Brains/ContributionsDeluxe.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.
var ContributionsDeluxe_LinkText = "[Modification postérieures]"

var ContributionsDeluxe_DefaultTemplate = ''
    + '<a href="'+mw.config.get('wgServer')+mw.config.get('wgScript')+'?title=$0&oldid=$2">$1</a>'
    + ' – User : '
    + '<a href="'+mw.config.get('wgServer')+mw.config.get('wgArticlePath').split("$1").join(mw.config.get('wgFormattedNamespaces')[2]+':$3')+'">$3</a>'
    + ' ($4)';

// ///////////////////////////////////////////////////// FONCTION DE REQUÊTE ///////////////
 
var ContributionsDeluxe_ajax = {
  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)
          ContributionsDeluxe_ajax.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)
          try {
            xmlhttp.setRequestHeader(field,bundle.headers[field]);
          } catch(err) {
            //Argos42 : pour rendre LiveRC fonctionnel sur chrome
          }
      }
      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.status+xmlhttp.responseText);
    }
  }
};

function ContributionsDeluxe_AddLinks(){
    var BC = document.getElementById('bodyContent');
    if(!BC) BC = document.getElementById('article');
    if(!BC) BC = document.getElementById('mw_contentholder');
    if(!BC) return;
    var ContribList = BC.getElementsByTagName('ul')[0];
    if(!ContribList) return;
    var Contribs = ContribList.getElementsByTagName('li');
    for(var a=0,l=Contribs.length;a<l;a++){
        if(!ContributionsDeluxe_IsTop(Contribs[a])) ContributionsDeluxe_AddLink(Contribs[a], a);
    }
}

function ContributionsDeluxe_IsTop(Li){
    var IsTop = false;
    var Spans = Li.getElementsByTagName('span');
    for(var x=0,z=Spans.length;x<z;x++){
        if($(Spans[x]).hasClass("mw-uctop")) IsTop = true;
    }
    return IsTop;
}

function ContributionsDeluxe_AddLink(Li, LIindex){
    var Num = -1;
    var Index = 0;
    Li.id = "Li_"+LIindex;
    var Links = Li.getElementsByTagName('a');
    for(var a=0,l=Links.length;a<l;a++){
        if(Links[a].innerHTML == "diff") Num = -2;
        if(Links[a].innerHTML == "hist") Index = a;
    }
    var OldidLink = Links[(Index+Num)];
    if(!OldidLink) return;
    var Oldid = ContributionsDeluxe_GetArgFromURL(OldidLink.href, "oldid");
    var Title = ContributionsDeluxe_GetArgFromURL(OldidLink.href, "title");
    if(!Oldid || !Title) return;
    var NewLink = document.createElement('a');
    NewLink.innerHTML = ContributionsDeluxe_LinkText;
    NewLink.href = "javascript:ContributionsDeluxe_getLastMod("+ContributionsDeluxe_EscapeStrHTML(Title)+","+Oldid+", "+LIindex+");";
    Li.appendChild(document.createTextNode(" - "));
    Li.appendChild(NewLink);
}

function ContributionsDeluxe_GetArgFromURL(URL, Arg){
    if(!URL || !Arg) return false;
    try{
        var Args = decodeURIComponent(URL).split("&");
        for(var a=0,l=Args.length;a<l;a++){
            if(Args[a].indexOf(Arg+"=")!=-1) return Args[a].split("=")[1];
        }
    }catch(e){
        return false;
    }
    return false;
}

function ContributionsDeluxe_EscapeStrHTML(str) {
    return "'" + (""+str).replace(/\\/g, '\\\\').replace(/'/g, '\\\'') + "'";
}

if(mw.config.get('wgCanonicalSpecialPageName') == "Contributions") addOnloadHook(ContributionsDeluxe_AddLinks);


function ContributionsDeluxe_getLastMod(Title,Oldid,LIindex){
    var OldUl = document.getElementById("UlDetail_"+LIindex);
    if(OldUl){
        if(OldUl.style.display == "none"){
            OldUl.style.display = ""

        }else{
            OldUl.style.display = "none"
        }
        return;
    }
    var URL = mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/api.php?format=xml&action=query&prop=revisions'
            + '&rvprop=ids|flags|timestamp|user|size|comment|parsedcomment|tags'
            + '&rvlimit=499'
            + '&rvendid=' + Oldid
            + '&titles='+encodeURIComponent(Title);
    wpajax.http({ url  : URL,
                  onSuccess : ContributionsDeluxe_getLastModDone,
                  title: Title,
                  oldid: Oldid,
                  li: LIindex
    });
}

function ContributionsDeluxe_getLastModDone(Req, data){
    var ObjetXML = Req.responseXML;
    if(!ObjetXML) return;
    var NewUl = document.createElement('ul');
    NewUl.id = "UlDetail_"+data.li;
    var Li = document.getElementById("Li_"+data.li);
    Li.appendChild(NewUl);
    var Revision = ObjetXML.getElementsByTagName('rev');
    for(var a=(Revision.length-1);a>-1;a--){
        var ThisRevision = Revision[a];
        var Oldid = ThisRevision.getAttribute("revid");
        if(parseInt(Oldid) == parseInt(data.oldid)) continue
        var User = ThisRevision.getAttribute("user");
        var Comment = ThisRevision.getAttribute("parsedcomment");
        if(!Comment) Comment = ThisRevision.getAttribute("comment");
        if(!Comment) Comment = "";
        var TS = ThisRevision.getAttribute("timestamp");
        var Template = ContributionsDeluxe_DefaultTemplate;
        Template = Template.split("$0").join(encodeURIComponent(data.title));
        Template = Template.split("$1").join(ContributionsDeluxe_ManageAPITimestamp(TS));
        Template = Template.split("$2").join(Oldid);
        Template = Template.split("$3").join(User);
        Template = Template.split("$4").join(Comment);
        var NewLi = document.createElement('li');
        NewLi.innerHTML = Template;
        NewUl.appendChild(NewLi);
    }
}

function ContributionsDeluxe_ManageAPITimestamp(Timestamp){
    if(!Timestamp) return false;
    var DateUgly = Timestamp.replace(/Z/g, "").split("T")[0];
    var Time = Timestamp.split("T")[1].replace(/Z/g, "").split(":");
    var Hour = parseInt(Time[0]);
    var ZeroHour = ((Hour<10) ? "0" : "");
    var Minutes = parseInt(Time[1]);
    var ZeroMinutes = ((Minutes<10) ? "0" : "");
    var Date = DateUgly.split("-");
    var Year = Date[0];
    var Month = Date[1];
    var Day = Date[2];
    var MonthName = {"01":"janvier", "02":"février", "03":"mars", "04":"avril", "05":"mai", "06":"juin", "07":"juillet", "08":"août", "09":"septembre", "10":"octobre", "11":"novembre", "12":"décembre"};
    var Date = Day+" "+MonthName[Month]+" "+Year+" à "+ZeroHour+Hour+":"+ZeroMinutes+Minutes;
    return Date;
}