Jump to content

User:Dr Brains/Utilities.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Dr Brains (talk | contribs) at 14:23, 13 December 2009. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
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.
function addLoadEvent(func) {
  addOnloadHook(func);
}
function loadJs(page) {
  importScript(page);
}
function obtenir(name) {
  importScript('MediaWiki:Gadget-' + name + '.js');
}
function getVarValue(nom_variable, val_defaut){
	var result = null; 
	try{
		result = eval(nom_variable.toString());
	}
	catch(e){
		result = val_defaut;
	} 
	return(result);
}
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);
}

function insertAfter(parent, node, referenceNode) {
  parent.insertBefore(node, referenceNode.nextSibling); 
}

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

// Récupère proprement le contenu textuel d'un noeud et de ses noeuds descendants
// Copyright Harmen Christophe, http://openweb.eu.org/articles/validation_avancee, CC
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;
}