MediaWiki:Common.js: Difference between revisions
From OmegaMMO
|  (Created page with "→Any JavaScript here will be loaded for all users on every page load.:  // Asegurarse de que el DOM esté listo antes de ejecutar el script $(function() {     // Función para copiar texto al portapapeles     function copyTextToClipboard(text) {         if (!navigator.clipboard) {             // Fallback para navegadores antiguos o si navigator.clipboard no está disponible             // Puedes usar un alert o un método más simple como un prompt (ver notas abajo)...") | No edit summary | ||
| Line 31: | Line 31: | ||
|          } |          } | ||
|      }); |      }); | ||
| }); | |||
| /* Click-to-copy para .copy-tag */ | |||
| mw.loader.using([]).then(function () { | |||
|   function copyText(txt) { | |||
|     if (navigator.clipboard && navigator.clipboard.writeText) { | |||
|       return navigator.clipboard.writeText(txt); | |||
|     } | |||
|     // Fallback para navegadores viejos | |||
|     return new Promise(function (resolve) { | |||
|       var ta = document.createElement('textarea'); | |||
|       ta.value = txt; | |||
|       document.body.appendChild(ta); | |||
|       ta.select(); | |||
|       document.execCommand('copy'); | |||
|       document.body.removeChild(ta); | |||
|       resolve(); | |||
|     }); | |||
|   } | |||
|   $(document).on('click', '.copy-tag', function () { | |||
|     var $el = $(this); | |||
|     var txt = $el.attr('data-copy') || $el.text(); | |||
|     copyText(txt).then(function () { | |||
|       $el.addClass('copied'); | |||
|       setTimeout(function(){ $el.removeClass('copied'); }, 1400); | |||
|     }); | |||
|   }); | |||
| }); | }); | ||
Latest revision as of 21:36, 28 August 2025
/* Any JavaScript here will be loaded for all users on every page load. */
// Asegurarse de que el DOM esté listo antes de ejecutar el script
$(function() {
    // Función para copiar texto al portapapeles
    function copyTextToClipboard(text) {
        if (!navigator.clipboard) {
            // Fallback para navegadores antiguos o si navigator.clipboard no está disponible
            // Puedes usar un alert o un método más simple como un prompt (ver notas abajo)
            alert("Tu navegador no soporta la API de portapapeles. Por favor, copia el texto manualmente: " + text);
            return;
        }
        navigator.clipboard.writeText(text).then(function() {
            console.log('Texto copiado al portapapeles: ' + text);
            // Opcional: Mostrar un mensaje al usuario
            mw.notify('Texto copiado al portapapeles: ' + text, { tag: 'copy-success', type: 'success' });
        }).catch(function(err) {
            console.error('Error al copiar texto: ', err);
            mw.notify('No se pudo copiar el texto. Inténtalo de nuevo o cópialo manualmente.', { tag: 'copy-fail', type: 'error' });
        });
    }
    // Delegar el evento click para elementos con la clase 'copy-link'
    // Esto asegura que funcione incluso si el elemento se añade dinámicamente
    $('body').on('click', '.copy-link', function(e) {
        e.preventDefault(); // Evita que el enlace siga su href
        var textToCopy = $(this).data('text-to-copy');
        if (textToCopy) {
            copyTextToClipboard(textToCopy);
        } else {
            console.warn('El enlace de copia no tiene el atributo data-text-to-copy.');
        }
    });
});
/* Click-to-copy para .copy-tag */
mw.loader.using([]).then(function () {
  function copyText(txt) {
    if (navigator.clipboard && navigator.clipboard.writeText) {
      return navigator.clipboard.writeText(txt);
    }
    // Fallback para navegadores viejos
    return new Promise(function (resolve) {
      var ta = document.createElement('textarea');
      ta.value = txt;
      document.body.appendChild(ta);
      ta.select();
      document.execCommand('copy');
      document.body.removeChild(ta);
      resolve();
    });
  }
  $(document).on('click', '.copy-tag', function () {
    var $el = $(this);
    var txt = $el.attr('data-copy') || $el.text();
    copyText(txt).then(function () {
      $el.addClass('copied');
      setTimeout(function(){ $el.removeClass('copied'); }, 1400);
    });
  });
});

