Jump to content

MediaWiki:Gadget-HistoryNumDiff.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
/*  _____________________________________________________________________________
 * |                                                                             |
 * |                    === WARNING: GLOBAL GADGET FILE ===                      |
 * |                  Changes to this page affect many users.                    |
 * | Please discuss changes on the talk page or on [[WT:Gadget]] before editing. |
 * |_____________________________________________________________________________|
 *
 * Imported from the January 17, 2011 version at [[fr:MediaWiki:Gadget-HistoryNumDiff.js]]
 *
 * Shows the number of characters added or removed, rather than the size of the revision.
 * This format is like the one used by the Watchlist and Recent Changes. 
 * 
 * See [[MediaWiki:Gadget-HistoryNumDiff]].
 *
 * Author: The RedBurn
 *
 */
 
function getNumFromString (i,eltsByTag) {
  var regString = /\((.*) bytes?\)/;
  var resultString;
  var string = eltsByTag.item(i).innerHTML;
  var separator = /[^0-9]/g;
  var empty = "(empty)";
 
  if(mw.config.get( 'wgUserLanguage' ) == "fr")
  {
    regString = /\((.*) octets?\)/;
    empty = "(vide)";
  }
 
  if(string==empty) {
    string=0;
  } else {
    resultString= regString.exec(string);
    string = resultString[1].replace(separator,"");
    string = parseInt(string, 10);
  }
  return string;
}
 
function makeNumDiff() {
  var string;
  var resultPrevString = 0; // précédent dans l'ordre chronologique
  var resultNextString = 0;
  var className;
  var lastI = 0;
  var i = 0;
  var eltsByTag = document.getElementsByTagName("span");
  var length = eltsByTag.length;
 
  while(i<length && eltsByTag.item(i).className != "history-size") {
    i++;
  }
  if(i<length) {
    resultNextString = getNumFromString(i,eltsByTag);
    lastI = i;
    i++;
 
    while (i<length) {
      if (eltsByTag.item(i).className == "history-size"){
 
        resultPrevString = getNumFromString(i,eltsByTag);
 
        string = resultNextString - resultPrevString;
 
        if (string>0) {
          className = "mw-plusminus-pos";
          string = "+" + string;
        }
        else {
          if (string<0) {
            className = "mw-plusminus-neg";
          } else {
            className = "mw-plusminus-null";
          }
        }
        if (string<-500 || string>500) {
          string = "<strong>" + "(" + string + ")" + "</strong>";
        } else {
          string = "(" + string + ")";
        }
        eltsByTag.item(lastI).innerHTML = string ;
        eltsByTag.item(lastI).className = className;
 
        resultNextString = resultPrevString;
        lastI = i;
      }
      i++;
    }
  }
}
if(mw.config.get( 'wgAction' ) && mw.config.get( 'wgAction' ) == "history") {
  $(makeNumDiff);
}