MediaWiki:Common:js: различия между версиями

мНет описания правки
мНет описания правки
 
(не показана 1 промежуточная версия этого же участника)
Строка 1: Строка 1:
// Функция переключения темы
// Объявляем функцию глобально
function toggleTheme() {
window.toggleTheme = function() {
  const body = document.body;
    document.body.classList.toggle('dark-theme');
  body.classList.toggle('dark-theme');
    localStorage.setItem('theme', document.body.classList.contains('dark-theme') ? 'dark' : 'light');
 
};
  // Сохранение выбора в localStorage
  const isDark = body.classList.contains('dark-theme');
  localStorage.setItem('theme', isDark ? 'dark' : 'light');
}


// Загрузка сохраненной темы при открытии страницы
// Загрузка сохраненной темы
document.addEventListener('DOMContentLoaded', function() {
document.addEventListener('DOMContentLoaded', function() {
  const savedTheme = localStorage.getItem('theme');
    const theme = localStorage.getItem('theme');
  if (savedTheme === 'dark') {
    if (theme === 'dark') document.body.classList.add('dark-theme');
    document.body.classList.add('dark-theme');
  }
});
});


// Добавление кнопки переключения темы
// Добавляем кнопку через jQuery
// Добавление кнопки в раздел "Инструменты" левого меню
mw.loader.using('jquery').then(function() {
    $(function() {
        const button = $('<button>')
            .text('🌓 Тема')
            .css({
                position: 'fixed',
                bottom: '20px',
                right: '20px',
                zIndex: 1000,
                padding: '10px',
                cursor: 'pointer',
                background: '#fff',
                border: '1px solid #000'
            })
            .on('click', toggleTheme);


mw.hook('ve.activationComplete').add(function() {
         $('body').append(button);
    mw.util.addPortletLink(
        'p-tb', // ID раздела (p-tb = инструменты)
        '#',
        '🌓 Тема',
        'theme-switcher',
        'Переключить цветовую тему',
        null,
         $('#pt-preferences').next()
    ).find('a').on('click', function(e) {
        e.preventDefault();
        toggleTheme();
     });
     });
});
});

Текущая версия от 13:51, 1 апреля 2025

// Объявляем функцию глобально window.toggleTheme = function() {

   document.body.classList.toggle('dark-theme');
   localStorage.setItem('theme', document.body.classList.contains('dark-theme') ? 'dark' : 'light');

};

// Загрузка сохраненной темы document.addEventListener('DOMContentLoaded', function() {

   const theme = localStorage.getItem('theme');
   if (theme === 'dark') document.body.classList.add('dark-theme');

});

// Добавляем кнопку через jQuery mw.loader.using('jquery').then(function() {

   $(function() {
       const button = $('<button>')
           .text('🌓 Тема')
           .css({
               position: 'fixed',
               bottom: '20px',
               right: '20px',
               zIndex: 1000,
               padding: '10px',
               cursor: 'pointer',
               background: '#fff',
               border: '1px solid #000'
           })
           .on('click', toggleTheme);
       $('body').append(button);
   });

});