Utilisateur:Alphos/abuseFilterHelper4.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.
function addAbuseFilterLastCheckUpdaters() {
  if (mw.config.get('wgPageName') != "Spécial:Filtre_antiabus") { return true; }

  var abfMainTable = document.getElementById("content").getElementsByTagName("table")[document.getElementById("content").getElementsByTagName("table").length - 1];
  if (!abfMainTable) { return true; }

  var abfMainTableFirstRowLinks = abfMainTable.getElementsByTagName("tr")[0].getElementsByTagName("a");
  if (!abfMainTableFirstRowLinks) { return true; }
  if (!abfMainTableFirstRowLinks[3]) { return true; }
  if (abfMainTableFirstRowLinks[3].innerHTML != "Nombre de détections") { return true; }

  var abfMainTableRows = abfMainTable.getElementsByTagName("tr");

  for (var i=1;i<abfMainTableRows.length;i++) {
    var abfNewLastCheckUpdater = document.createElement("span");
    abfNewLastCheckUpdater.style.fontSize = ".85em";
    abfNewLastCheckUpdater.innerHTML = "&nbsp;MàJ";
    abfNewLastCheckUpdater.addEventListener("click",abfLastCheckUpdate,false);
    abfNewLastCheckUpdater.addEventListener("mouseover",abfGetLastUpdate,false);
    abfMainTableRows[i].getElementsByTagName("td")[0].appendChild(abfNewLastCheckUpdater);
  }
  return true;
}
$(addAbuseFilterLastCheckUpdaters);

function abfLastCheckUpdate() {
  if (this.parentNode.getElementsByTagName("a")[0]) {
    var abfLastCheckUpdateFilterId = this.parentNode.getElementsByTagName("a")[0].getAttribute("href").match(/\d+$/);
    var abfLastCheckUpdateFilterHits = this.parentNode.parentNode.getElementsByTagName("td")[6].getElementsByTagName("a")[0].innerHTML.replace(/&nbsp;/,"").match(/^\d+/);
    var abfLastCheckUpdateDate = new Date();
    var abfLastCheckUpdateConfirm = "Mettre à jour la date de dernière vérification pour le filtre antiabus n°" + abfLastCheckUpdateFilterId + " (date : " + abfLastCheckUpdateDate.toLocaleString() + " ; " + abfLastCheckUpdateFilterHits + " détections) ?";
    if (!confirm(abfLastCheckUpdateConfirm)) {
      alert("Les infos de dernière vérification des déclenchements pour le filtre n°" + abfLastCheckUpdateFilterId + " n'ont finalement pas été mises à jour.");
      return false;
    }
    else { 
      var abf4xhttp = new XMLHttpRequest();

      abf4xhttp.open("GET","https://fr.wikipedia.org/w/api.php?action=query&meta=tokens&type=csrf&format=xml", false);
      abf4xhttp.send(null);

      var abf4XML = abf4xhttp.responseXML;
      var abf4EditToken = abf4XML.getElementsByTagName("api")[0].getElementsByTagName("query")[0].getElementsByTagName("tokens")[0].getAttribute("csrftoken");

      abf4xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      abf4xhttp.setRequestHeader("Connection", "close");
      abf4xhttp.open("POST","https://fr.wikipedia.org/w/api.php?action=edit&format=xml&title=" + encodeURIComponent("Utilisateur:Alphos/abuseFilterHelper/CheckUpdate/Date/") + abfLastCheckUpdateFilterId + "&summary=update&watchlist=nochange&token=" + encodeURIComponent(abf4EditToken) + "&text=" + encodeURIComponent(abfLastCheckUpdateDate.toLocaleString()),false);
      abf4xhttp.send(null);
      if (abf4xhttp.responseXML.getElementsByTagName("api")[0].getElementsByTagName("edit")[0].getAttribute("result") == "Success") { var abf4DateUpdated = true; }
      else { var abf4DateUpdated = false; }

      abf4xhttp.open("POST","https://fr.wikipedia.org/w/api.php?action=edit&format=xml&title=" + encodeURIComponent("Utilisateur:Alphos/abuseFilterHelper/CheckUpdate/Hits/") + abfLastCheckUpdateFilterId + "&summary=update&watchlist=nochange&token=" + encodeURIComponent(abf4EditToken) + "&text=" + abfLastCheckUpdateFilterHits,false);
      abf4xhttp.send(null);
      if (abf4xhttp.responseXML.getElementsByTagName("api")[0].getElementsByTagName("edit")[0].getAttribute("result") == "Success") { var abf4HitsUpdated = true; }
      else { var abf4HitsUpdated = true; }

      if ((!abf4HitsUpdated) || (!abf4HitsUpdated)) {
        alert("Tout n'a pas été mis à jour... Il faudra peut-être mettre à jour manuellement");
        var abfLastCheckUpdatedImg = document.createElement("img");
        abfLastCheckUpdatedImg.style.marginLeft = "5px";
        abfLastCheckUpdatedImg.src = "https://upload.wikimedia.org/wikipedia/commons/thumb/a/a2/X_mark.svg/10px-X_mark.svg.png";
        this.appendChild(abfLastCheckUpdatedImg);
        return false;
      }
      this.style.fontStyle = "italic";
      this.style.color = "#AAAAAA";
      this.removeEventListener("click",abfLastCheckUpdate,false);
      if (!this.getElementsByTagName("img")[0]) {
        var abfLastCheckUpdatedImg = document.createElement("img");
        abfLastCheckUpdatedImg.style.marginLeft = "5px";
      }
      else { var abfLastCheckUpdatedImg = this.getElementsByTagName("img")[0]; }
      abfLastCheckUpdatedImg.src = "https://upload.wikimedia.org/wikipedia/commons/thumb/f/fb/Yes_check.svg/10px-Yes_check.svg.png";
      this.appendChild(abfLastCheckUpdatedImg);
    }
  }
  else {
    return false;
  }
  return true;
}

function abfGetLastUpdate() {
  var abfLastUpdateFilterId = this.parentNode.getElementsByTagName("a")[0].getAttribute("href").match(/\d+$/);

  var abf4xhttp = new XMLHttpRequest();
  abf4xhttp.open("GET","https://fr.wikipedia.org/w/api.php?action=query&prop=revisions&rvlimit=1&format=xml&rvprop=timestamp|user|comment|content&titles=Utilisateur:Alphos/abuseFilterHelper/CheckUpdate/Hits/" + abfLastUpdateFilterId, false);
  abf4xhttp.send(null);

  var abf4XML = abf4xhttp.responseXML.getElementsByTagName("api")[0].getElementsByTagName("query")[0].getElementsByTagName("pages")[0].getElementsByTagName("page")[0];
  if (abf4XML.getAttribute("missing") === "") { 
    var abf4LastUpdateTitle = "pas de données pour le filtre n°" + abfLastUpdateFilterId + ". Mettre à jour ?"
  }
  else {
    var abf4LastUpdateTitle = abf4XML.getElementsByTagName("revisions")[0].getElementsByTagName("rev")[0].firstChild.nodeValue + " détections, le " + abf4XML.getElementsByTagName("revisions")[0].getElementsByTagName("rev")[0].getAttribute("timestamp") + " (UTC) ; mis à jour par " + abf4XML.getElementsByTagName("revisions")[0].getElementsByTagName("rev")[0].getAttribute("user") + ". Remettre à jour ?";
  }
  this.title = abf4LastUpdateTitle;
  this.removeEventListener("mouseover",abfGetLastUpdate,false);
  return true;
}