Diferencia entre revisiones de «MediaWiki:Common.js»

De Vanaheim Wiki
Ir a la navegación Ir a la búsqueda
Sin resumen de edición
Sin resumen de edición
Línea 4: Línea 4:
   }
   }


  // Delegación: capturar clicks en .copy-text aunque se creen luego
   $(document).on('click', '.copy-text', function() {
   $(document).on('click', '.copy-text', function() {
     var text = $(this).attr('data-copy');
     var text = $(this).attr('data-copy');
     if (!text) return;
    console.log('Intentando copiar:', text);
     if (!text) {
      alert('Error: no hay texto para copiar');
      return;
    }


     if (navigator.clipboard) {
     if (navigator.clipboard && window.isSecureContext) {
       navigator.clipboard.writeText(text).then(() => {
       navigator.clipboard.writeText(text).then(() => {
         $('#copy-message').fadeIn(200).delay(1000).fadeOut(200);
         $('#copy-message').fadeIn(200).delay(1000).fadeOut(200);
       }, () => {
        console.log('Copiado con Clipboard API');
         alert('Error al copiar');
       }).catch(err => {
         console.error('Clipboard API falló:', err);
        fallbackCopy(text);
       });
       });
     } else {
     } else {
      fallbackCopy(text);
    }
  });
  function fallbackCopy(text) {
    try {
       var $temp = $('<textarea>');
       var $temp = $('<textarea>');
       $('body').append($temp);
       $('body').append($temp);
       $temp.val(text).select();
       $temp.val(text).select();
       document.execCommand('copy');
       var successful = document.execCommand('copy');
       $temp.remove();
       $temp.remove();
       $('#copy-message').fadeIn(200).delay(1000).fadeOut(200);
       if (successful) {
        $('#copy-message').fadeIn(200).delay(1000).fadeOut(200);
        console.log('Copiado con execCommand');
      } else {
        alert('Error: no se pudo copiar al portapapeles');
        console.error('execCommand copy falló');
      }
    } catch(e) {
      alert('Error al copiar: ' + e);
      console.error(e);
     }
     }
   });
   }
});
});

Revisión del 18:17 17 jul 2025

$(document).ready(function() {
  if ($('#copy-message').length === 0) {
    $('body').append('<div id="copy-message" style="position:fixed; bottom:20px; right:20px; background:#4CAF50; color:white; padding:8px 12px; border-radius:4px; display:none; font-weight:bold; z-index:9999;">¡Copiado!</div>');
  }

  $(document).on('click', '.copy-text', function() {
    var text = $(this).attr('data-copy');
    console.log('Intentando copiar:', text);
    if (!text) {
      alert('Error: no hay texto para copiar');
      return;
    }

    if (navigator.clipboard && window.isSecureContext) {
      navigator.clipboard.writeText(text).then(() => {
        $('#copy-message').fadeIn(200).delay(1000).fadeOut(200);
        console.log('Copiado con Clipboard API');
      }).catch(err => {
        console.error('Clipboard API falló:', err);
        fallbackCopy(text);
      });
    } else {
      fallbackCopy(text);
    }
  });

  function fallbackCopy(text) {
    try {
      var $temp = $('<textarea>');
      $('body').append($temp);
      $temp.val(text).select();
      var successful = document.execCommand('copy');
      $temp.remove();
      if (successful) {
        $('#copy-message').fadeIn(200).delay(1000).fadeOut(200);
        console.log('Copiado con execCommand');
      } else {
        alert('Error: no se pudo copiar al portapapeles');
        console.error('execCommand copy falló');
      }
    } catch(e) {
      alert('Error al copiar: ' + e);
      console.error(e);
    }
  }
});