Utilisateur:Dr Brains/IPWatch.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.
// <nowiki>
// LIEN API 
// http://fr.wikipedia.org/w/api.php?action=query&list=recentchanges&rclimit=499&rcshow=anon&rcprop=user|title|timestamp

// LIEN CONTRIBUTIONS 
// http://fr.wikipedia.org/w/index.php?title=Special:Contributions&tagfilter=&contribs=user&target=123.*&namespace=&year=&month=-1


/////////////////////////////////////////////////////////////// PARTIE PERSONNALISABLE /////////////////////////////////////////////////////////////// 

var IPWatch_NbResults = 50;

var IPWatch_PageTitle ='Voir la page';
var IPWatch_DiffTitle ='Voir la différence de version';
var IPWatch_DiffText ='diff';
var IPWatch_HistTitle ='Voir l\'historique de cette page';
var IPWatch_HistText ='hist';
var IPWatch_EditTitle ='Modifier cette page';
var IPWatch_EditText ='edit';
var IPWatch_UserTitle ='Voir la page personnelle de cet utilisateur';
var IPWatch_TalkTitle ='Discuter avec cet utilisateur';
var IPWatch_TalkText ='d';
var IPWatch_ContribTitle ='Voir les contributions de cet utilisateur';
var IPWatch_ContribText ='c';
var IPWatch_DeletedContribTitle ='Voir les contributions supprimées de cet utilisateur';
var IPWatch_DeletedContribText ='s';
var IPWatch_BlockTitle ='Bloquer cet utilisateur';
var IPWatch_BlockText ='b';

//// Fenêtre de prévisualisation
var IPWatch_Height = 500; 

var IPWatch_LoadingText = 'Chargement';
var IPWatch_CloseWindowTitle ='Cliquer pour fermer la fenêtre de prévisualisation';
var IPWatch_CloseWindowText ='Fermer';
var IPWatch_PreviewSizePlusTitle = 'Cliquer pour augmenter la taille de la fenêtre de prévisualisation';
var IPWatch_PreviewSizePlusText = '▼';
var IPWatch_PreviewSizeMinusTitle = 'Cliquer pour diminuer la taille de la fenêtre de prévisualisation';
var IPWatch_PreviewSizeMinusText = '▲';
var IPWatch_DirectLinkText ='Lien direct';
// Boutons Boîtes déroulantes/Palettes de navigation
var IPWatch_Enrouler = '[enrouler]';
var IPWatch_Derouler = '[dérouler]';
// Boutons Sommaire
var IPWatch_EnroulerBis = '[-]';
var IPWatch_DeroulerBis = '[+]';

/////////////////////////////////////////////////////////////// FIN DE LA PARTIE PERSONNALISABLE /////////////////////////////////////////////////////////////// 

var IPWatch_Namespace;
var IPWatch_User;

if(mw.config.get('wgCanonicalSpecialPageName')=="Contributions"){
      addOnloadHook(IPWatch);
}

function IPWatch(){
      var Content = document.getElementById("bodyContent");
      if(!Content) Content = document.getElementById("mw_contentholder");
      if(!Content) Content = document.getElementById("article");
      if(!Content) Content = document.body;

      IPWatch_Form = Content.getElementsByTagName('form')[0];
      var IPWatch_Fielset = IPWatch_Form.getElementsByTagName('fieldset')[0];
      if(IPWatch_Fielset) IPWatch_Fielset.innerHTML += "<p>Pour effectuer une recherche sur une plage IP, insérer dans le champ « Adresse IP ou nom d’utilisateur » le début de l'adresse suivi d'une astérisque (*).</p>";

      IPWatch_Results = document.createElement('div');
      Content.appendChild(IPWatch_Results);
      IPWatch_Results.innerHTML = '<div id="IPWatchPreview"></div><div id="IPWatchSuivante"></div><ul id="IPWatchResultats"></ul>';
      IPWatch_Init();

}

function IPWatch_Init(){
      var URL = decodeURIComponent(document.URL);
      if(URL.indexOf('&contribs=user')!=-1){
             IPWatch_User = URL.split('&target=')[1].split('&')[0];
             IPWatch_Namespace = URL.split('&namespace=')[1].split('&')[0];
             if(IPWatch_User.indexOf("*")!=-1) IPWatch_Run('', 'On');

      }
}

var IPWatch_Match = new Array();
var IPWatch_MatchCount = -1;

function IPWatch_Run(RcStart, NewRequest){
      if(!RcStart) RcStart =''
      var Resultats = document.getElementById('IPWatchResultats');
      var PageSuivante = document.getElementById('IPWatchSuivante');
      if(NewRequest){
            while(Resultats.firstChild){Resultats.removeChild(Resultats.firstChild); }
            while(PageSuivante.firstChild){PageSuivante.removeChild(PageSuivante.firstChild); }
            PageSuivante.innerHTML = '<img src="http://upload.wikimedia.org/wikipedia/commons/8/88/Throbber_allbackgrounds_twelvebar.gif" height="18" width="18" /> <b>Recherche en cours</b>';
      }
      var AdresseRequete = mw.config.get('wgServer') + '/w/api.php?action=query&list=recentchanges&rclimit=499&rcshow=anon&rcprop=user|title|timestamp|ids|comment' + RcStart + '&rcnamespace=' + IPWatch_Namespace;
      var NouvelleRequete = new XMLHttpRequest()
      NouvelleRequete.open("GET", AdresseRequete, true);
      NouvelleRequete.send(null);
      NouvelleRequete.onreadystatechange = function() {
            if(NouvelleRequete.readyState == 4 && NouvelleRequete.status == 200) {
                  var IPWatch_MatchUser = new RegExp("^" + IPWatch_User.replace(/\./g, "\\.").replace(/\*$/, "") );

                //  if(IPWatch_MatchCount==-1){alert("^" + IPWatch_User.replace(/\./g, "\\.").replace(/\*$/, ""));  }

                  var ElementTraitement = document.createElement('div');
                  ElementTraitement.innerHTML = NouvelleRequete.responseText; 
                  var Informations = ElementTraitement.getElementsByTagName('span');
                  var AllMatch = false;
                  var AutreRequete = false;
                  for(var b=0;b<Informations.length;b++){
                        var ContenuInformation = Informations[b].innerHTML;
                        while(ContenuInformation.indexOf('&amp;')!=-1){
                              ContenuInformation = ContenuInformation.split('&amp;').join("&");
                              if(ContenuInformation.indexOf('&amp;')==-1) break;
                        }
                        while(ContenuInformation.indexOf('&#039;')!=-1){
                              ContenuInformation = ContenuInformation.split('&#039;').join("'");
                              if(ContenuInformation.indexOf('&#039;')==-1) break;
                        }
                        while(ContenuInformation.indexOf('&quot;')!=-1){
                              ContenuInformation = ContenuInformation.split('&quot;').join('"');
                              if(ContenuInformation.indexOf('&quot;')==-1) break;
                        }
                        if(ContenuInformation.indexOf('title=')!=-1){
                              var IPWatch_ThisUser = ContenuInformation.split('user="')[1].split('"')[0];
                              if( IPWatch_ThisUser.match(IPWatch_MatchUser) ){
                                    IPWatch_MatchCount++
                                    var IPWatch_ThisTitle = ContenuInformation.split('title="')[1].split('"')[0];
                                    var IPWatch_ThisNamespace = ContenuInformation.split('ns="')[1].split('"')[0];
                                    var IPWatch_ThisTimestamp = ContenuInformation.split('timestamp="')[1].split('"')[0];
                                    var IPWatch_ThisRevid = ContenuInformation.split('old_revid="')[1].split('"')[0];
                                    var IPWatch_ThisOldid = ContenuInformation.split('revid="')[1].split('"')[0];
                                    var IPWatch_ThisComment = ContenuInformation.split('comment="')[1].split('"')[0];
                                    if(IPWatch_MatchCount<=IPWatch_NbResults){

IPWatch_Match[IPWatch_MatchCount] = ''
+ '<li>' + IPWatch_ThisTimestamp.split("T").join(" ").split("Z").join(" ") 
+ ' — <a href="javascript:IPWatch_Preview(\'' + mw.config.get('wgServer') + '/wiki/' + encodeURIComponent(IPWatch_ThisTitle).replace(/'/g, "ZAPOSTROPHE") + '\')" title="'+IPWatch_PageTitle+'" >' + IPWatch_ThisTitle + '</a> '
+ '(<a href="javascript:IPWatch_Preview(\'' + mw.config.get('wgServer') + mw.config.get('wgScript') + '?title=' + encodeURIComponent(IPWatch_ThisTitle).replace(/'/g, "ZAPOSTROPHE") + '&diff=' +IPWatch_ThisOldid+ '&oldid='+IPWatch_ThisRevid+'\')" title="'+IPWatch_DiffTitle+'" >'+IPWatch_DiffText+'</a>'
+ '|<a href="javascript:IPWatch_Preview(\'' + mw.config.get('wgServer') + mw.config.get('wgScript') + '?title=' + encodeURIComponent(IPWatch_ThisTitle).replace(/'/g, "ZAPOSTROPHE") + '&action=history\')" title="'+IPWatch_HistTitle+'" >'+IPWatch_HistText+'</a>'
+ '|<a href="javascript:IPWatch_Preview(\'' + mw.config.get('wgServer') + mw.config.get('wgScript') + '?title=' + encodeURIComponent(IPWatch_ThisTitle).replace(/'/g, "ZAPOSTROPHE") + '&action=edit\')" title="'+IPWatch_EditTitle +'" >'+IPWatch_EditText+'</a>) — '
+ '<a href="javascript:IPWatch_Preview(\'' + mw.config.get('wgServer') + '/wiki/User:' + encodeURIComponent(IPWatch_ThisUser).replace(/'/g, "ZAPOSTROPHE") + '\')" title="'+IPWatch_UserTitle+'" >' + IPWatch_ThisUser + '</a> '
+ '(<a href="javascript:IPWatch_Preview(\'' + mw.config.get('wgServer') + '/wiki/User talk:' + encodeURIComponent(IPWatch_ThisUser).replace(/'/g, "ZAPOSTROPHE") + '\')" title="'+IPWatch_TalkTitle+'" >'+IPWatch_TalkText+'</a>'
+ '|<a href="javascript:IPWatch_Preview(\'' + mw.config.get('wgServer') + '/wiki/Special:Contributions/' + encodeURIComponent(IPWatch_ThisUser).replace(/'/g, "ZAPOSTROPHE") + '\')" title="'+IPWatch_ContribTitle+'" >'+IPWatch_ContribText+'</a>';

if(mw.config.get('wgUserGroups').indexOf("sysop")!=-1){ 
     IPWatch_Match[IPWatch_MatchCount] += '|<a href="javascript:IPWatch_Preview(\'' + mw.config.get('wgServer') + '/wiki/Special:DeletedContributions/' + encodeURIComponent(IPWatch_ThisUser).replace(/'/g, "ZAPOSTROPHE") + '\')" title="'+IPWatch_DeletedContribTitle+'" >'+IPWatch_DeletedContribText+'</a>'
+ '|<a href="javascript:IPWatch_Preview(\''+mw.config.get('wgServer') + '/wiki/Special:Blockip/'+encodeURIComponent(IPWatch_ThisUser)+'\');" title="'+IPWatch_BlockTitle+'">'+IPWatch_BlockText+'</a>';
}
IPWatch_Match[IPWatch_MatchCount] += ') — ' + IPWatch_ThisComment + '</li>';

                                          Resultats.innerHTML += IPWatch_Match[IPWatch_MatchCount];
                                    }else{
                                          AllMatch = true;
                                          IPWatch_MatchCount = -1;
                                          var RcStart = '&rcstart=' + IPWatch_ThisTimestamp;
                                          PageSuivante.innerHTML = '<b><a href="javascript:IPWatch_Run(\''+RcStart+'\', \'On\');" >Voir les '+IPWatch_NbResults+' résultats suivants</a></b>'
                                    }
                              }
                        }
                        if((ContenuInformation.indexOf('rcstart=')!=-1)&&(AllMatch == false)){
                              var RcStart = '&rcstart=' + ContenuInformation.split('rcstart="')[1].split('"')[0];
                              IPWatch_Run(RcStart);
                        }
                  }
            }
      }
}

function IPWatch_Preview(url){
      if(!url) return;
      url = decodeURIComponent(url).replace(/ZAPOSTROPHE/g, "'");

      var PreviewDiv = document.getElementById('IPWatchPreview');
      if(!PreviewDiv) return;
      while (PreviewDiv.firstChild){  PreviewDiv.removeChild(PreviewDiv.firstChild); }
      PreviewDiv.innerHTML = '<h2>'+IPWatch_LoadingText+'<span style="text-decoration:blink;">...</span><h2><br/>';
      var LiensAction = '<h2>'
+'<a href="javascript:IPWatch_CancelPreview();" title="'+IPWatch_CloseWindowTitle+'">'+IPWatch_CloseWindowText+'</a>'
+' | '
+'<b><a href="javascript:IPWatch_QuickPreviewSize(50);" title="'+IPWatch_PreviewSizePlusTitle+'">'+IPWatch_PreviewSizePlusText+'</a></b>'
+' : '
+'<b><a href="javascript:IPWatch_QuickPreviewSize(-50);" title="'+IPWatch_PreviewSizeMinusTitle+'">'+IPWatch_PreviewSizeMinusText+'</a></b>'
+' | '
+'<a href="'+url+'" title="'+url+'">'+IPWatch_DirectLinkText+'</a>'
+'</h2>';
      var DiffRequest = new XMLHttpRequest();
      DiffRequest.open("GET", url, true);
      DiffRequest.send(null);
      DiffRequest.onreadystatechange = function() {
            if(DiffRequest.readyState == 4 && DiffRequest.status == 200) {
                while (PreviewDiv.firstChild){  PreviewDiv.removeChild(PreviewDiv.firstChild); }
                PreviewDiv.innerHTML = LiensAction + '<div id="IPWatchPreview_PreviewContent" style="height:'+IPWatch_Height+'px;overflow-y:auto;border:2px solid black;padding:1em;font-size:0.8em;">' +  DiffRequest.responseText + '<div class="visualClear"/></div>';
                IPWatch_cleanPreviewContent(PreviewDiv);
            }
      }
}
 
 
// Fermeture de la fenêtre de prévisualisation
function IPWatch_CancelPreview(){
      var PreviewDiv = document.getElementById('IPWatchPreview');
      while (PreviewDiv.firstChild){ PreviewDiv.removeChild(PreviewDiv.firstChild); }
}
 
 
// Modification de la taille de la fenêtre de prévisualisation
function IPWatch_QuickPreviewSize(diff){
      var PreviewDiv = document.getElementById('IPWatchPreview_PreviewContent');
      PreviewDiv.style.height = (parseInt(PreviewDiv.style.height.split('px').join('')) + diff) + 'px';
}
 
 
// ################################################################################################################## GESTION DU CONTENU DE LA PREVISUALISATION
 
var IPWatch_indexSummary = 0;
var IPWatch_indexNavigationBars = 10000;
var IPWatch_indexCollapsibleTables = 10000;
 
function IPWatch_cleanPreviewContent(element){
        if(!element) element = document;
 
        var newContentWrapper = element.getElementsByTagName("div");
        var div=0;
        while(div<newContentWrapper.length){
                if( 
                ($(newContentWrapper[div]).hasClass("portlet")) 
                || 
                ($(newContentWrapper[div]).hasClass("printfooter"))
                ){
                        newContentWrapper[div].style.display="none";
                }
                if (
                ($(newContentWrapper[div]).hasClass("NavFrame"))  // ************************************* Boîtes déroulantes
                ){          
                    MessageWatchlist_indexCollapsibleTables++;
                    NavHead =  newContentWrapper[div].firstChild;
                    var NavToggle = document.createElement("a");
                    NavToggle.className = 'NavToggle';
                    NavToggle.id = 'NavToggle' + IPWatch_indexCollapsibleTables;
                    NavToggle.href = 'javascript:IPWatch_toggleCollapsibleTables(' + IPWatch_indexCollapsibleTables + ');'; 
                    NavToggle.innerHTML = IPWatch_Enrouler;
                    newContentWrapper[div].insertBefore( NavToggle, NavHead );  
                    var Content = newContentWrapper[div].getElementsByTagName("div");
                    for( var c=0;c<Content.length; c++ ) {
                        if ($(Content[c]).hasClass("NavContent")) { 
                           Content[c].id = 'NavContent' + IPWatch_indexCollapsibleTables;
                        }
                    }
                    MessageWatchlist_toggleCollapsibleTables(IPWatch_indexCollapsibleTables);
               }
 
                Div_Id = newContentWrapper[div].id;
                if(Div_Id){
                        if(
                        (Div_Id=="toctitle")     // ************************************* Sommaire
                        ){
                                IPWatch_indexSummary = (IPWatch_indexSummary+1);
                                newContentWrapper[div].id = 'toctitle_'+IPWatch_indexSummary;
                                var TitreSommaire = newContentWrapper[div].getElementsByTagName('h2')[0];
                                TitreSommaire.innerHTML = TitreSommaire.innerHTML + ' <a href="javascript:IPWatch_afficherSommaire(\'toc_'+IPWatch_indexSummary
                                +'\',\'toctitle_'+IPWatch_indexSummary
                                +'\', \''+TitreSommaire.innerHTML
                                +'\');" >'+IPWatch_DeroulerBis+'</a>';
                                var Conteneur = newContentWrapper[div].parentNode;
                                var Sommaire = Conteneur.getElementsByTagName('ul')[0];
                                Sommaire.id = 'toc_'+IPWatch_indexSummary;
                                Sommaire.style.display="none";
                        }
                        if(
                        (Div_Id=="globalWrapper")
                        ||
                        (Div_Id=="column-content")
                        ||
                        (Div_Id=="content")
                        ||
                        (Div_Id=="bodyContent")
                        ||
                        (Div_Id=="article")
                        ||
                        (Div_Id=="mw_main")
                        ||
                        (Div_Id=="mw_contentwrapper")
                        ||
                        (Div_Id=="mmw_content")
                        ||
                        (Div_Id=="mmw_contentholder")
                        ){
                                newContentWrapper[div].id = newContentWrapper[div].id + '_Preview'; 
                        }
                        if(
                        (Div_Id=="siteNotice")
                        ||
                        (Div_Id=="p-personal")
                        ||
                        (Div_Id=="topbar")
                        ||
                        (Div_Id=="quickbar")
                        ||
                        (Div_Id=="column-one")
                        ||
                        (Div_Id=="mw_portlets")
                        ||
                        (Div_Id=="page-base")
                        ||
                        (Div_Id=="head-base")
                        ||
                        (Div_Id=="head")
                        ||
                        (Div_Id=="panel")
                        ||
                        (Div_Id=="footer")
                        ||
                        (Div_Id=="previewnote")
                        ){
                                newContentWrapper[div].style.display= "none";
                        }
                }
                div++        
        }
 
        var Tables = element.getElementsByTagName( "table" ); 
        for ( var i = 0; i < Tables.length; i++ ) {
              if( $(Tables[i]).hasClass("collapsible") ){     // ************************************* Palettes de navigation
                     NavigationBar = Tables[i];
                     NavigationBar.id = "collapsibleTable" + IPWatch_indexNavigationBars ;
 
                     var Button     = document.createElement( "span" );
                     var ButtonLink = document.createElement( "a" );
                     var ButtonText = document.createTextNode( IPWatch_Enrouler );
 
                     Button.style.styleFloat = "right";
                     Button.style.cssFloat = "right";
                     Button.style.fontWeight = "normal";
                     Button.style.textAlign = "right";
                     Button.style.width = "6em";
 
                     ButtonLink.id = "collapseButton" + IPWatch_indexNavigationBars ;
                     ButtonLink.href = "javascript:IPWatch_collapseNavigationBar(" + IPWatch_indexNavigationBars + ");" ;
                     ButtonLink.appendChild( ButtonText ); 
                     Button.appendChild( ButtonLink );
 
                     var Header = NavigationBar.getElementsByTagName( "tr" )[0].getElementsByTagName( "th" )[0];
                     if (Header) {
                            Header.insertBefore( Button, Header.firstChild );
 
                     }
                     if ( $( NavigationBar ).hasClass( "collapsed" ) ) IPWatch_collapseNavigationBar( IPWatch_indexNavigationBars );
                     IPWatch_indexNavigationBars++;
               }
        }
}
 
// ###################################################### FONCTIONS SUBALTERNES A LA GESTION DU CONTENU 
 
// -------------------------------------------- Sommaire
 
function IPWatch_afficherSommaire(IDSommaire, IdTitre, TitreInitial){
       var Sommaire = document.getElementById(IDSommaire);
       Sommaire.style.display="block";
       var Titre = document.getElementById(IdTitre).getElementsByTagName('h2')[0];
       Titre.innerHTML = TitreInitial + ' <a href="javascript:IPWatch_masquerSommaire(\''+IDSommaire+'\',\''+IdTitre+'\', \''+TitreInitial+'\');" >'+IPWatch_EnroulerBis+'</a>';
}
 
function IPWatch_masquerSommaire(IDSommaire, IdTitre, TitreInitial){
       var Sommaire = document.getElementById(IDSommaire);
       Sommaire.style.display="none";
       var Titre = document.getElementById(IdTitre).getElementsByTagName('h2')[0];
       Titre.innerHTML = TitreInitial + ' <a href="javascript:IPWatch_afficherSommaire(\''+IDSommaire+'\',\''+IdTitre+'\', \''+TitreInitial+'\');" >'+IPWatch_DeroulerBis+'</a>';
}
 
// -------------------------------------------- Palettes de navigation
 
function IPWatch_collapseNavigationBar( tableIndex ) {
  var Button = document.getElementById( "collapseButton" + tableIndex );
  var Table = document.getElementById( "collapsibleTable" + tableIndex );
  if ( !Table || !Button ) return false;
 
  var Rows = Table.getElementsByTagName( "tr" ); 
 
  if ( Button.firstChild.data == IPWatch_Enrouler ) {
    for ( var i = 1; i < Rows.length; i++ ) {
      Rows[i].style.display = "none";
    }
    Button.firstChild.data = IPWatch_Derouler;
  } else {
    for ( var i = 1; i < Rows.length; i++ ) {
      Rows[i].style.display = Rows[0].style.display;
    }
    Button.firstChild.data = IPWatch_Enrouler ;
  }
}
 
// -------------------------------------------- Boîtes déroulantes 
 
function MessageWatchlist_toggleCollapsibleTables(index) {
    var NavToggle = document.getElementById("NavToggle" + index);
    var NavContent = document.getElementById("NavContent" + index);
 
    if( (!NavToggle) || (!NavContent) ) return; 
 
    var Caption = NavToggle.innerHTML;  
    if (Caption == MessageWatchlist_Enrouler) {
        NavContent.style.display = 'none';
        NavToggle.innerHTML = IPWatch_Derouler; 
    } else if (Caption == IPWatch_Derouler) {
        NavContent.style.display = 'block';
        NavToggle.innerHTML = IPWatch_Enrouler;
    }
}

// </nowiki>