MediaWiki:Common.js

Revisão em 11h16min de 17 de novembro de 2025 por Eleia (Discussão | contribs)
(dif) ← Revisão anterior | Revisão atual (dif) | Revisão seguinte → (dif)

Nota: Após gravar, terá de limpar a cache do seu navegador para ver as alterações.

  • Firefox / Safari: Pressione Shift enquanto clica Recarregar, ou pressione Ctrl-F5 ou Ctrl-R (⌘-R no Mac)
  • Google Chrome: Pressione Ctrl-Shift-R (⌘-Shift-R no Mac)
  • Internet Explorer: Pressione Ctrl enquanto clica Recarregar, ou pressione Ctrl-F5
  • Opera: Ir para Menu → Configurações (Opera → Preferências no Mac) e, em seguida, Privacidade e segurança → Limpar dados de navegação → Imagens e ficheiros em cache.
/* Código Javascript colocado aqui será carregado para todos os utilizadores em cada carregamento de página */

/* =====================================
   FUNÇÕES AUXILIARES
   ===================================== */

/* Tabber helpers */
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() {
  var favicon = document.querySelector('link[rel="shortcut icon"]');
  if (favicon) {
    favicon.href = "https://gf1.geo.gfsrv.net/cdn98/191b803adbf82f4b8febe3a2c38c2c.ico";
  }
}

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

  if (!switchButton || !content) return;

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

/* Remove animação de loading e mostra conteúdo assim que estiver pronto */
function removeLoadingAnimation() {
  var loadingAnimation = document.getElementById("loading-animation");
  var showAfterLoading = document.getElementById("show-after-loading");

  if (loadingAnimation) {
    hideElement(loadingAnimation);
  }

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

/* BOTÃO VOLTAR AO TOPO */
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 && topButton !== 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 GF */
function cookies() {
  var req = new XMLHttpRequest();
  req.addEventListener("load", function () {
    if (this.status >= 200 && this.status < 300) {
      try {
        var data = JSON.parse(this.responseText);
        if (data && Object.prototype.hasOwnProperty.call(data, "version")) {
          var gdpr = document.createElement("script");
          gdpr.src =
            "https://s3-static.geo.gfsrv.net/cookiebanner/" +
            data.version +
            "/cookie.min.js";
          document.head.appendChild(gdpr);
        }
      } catch (e) {
        // falha a ler JSON, ignora
      }
    }
  });
  req.open("GET", "https://s3-static.geo.gfsrv.net/cookiebanner/version.json");
  req.send();
}

/* =====================================
   SLIDESHOW
   ===================================== */

/* Slideshow con pulsanti avanti e indietro in javascript */
(function () {
  function Slideshow(element) {
    this.el = document.querySelector(element);
    if (!this.el) return;
    this.init();
  }

  Slideshow.prototype = {
    init: function () {
      this.wrapper = this.el.querySelector(".slider-wrapper_wiki");
      this.slides = this.el.querySelectorAll(".slide_wiki");
      this.previous = this.el.querySelector(".slider-previous");
      this.next = this.el.querySelector(".slider-next");
      this.index = 0;
      this.total = this.slides.length;

      if (!this.slides.length || !this.next || !this.previous) return;

      this.actions();
      this._slideTo(this.index);
    },
    _slideTo: function (slide) {
      var currentSlide = this.slides[slide];
      if (!currentSlide) return;

      currentSlide.style.display = "block";

      for (var i = 0; i < this.slides.length; i++) {
        var s = this.slides[i];
        if (s !== currentSlide) {
          s.style.display = "none";
        }
      }
    },
    actions: function () {
      var self = this;

      this.next.addEventListener(
        "click",
        function () {
          self.index++;
          self.previous.style.display = "block";

          if (self.index >= self.total - 1) {
            self.index = self.total - 1;
            self.next.style.display = "none";
          }
          if (self.index < 0) self.index = 0;

          self._slideTo(self.index);
        },
        false
      );

      this.previous.addEventListener(
        "click",
        function () {
          self.index--;
          self.next.style.display = "block";

          if (self.index <= 0) {
            self.index = 0;
            self.previous.style.display = "none";
          }

          self._slideTo(self.index);
        },
        false
      );
    }
  };

  document.addEventListener("DOMContentLoaded", function () {
    new Slideshow("#main-slider");
  });
})();

/* =====================================
   MODAIS (JQUERY)
   ===================================== */

$(".trigger").on("click", function () {
  var modal = $(this).data("modal");
  $(modal).toggle();
});

$(".modal").on("click", function (e) {
  var className = e.target.className;
  if (className === "modal" || className === "close-button") {
    $(this).closest(".modal").toggle();
  }
});

/* =======================================
   FUNÇÃO GLOBAL – corre ao carregar a página
   ======================================= */

(function () {
  fixInsecureFavicon();

  var urlStart = "/index.php?title=MediaWiki:Script/";
  var urlEnd = ".js&action=raw&ctype=text/javascript";
  var loadScripts = document.querySelectorAll("div[data-load-javascript]");
  var equipmentContainer = document.querySelectorAll(
    "div#mw-content-text .list-equip"
  );

  /* Lista de equipamentos */
  equipmentContainer.forEach(function (container) {
    changeEquipementDisplay(container);
  });

  /* Carrega scripts específicos dependendo da página */
  if (loadScripts.length) {
    var allowedScripts = {
      Tabber: true,
      Skills: true,
      Modal: true,
      Switch: true,
      Loot: true,
      Map: true,
      Filter: true,
      Calculator: true,
      Element: true,
      Pets: true,
      Colorblind: true
    };

    var scriptsToLoad = [];

    loadScripts.forEach(function (scriptElement) {
      var scriptName = scriptElement.dataset.loadJavascript;

      if (allowedScripts[scriptName]) {
        allowedScripts[scriptName] = false;
        scriptsToLoad.push(scriptName);
      }
    });

    if (scriptsToLoad.length) {
      var firstScriptName = scriptsToLoad[0];

      if (firstScriptName === "Element") {
        scriptsToLoad.shift();
        $.getScript(urlStart + firstScriptName + urlEnd, function () {
          if (typeof injectCustomElements === "function") {
            injectCustomElements();
          }
          loadNextScripts();
        });
      } else {
        loadNextScripts();
      }
    }

    function loadNextScripts() {
      scriptsToLoad.forEach(function (scriptName) {
        mw.loader.load(urlStart + scriptName + urlEnd);
      });

      removeLoadingAnimation();
    }
  }

  /* Editor avançado para utilizadores autenticados */
  if (mw.config.get("wgUserName")) {
    mw.loader.load(urlStart + "Redactor" + urlEnd);
  }

  /* Botão topo & cookies */
  addButtonTop();
  buttonTop();
  cookies();
})();