Aller au contenu

« MediaWiki:Common.js » : différence entre les versions

De Wikiquestia
Aucun résumé des modifications
Aucun résumé des modifications
Ligne 1 : Ligne 1 :
/* Tout JavaScript présent ici sera exécuté par tous les utilisateurs à chaque chargement de page. */
function fallbackCopyText(text) {
  const textarea = document.createElement("textarea");
  textarea.value = text;
  textarea.setAttribute("readonly", "");
  textarea.style.position = "absolute";
  textarea.style.left = "-9999px";
  document.body.appendChild(textarea);
  textarea.select();
  try {
    document.execCommand("copy");
  } catch (err) {
    console.error("Échec de la copie fallback :", err);
  }
  document.body.removeChild(textarea);
}
 
document.addEventListener('DOMContentLoaded', function () {
document.addEventListener('DOMContentLoaded', function () {
   const buttons = document.querySelectorAll('.copy-button');
   document.querySelectorAll('.copy-button').forEach(button => {
  buttons.forEach(button => {
     button.addEventListener('click', function () {
     button.addEventListener('click', () => {
       const targetId = this.getAttribute('data-copy-target');
       const targetId = button.getAttribute('data-copy-target');
       const target = document.getElementById(targetId);
       const target = document.getElementById(targetId);
       if (target) {
       if (!target) return;
        const text = target.textContent || target.innerText;
      const text = target.innerText || target.textContent;
      // Essayons clipboard.writeText d'abord
      if (navigator.clipboard && window.isSecureContext) {
         navigator.clipboard.writeText(text).then(() => {
         navigator.clipboard.writeText(text).then(() => {
           button.textContent = '✅ Copié';
           this.textContent = '✅ Copié';
           setTimeout(() => {
           setTimeout(() => this.textContent = '📋 Copier', 2000);
            button.textContent = '📋 Copier';
         }).catch(() => {
          }, 2000);
           fallbackCopyText(text);
         }).catch(err => {
          this.textContent = '📎 Copié';
           console.error('Erreur de copie :', err);
           setTimeout(() => this.textContent = '📋 Copier', 2000);
           button.textContent = '❌ Erreur';
         });
         });
      } else {
        fallbackCopyText(text);
        this.textContent = '📎 Copié';
        setTimeout(() => this.textContent = '📋 Copier', 2000);
       }
       }
     });
     });
   });
   });
});
});

Version du 26 août 2025 à 15:44

function fallbackCopyText(text) {
  const textarea = document.createElement("textarea");
  textarea.value = text;
  textarea.setAttribute("readonly", "");
  textarea.style.position = "absolute";
  textarea.style.left = "-9999px";
  document.body.appendChild(textarea);
  textarea.select();
  try {
    document.execCommand("copy");
  } catch (err) {
    console.error("Échec de la copie fallback :", err);
  }
  document.body.removeChild(textarea);
}

document.addEventListener('DOMContentLoaded', function () {
  document.querySelectorAll('.copy-button').forEach(button => {
    button.addEventListener('click', function () {
      const targetId = this.getAttribute('data-copy-target');
      const target = document.getElementById(targetId);
      if (!target) return;
      const text = target.innerText || target.textContent;
      // Essayons clipboard.writeText d'abord
      if (navigator.clipboard && window.isSecureContext) {
        navigator.clipboard.writeText(text).then(() => {
          this.textContent = '✅ Copié';
          setTimeout(() => this.textContent = '📋 Copier', 2000);
        }).catch(() => {
          fallbackCopyText(text);
          this.textContent = '📎 Copié';
          setTimeout(() => this.textContent = '📋 Copier', 2000);
        });
      } else {
        fallbackCopyText(text);
        this.textContent = '📎 Copié';
        setTimeout(() => this.textContent = '📋 Copier', 2000);
      }
    });
  });
});