Jump to content

User:Dr Brains/Utilities.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.
/*
    Fonctions standards issues de [[:fr:MediaWiki:Common.js]] ou de certains gadgets.
*/

if(typeof(addLoadEvent)=="undefined"){
     function addLoadEvent(func) {
          addOnloadHook(func);
     }
}

if(typeof(loadJs)=="undefined"){
     function loadJs(page) {
          importScript(page);
     }
}

if(typeof(obtenir)=="undefined"){
     function obtenir(name) {
          importScript('MediaWiki:Gadget-' + name + '.js');
     }
}

if(typeof(getVarValue)=="undefined"){
     function getVarValue(nom_variable, val_defaut){
          var result = null; 
          try{
               result = eval(nom_variable.toString());
          }catch(e){
               result = val_defaut;
          } 
          return(result);
     }
}

if(typeof(getStrDateToday)=="undefined"){
     function getStrDateToday(format){
          var str_mois = new Array();
          with (str_mois)  {
               push("janvier");
               push("février");
               push("mars");
               push("avril");
               push("mai");
               push("juin");
               push("juillet");
               push("août");
               push("septembre");
               push("octobre");
               push("novembre");
               push("décembre");
          }
          var today = new Date();
          var day = today.getDate();
          var year = today.getYear();
          if (year < 2000)  {
               year = year + 1900;
          }
          var str_date = format;
          //Création de la chaîne
          var regex = /j/gi;
          str_date = str_date.replace(regex, day.toString());
          regex = /a/gi;
          str_date = str_date.replace(regex, year.toString());
          regex = /m/gi;
          str_date = str_date.replace(regex, str_mois[today.getMonth()]);
          return (str_date);
     }
}

if(typeof(insertAfter)=="undefined"){
     function insertAfter(parent, node, referenceNode) {
          parent.insertBefore(node, referenceNode.nextSibling); 
     }
}

if(typeof(hasClass)=="undefined"){
     function hasClass(node, className) {
          if (node.className == className) {
               return true;
          }
          var reg = new RegExp('(^| )'+ className +'($| )')
          if (reg.test(node.className)) {
               return true;
          }
          return false;
     }
}

if(typeof(addClass)=="undefined"){
     function addClass(node, className) {
          if (hasClass(node, className)) {
               return false;
          }
          node.className += ' '+ className;
          return true;
     }
}

if(typeof(removeClass)=="undefined"){
     function removeClass(node, className) {
          if (!hasClass(node, className)) {
               return false;
          }
          node.className = eregReplace('(^|\\s+)'+ className +'($|\\s+)', ' ', node.className);
          return true;
     }
}

if(typeof(eregReplace)=="undefined"){
     function eregReplace(search, replace, subject) {
          return subject.replace(new RegExp(search,'g'), replace);
     }
}

if(typeof(getTextContent)=="undefined"){
     function getTextContent(oNode) {
     if (typeof(oNode.textContent)!="undefined") {return oNode.textContent;}
     switch (oNode.nodeType) {
          case 3: // TEXT_NODE
          case 4: // CDATA_SECTION_NODE
               return oNode.nodeValue;
               break;
          case 7: // PROCESSING_INSTRUCTION_NODE
          case 8: // COMMENT_NODE
               if (getTextContent.caller!=getTextContent) {
                    return oNode.nodeValue;
               }
               break;
          case 9: // DOCUMENT_NODE
          case 10: // DOCUMENT_TYPE_NODE
          case 12: // NOTATION_NODE
               return null;
               break;
          }
          var _textContent = "";
          oNode = oNode.firstChild;
          while (oNode) {
               _textContent += getTextContent(oNode);
               oNode = oNode.nextSibling;
          }
          return _textContent;
     }
}