MediaWiki:Common.js : Différence entre versions

 
(95 révisions intermédiaires par 3 utilisateurs non affichées)
Ligne 1 : Ligne 1 :
/* Tout JavaScript ici sera chargé avec chaque page accédée par n’importe quel utilisateur. */
+
/* Tout le JavaScript ici sera chargé avec chaque page accédée par n’importe quel utilisateur. */
  
$('div#content').append( "<div class='content-footer'></div>" );
+
/* =====================================
 +
DECLARATIONS DES FONCTIONS ET PROCEDURES
 +
======================================== */
  
(function () {
+
function showElement(element) {
     var req = new XMLHttpRequest();
+
  element.classList.remove("tabber-noactive");
    req.addEventListener('load', function (ev) {
+
}
        if (this.status >= 200 && this.status < 300) {
+
 
            var data = JSON.parse(this.responseText);
+
function hideElement(element) {
            if (data.hasOwnProperty('version')) {
+
  element.classList.add("tabber-noactive");
                var gdpr = document.createElement("script");
+
}
                gdpr.src = "https://s3-static.geo.gfsrv.net/cookiebanner/" + data.version + "/cookie.min.js";
+
 
                document.head.appendChild(gdpr);
+
function toggleElement(element) {
            }
+
  element.classList.toggle("tabber-noactive");
 +
}
 +
 
 +
/* Favicon */
 +
function fixInsecureFavicon() {
 +
  document.querySelector('link[rel="shortcut icon"]').href =
 +
     "https://gf1.geo.gfsrv.net/cdn98/191b803adbf82f4b8febe3a2c38c2c.ico";
 +
}
 +
 
 +
/* Liste des équipements */
 +
function changeEquipementDisplay(container) {
 +
  var switchButton = container.querySelector(".button");
 +
  var content = container.nextElementSibling;
 +
 
 +
  switchButton.addEventListener("click", function () {
 +
    switchButton.classList.toggle("tabber-active");
 +
    toggleElement(content);
 +
  });
 +
}
 +
 
 +
/* Enlève l'animation de chargement et affiche le contenu de la page lorsque c'est nécessaire */
 +
function removeLoadingAnimation() {
 +
  var loadingAnimation = document.getElementById("loading-animation");
 +
  var showAfterLoading = document.getElementById("show-after-loading");
 +
 
 +
  if (loadingAnimation) {
 +
    hideElement(loadingAnimation);
 +
  }
 +
 
 +
  if (showAfterLoading) {
 +
    showElement(showAfterLoading);
 +
  }
 +
}
 +
 
 +
/* BOUTON RETOUR VERS LE HAUT */
 +
function addButtonTop() {
 +
  var contentText = document.querySelector("div#mw-content-text");
 +
 
 +
  if (contentText !== null) {
 +
    var divButtonTop = document.createElement("div");
 +
    divButtonTop.classList.add("top-button");
 +
    contentText.appendChild(divButtonTop);
 +
  }
 +
}
 +
 
 +
function buttonTop() {
 +
  var balise = document.querySelector("div#mw-page-header-links");
 +
  var topButton = document.querySelector(".top-button");
 +
 
 +
  if (balise !== null) {
 +
    var options = {
 +
      root: null,
 +
      rootMargin: "0px",
 +
      threshold: 0,
 +
    };
 +
 
 +
    var observer = new IntersectionObserver(callback, options);
 +
    observer.observe(balise);
 +
 
 +
    function callback(entries) {
 +
      entries.forEach(function (entry) {
 +
        if (entry.isIntersecting) {
 +
          topButton.classList.remove("show-button");
 +
        } else {
 +
          topButton.classList.add("show-button");
 
         }
 
         }
 +
      });
 +
    }
 +
 +
    topButton.addEventListener("click", function () {
 +
      document.documentElement.scrollTo({
 +
        top: 0,
 +
      });
 
     });
 
     });
     req.open('GET', "https://s3-static.geo.gfsrv.net/cookiebanner/version.json");
+
  }
    req.send();
+
}
})();
+
 
 +
/* Cookies */
 +
function cookies() {
 +
  var req = new XMLHttpRequest();
 +
  req.addEventListener("load", function (ev) {
 +
    if (this.status >= 200 && this.status < 300) {
 +
      var data = JSON.parse(this.responseText);
 +
      if (data.hasOwnProperty("version")) {
 +
        var gdpr = document.createElement("script");
 +
        gdpr.src =
 +
          "https://s3-static.geo.gfsrv.net/cookiebanner/" +
 +
          data.version +
 +
          "/cookie.min.js";
 +
        document.head.appendChild(gdpr);
 +
      }
 +
     }
 +
  });
 +
  req.open("GET", "https://s3-static.geo.gfsrv.net/cookiebanner/version.json");
 +
  req.send();
 +
}
 +
 
 +
function addScript(src, callback) {
 +
  var script = document.createElement("script");
 +
  script.src = src;
  
 +
  function onComplete() {
 +
    if (script.parentNode) {
 +
      script.parentNode.removeChild(script);
 +
    }
 +
    if (callback) {
 +
      callback();
 +
    }
 +
  }
  
/* fonction pour créer des scripts persos */
+
  script.onload = onComplete;
 +
  script.onerror = onComplete;
  
(function jsperso() {
+
  document.head.appendChild(script);
 +
}
  
  var myElement = document.getElementById('mw-mywiki-example');
+
/* =======================================
  myElement.innerHTML = '<button>hello 2</button>';
+
FONCTION GLOBALE
 +
Exécutée une fois au chargement de la page
 +
========================================== */
  
}());
+
(function () {
 +
  fixInsecureFavicon();
  
 +
  var equipmentContainer = document.querySelectorAll(
 +
    "div#mw-content-text .list-equip"
 +
  );
 +
  var loadScripts = document.querySelectorAll("div[data-load-javascript]");
  
 +
  equipmentContainer.forEach(function (container) {
 +
    changeEquipementDisplay(container);
 +
  });
  
(function jsperso2() {
+
  /* Charge des scripts spécifiques au chargement de certaines pages */
 +
  if (loadScripts.length > 0) {
 +
    var allowedScripts = [
 +
      "Tabber",
 +
      "Skills",
 +
      "Modal",
 +
      "Switch",
 +
      "Loot",
 +
      "Map",
 +
      "Filter",
 +
      "Calculator",
 +
      "Element",
 +
      "Pets",
 +
      "Colorblind",
 +
    ];
 +
    var scriptsToLoad = [];
  
  var myElement2 = document.getElementById('mw-mywiki-example2');
+
    for (var i = 0; i < loadScripts.length; i++) {
  myElement2.innerHTML = '<button>hello 4</button>'
+
      var script = loadScripts[i].dataset["loadJavascript"];
  
 +
      if (
 +
        allowedScripts.indexOf(script) !== -1 &&
 +
        scriptsToLoad.indexOf(script) === -1
 +
      ) {
 +
        scriptsToLoad.push(script);
 +
      }
 +
    }
  
mw.loader.using( ['mediawiki.util', 'mediawiki.notify'], function () {
+
    function loadNextScript() {
var extraJS = 'MediaWiki:Scriptsperso.js';
+
      if (scriptsToLoad.length > 0) {
 +
        var script = scriptsToLoad.shift();
 +
        addScript(
 +
          "/index.php?title=MediaWiki:Script/" +
 +
            script +
 +
            ".js&action=raw&ctype=text/javascript",
 +
          loadNextScript
 +
        );
 +
      } else {
 +
        removeLoadingAnimation();
 +
      }
 +
    }
  
if ( extraJS ) {
+
    loadNextScript();
// DONT REMOVE THIS IF (unless you are OK with XSS & CSRF attacks)
+
  }
if ( /^MediaWiki:[^&<>=%#]*\.js$/.test( extraJS ) ) {
+
 
mw.loader.load( '/index.php?title=' + encodeURIComponent( extraJS ) + '&action=raw&ctype=text/javascript' );
+
  if (mw.config.get("wgUserName")) {
} else {
+
    mw.loader.load(
mw.notify( 'Only pages from the MediaWiki namespace are allowed.', { title: 'Invalid withJS value' } );
+
      "/index.php?title=MediaWiki:Script/Redactor.js&action=raw&ctype=text/javascript"
}
+
    );
}
+
  }
});
+
 
}());
+
  addButtonTop();
 +
  buttonTop();
 +
  cookies();
 +
})();

Version actuelle datée du 1 décembre 2024 à 06:23

/* Tout le JavaScript ici sera chargé avec chaque page accédée par n’importe quel utilisateur. */

/* =====================================
DECLARATIONS DES FONCTIONS ET PROCEDURES
======================================== */

function showElement(element) {
  element.classList.remove("tabber-noactive");
}

function hideElement(element) {
  element.classList.add("tabber-noactive");
}

function toggleElement(element) {
  element.classList.toggle("tabber-noactive");
}

/* Favicon */
function fixInsecureFavicon() {
  document.querySelector('link[rel="shortcut icon"]').href =
    "https://gf1.geo.gfsrv.net/cdn98/191b803adbf82f4b8febe3a2c38c2c.ico";
}

/* Liste des équipements */
function changeEquipementDisplay(container) {
  var switchButton = container.querySelector(".button");
  var content = container.nextElementSibling;

  switchButton.addEventListener("click", function () {
    switchButton.classList.toggle("tabber-active");
    toggleElement(content);
  });
}

/* Enlève l'animation de chargement et affiche le contenu de la page lorsque c'est nécessaire */
function removeLoadingAnimation() {
  var loadingAnimation = document.getElementById("loading-animation");
  var showAfterLoading = document.getElementById("show-after-loading");

  if (loadingAnimation) {
    hideElement(loadingAnimation);
  }

  if (showAfterLoading) {
    showElement(showAfterLoading);
  }
}

/* BOUTON RETOUR VERS LE HAUT */
function addButtonTop() {
  var contentText = document.querySelector("div#mw-content-text");

  if (contentText !== null) {
    var divButtonTop = document.createElement("div");
    divButtonTop.classList.add("top-button");
    contentText.appendChild(divButtonTop);
  }
}

function buttonTop() {
  var balise = document.querySelector("div#mw-page-header-links");
  var topButton = document.querySelector(".top-button");

  if (balise !== null) {
    var options = {
      root: null,
      rootMargin: "0px",
      threshold: 0,
    };

    var observer = new IntersectionObserver(callback, options);
    observer.observe(balise);

    function callback(entries) {
      entries.forEach(function (entry) {
        if (entry.isIntersecting) {
          topButton.classList.remove("show-button");
        } else {
          topButton.classList.add("show-button");
        }
      });
    }

    topButton.addEventListener("click", function () {
      document.documentElement.scrollTo({
        top: 0,
      });
    });
  }
}

/* Cookies */
function cookies() {
  var req = new XMLHttpRequest();
  req.addEventListener("load", function (ev) {
    if (this.status >= 200 && this.status < 300) {
      var data = JSON.parse(this.responseText);
      if (data.hasOwnProperty("version")) {
        var gdpr = document.createElement("script");
        gdpr.src =
          "https://s3-static.geo.gfsrv.net/cookiebanner/" +
          data.version +
          "/cookie.min.js";
        document.head.appendChild(gdpr);
      }
    }
  });
  req.open("GET", "https://s3-static.geo.gfsrv.net/cookiebanner/version.json");
  req.send();
}

function addScript(src, callback) {
  var script = document.createElement("script");
  script.src = src;

  function onComplete() {
    if (script.parentNode) {
      script.parentNode.removeChild(script);
    }
    if (callback) {
      callback();
    }
  }

  script.onload = onComplete;
  script.onerror = onComplete;

  document.head.appendChild(script);
}

/* =======================================
FONCTION GLOBALE
Exécutée une fois au chargement de la page
========================================== */

(function () {
  fixInsecureFavicon();

  var equipmentContainer = document.querySelectorAll(
    "div#mw-content-text .list-equip"
  );
  var loadScripts = document.querySelectorAll("div[data-load-javascript]");

  equipmentContainer.forEach(function (container) {
    changeEquipementDisplay(container);
  });

  /* Charge des scripts spécifiques au chargement de certaines pages */
  if (loadScripts.length > 0) {
    var allowedScripts = [
      "Tabber",
      "Skills",
      "Modal",
      "Switch",
      "Loot",
      "Map",
      "Filter",
      "Calculator",
      "Element",
      "Pets",
      "Colorblind",
    ];
    var scriptsToLoad = [];

    for (var i = 0; i < loadScripts.length; i++) {
      var script = loadScripts[i].dataset["loadJavascript"];

      if (
        allowedScripts.indexOf(script) !== -1 &&
        scriptsToLoad.indexOf(script) === -1
      ) {
        scriptsToLoad.push(script);
      }
    }

    function loadNextScript() {
      if (scriptsToLoad.length > 0) {
        var script = scriptsToLoad.shift();
        addScript(
          "/index.php?title=MediaWiki:Script/" +
            script +
            ".js&action=raw&ctype=text/javascript",
          loadNextScript
        );
      } else {
        removeLoadingAnimation();
      }
    }

    loadNextScript();
  }

  if (mw.config.get("wgUserName")) {
    mw.loader.load(
      "/index.php?title=MediaWiki:Script/Redactor.js&action=raw&ctype=text/javascript"
    );
  }

  addButtonTop();
  buttonTop();
  cookies();
})();