Ir para o conteúdo

MediaWiki:Common.js: mudanças entre as edições

De Wiki TokuDrive
Botão de tema claro/escuro
Limpando JS customizado
 
(18 revisões intermediárias pelo mesmo usuário não estão sendo mostradas)
Linha 1: Linha 1:
(function () {
// Sem JavaScript visual customizado.
  function applyTheme(theme) {
    document.documentElement.setAttribute('data-td-theme', theme);
    localStorage.setItem('tdTheme', theme);
    var btn = document.getElementById('td-theme-toggle');
    if (btn) {
      btn.textContent = theme === 'dark' ? '☀️ Claro' : '🌙 Escuro';
    }
  }
 
  function currentTheme() {
    var saved = localStorage.getItem('tdTheme');
    if (saved === 'dark' || saved === 'light') return saved;
    if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) return 'dark';
    return 'light';
  }
 
  function mountBtn() {
    if (document.getElementById('td-theme-toggle')) return;
    var btn = document.createElement('button');
    btn.id = 'td-theme-toggle';
    btn.type = 'button';
    btn.addEventListener('click', function () {
      var active = document.documentElement.getAttribute('data-td-theme') || 'light';
      applyTheme(active === 'dark' ? 'light' : 'dark');
    });
    document.body.appendChild(btn);
    applyTheme(currentTheme());
  }
 
  applyTheme(currentTheme());
 
  if (document.readyState === 'loading') {
    document.addEventListener('DOMContentLoaded', mountBtn);
  } else {
    mountBtn();
  }
})();

Edição atual tal como às 01h11min de 29 de junho de 2026

// Sem JavaScript visual customizado.