Ir al contenido

Diferencia entre revisiones de «MediaWiki:Common.js»

De Caballipedia
Sin resumen de edición
Etiqueta: Revertido
Sin resumen de edición
Etiqueta: Revertido
Línea 1: Línea 1:
$(document).ready(function () {
$(document).ready(function () {
   // Añadir clase anon-user al <body> para usuarios no logueados
   // Añadir clase al <body> si el usuario NO ha iniciado sesión
   if (mw.config.get('wgUserName') === null) {
   if (mw.config.get('wgUserName') === null) {
     document.body.classList.add('anon-user');
     document.body.classList.add('anon-user');
Línea 9: Línea 9:
   const action = mw.config.get('wgAction');
   const action = mw.config.get('wgAction');


  // Solo ejecutar personalizaciones en modo "view" para no interferir con edición, etc.
   if (action === 'view') {
   if (action === 'view') {
    // Para usuarios anónimos con skin Vector-2022
     if (user === null && skin === 'vector-2022') {
     if (user === null && skin === 'vector-2022') {
       console.log("🛠️ Ocultando elementos para usuarios anónimos en Vector-2022");
       console.log("🛠️ Ocultando elementos para usuarios anónimos en Vector-2022");


      // Ocultar menú herramientas completo, botón incluido
       $('#vector-page-tools, #vector-page-tools-dropdown-checkbox, label[for="vector-page-tools-dropdown-checkbox"]').hide();
       $('#vector-page-tools, #vector-page-tools-dropdown-checkbox, label[for="vector-page-tools-dropdown-checkbox"]').hide();


       // Ocultar pestañas principales que queremos para anónimos
       // Ocultar pestañas de historial y vista principal
       $('#ca-view, #ca-history').hide();
       $('#ca-view, #ca-history').hide();


      // Ocultar pestañas de tipo de contenido ("Artículo", "Categoría", etc.)
       $('#ca-nstab-main, #ca-nstab-category, #ca-nstab-special, #ca-nstab-image').closest('li').hide();
       $('#ca-nstab-main, #ca-nstab-category, #ca-nstab-special, #ca-nstab-image').closest('li').hide();
     }
     }


    // Para usuarios logueados con skin Vector-2022
     if (user !== null && skin === 'vector-2022') {
     if (user !== null && skin === 'vector-2022') {
       console.log("🔧 Configurando interfaz para usuario logueado");
       console.log("🔧 Configurando interfaz personalizada");


       function ocultarElementosUsuario() {
       function ocultarElementosUsuario() {
        // Ocultar lista de seguimiento y mensajes personales
         $('#pt-watchlist-2').hide();
         $('#pt-watchlist-2').hide();
         $('#pt-mytalk').hide();
         $('#pt-mytalk').hide();


         // Ocultar sólo botón Discusión en menú principal (no tocamos editar ni otros)
         // Ocultar sólo el botón Discusión del menú principal
         $('#ca-talk').hide();
         $('#ca-talk').hide();
       }
       }
Línea 35: Línea 39:
       ocultarElementosUsuario();
       ocultarElementosUsuario();


      // Observar cambios para ocultar si se recarga o cambia DOM dinámicamente
       const observer = new MutationObserver(ocultarElementosUsuario);
       const observer = new MutationObserver(ocultarElementosUsuario);
       observer.observe(document.body, { childList: true, subtree: true });
       observer.observe(document.body, { childList: true, subtree: true });
Línea 100: Línea 105:
       });
       });


       // Mover botón "Ejecutar tareas" debajo de "Reemplazar texto"
       // Mover el botón debajo de "Reemplazar texto"
       const runJobsItem = $('#pt-runjobs').closest('li');
       const runJobsItem = $('#pt-runjobs').closest('li');
       const target = $('a:contains("Reemplazar texto")').closest('li');
       const target = $('a:contains("Reemplazar texto")').closest('li');
Línea 107: Línea 112:
       }
       }
     }
     }
  }


  // MutationObserver para eliminar sólo botones específicos en barra sticky y menús desplegables
    // MutationObserver para ocultar el botón "Discusión" en la barra flotante y menús desplegables,
  new MutationObserver(() => {
     // sin interferir con otros botones (como Editar o Guardar).
     // Selector exacto para solo botones que queremos ocultar (sin tocar "Editar" ni "Guardar")
     new MutationObserver(() => {
     const selectors = [
      const selectors = [
      // Discusión y variantes en sticky header y menús
        '.mw-sticky-header-element #ca-talk',
      '.mw-sticky-header-element #ca-talk, #p-views #ca-talk',
        '#p-views #ca-talk',
      '.mw-sticky-header-element a[title*="Discusión"], #p-views a[title*="Discusión"]',
        '.mw-sticky-header-element a[title*="Discusión"]',
      // Lista de seguimiento
        '#p-views a[title*="Discusión"]'
      '.mw-sticky-header-element #ca-watch, #p-views #ca-watch',
       ];
       '.mw-sticky-header-element #ca-unwatch, #p-views #ca-unwatch',
      selectors.forEach(sel => {
      // Proteger/Desproteger
        document.querySelectorAll(sel).forEach(el => {
      '.mw-sticky-header-element #ca-protect, #p-views #ca-protect',
          const li = el.closest('li');
      '.mw-sticky-header-element #ca-unprotect, #p-views #ca-unprotect'
          if (li) {
    ];
            li.remove();
 
          } else {
    selectors.forEach(sel => {
            el.remove();
      document.querySelectorAll(sel).forEach(el => {
          }
        const li = el.closest('li');
         });
        if (li) {
          li.remove();
        } else {
          el.remove();
         }
       });
       });
     });
     }).observe(document.body, { childList: true, subtree: true });
  }).observe(document.body, { childList: true, subtree: true });
  }
});
});

Revisión del 02:41 25 jun 2025

$(document).ready(function () {
  // Añadir clase al <body> si el usuario NO ha iniciado sesión
  if (mw.config.get('wgUserName') === null) {
    document.body.classList.add('anon-user');
  }

  const skin = mw.config.get('skin');
  const user = mw.config.get('wgUserName');
  const action = mw.config.get('wgAction');

  // Solo ejecutar personalizaciones en modo "view" para no interferir con edición, etc.
  if (action === 'view') {
    // Para usuarios anónimos con skin Vector-2022
    if (user === null && skin === 'vector-2022') {
      console.log("🛠️ Ocultando elementos para usuarios anónimos en Vector-2022");

      // Ocultar menú herramientas completo, botón incluido
      $('#vector-page-tools, #vector-page-tools-dropdown-checkbox, label[for="vector-page-tools-dropdown-checkbox"]').hide();

      // Ocultar pestañas de historial y vista principal
      $('#ca-view, #ca-history').hide();

      // Ocultar pestañas de tipo de contenido ("Artículo", "Categoría", etc.)
      $('#ca-nstab-main, #ca-nstab-category, #ca-nstab-special, #ca-nstab-image').closest('li').hide();
    }

    // Para usuarios logueados con skin Vector-2022
    if (user !== null && skin === 'vector-2022') {
      console.log("🔧 Configurando interfaz personalizada");

      function ocultarElementosUsuario() {
        $('#pt-watchlist-2').hide();
        $('#pt-mytalk').hide();

        // Ocultar sólo el botón Discusión del menú principal
        $('#ca-talk').hide();
      }

      ocultarElementosUsuario();

      // Observar cambios para ocultar si se recarga o cambia DOM dinámicamente
      const observer = new MutationObserver(ocultarElementosUsuario);
      observer.observe(document.body, { childList: true, subtree: true });

      // Añadir botón "Ejecutar tareas"
      mw.util.addPortletLink(
        'p-tb',
        '#',
        'Ejecutar tareas',
        'pt-runjobs',
        'Ejecutar tareas pendientes desde el navegador'
      );

      $('#pt-runjobs').on('click', function (e) {
        e.preventDefault();

        if ($('#runjobs-msg').length) return;

        const heading = document.querySelector('#firstHeading');
        if (!heading) return;

        const $msg = $(`
          <div id="runjobs-msg" style="margin: 1em 0; font-size: 90%; display: flex; align-items: center; gap: 1em;">
            ¿Ejecutar las tareas pendientes ahora?
            <button id="runjobs-confirm">✅ Sí</button>
            <button id="runjobs-cancel">❌ No</button>
          </div>
        `);
        $(heading).after($msg);

        $('#runjobs-cancel').on('click', () => {
          $('#runjobs-msg').remove();
        });

        $('#runjobs-confirm').on('click', () => {
          $('#runjobs-msg').html('⏳ Ejecutando tareas...');

          fetch('/trigger-jobs.php')
            .then(res => res.text())
            .then(msg => {
              $('#runjobs-msg').html(`
                <div style="font-size: 90%; display: flex; align-items: center; gap: 1em;">
                  ${msg.trim()}
                  <button id="runjobs-close">Aceptar</button>
                </div>
              `);

              $('#runjobs-close').on('click', () => {
                $('#runjobs-msg').remove();
              });
            })
            .catch(err => {
              $('#runjobs-msg').html(`
                <div style="display: flex; align-items: center; gap: 1em; font-size: 90%;">
                  ❌ Error de red: ${err}
                  <button id="runjobs-close">Aceptar</button>
                </div>
              `);

              $('#runjobs-close').on('click', () => {
                $('#runjobs-msg').remove();
              });
            });
        });
      });

      // Mover el botón debajo de "Reemplazar texto"
      const runJobsItem = $('#pt-runjobs').closest('li');
      const target = $('a:contains("Reemplazar texto")').closest('li');
      if (runJobsItem.length && target.length) {
        runJobsItem.insertAfter(target);
      }
    }

    // MutationObserver para ocultar el botón "Discusión" en la barra flotante y menús desplegables,
    // sin interferir con otros botones (como Editar o Guardar).
    new MutationObserver(() => {
      const selectors = [
        '.mw-sticky-header-element #ca-talk',
        '#p-views #ca-talk',
        '.mw-sticky-header-element a[title*="Discusión"]',
        '#p-views a[title*="Discusión"]'
      ];
      selectors.forEach(sel => {
        document.querySelectorAll(sel).forEach(el => {
          const li = el.closest('li');
          if (li) {
            li.remove();
          } else {
            el.remove();
          }
        });
      });
    }).observe(document.body, { childList: true, subtree: true });
  }
});